Création d'une fonction sous matlab

Fermé
sarah - 26 avril 2012 à 22:02
 Utilisateur anonyme - 29 avril 2012 à 16:02
Bonjour,
j'ai écris deux programmes sous matlab, le premier effectue un tatouage avec la dct et le deuxième effectue la récupération de la mark (tatouage) de l'image tatouée.
ces deux programmes sont longs et je veux les transformer en fonction afin de les utiliser dans d'autre programme. Veuiller m'aider à les transfomer s'il vous plait le plus vite possible.
Voici le programme qui effectue le tatouage:


clear all;

% save start time
start_time=cputime;

k=50; % set gain factor for embeding
blocksize=8; % set the dct blocksize

midband=[ 0,0,0,1,1,1,1,0; % defines the mid-band frequencies of an 8x8 dct
0,0,1,1,1,1,0,0;
0,1,1,1,1,0,0,0;
1,1,1,1,0,0,0,0;
1,1,1,0,0,0,0,0;
1,1,0,0,0,0,0,0;
1,0,0,0,0,0,0,0;
0,0,0,0,0,0,0,0 ];

% read in the cover object
file_name='Lena_512x512_Grey.bmp';
cover_object=double(imread(file_name));

% display original image
figure(1)
imshow(cover_object)
title('original Image')

% determine size of cover image
Mc=size(cover_object,1); %Height Lignes
Nc=size(cover_object,2); %Width Colonnes

% determine maximum message size based on cover object, and blocksize
max_message=Mc*Nc/(blocksize^2);

% read in the message image
file_name='Copyright_64x64.bmp';
message=double(imread(file_name));
Mm=size(message,1); %Height
Nm=size(message,2); %Width

% reshape the message to a vector
message=round(reshape(message,Mm*Nm,1));

% check that the message isn't too large for cover
%if (length(message) > max_message)
% error('Message too large to fit in Cover Object')
%end

% pad the message out to the maximum message size with ones's
message_vector=ones(1,max_message);
message_vector(1:length(message))=message;

% generate shell of watermarked image
watermarked_image=cover_object;

% read in key for PN generator
file_name='key.bmp';
key=double(imread(file_name));

Km=size(key,1); %Height
Kn=size(key,2); %Width
key=round(reshape(key,Km*Kn,1));
% reset MATLAB's PN generator to state "key"
rand('state',sum(key));

% generate PN sequence
pn_sequence_zero=round(2*(rand(1,sum(sum(midband)))-0.5));

% process the image in blocks
x=1; % col
y=1; % lign
for (kk = 1:length(message_vector))

% transform block using DCT
dct_block=dct2(cover_object(y:y+blocksize-1,x:x+blocksize-1));

% if message bit contains zero then embed pn_sequence_zero into the mid-band
% componants of the dct_block
ll=1;
if (message_vector(kk)==0)
for ii=1:blocksize
for jj=1:blocksize
if (midband(ii,jj)==1)
dct_block(ii,jj)=dct_block(ii,jj)+k*pn_sequence_zero(ll);
ll=ll+1;
end
end
end
end

% transform block back into spatial domain
watermarked_image(y:y+blocksize-1,x:x+blocksize-1)=idct2(dct_block);

% move on to next block. At end of row move to next row
if (y+blocksize) >= Mc
y=1;
x=x+blocksize;
else
y=y+blocksize;
end
end

% convert to uint8 and write the watermarked image out to a file
watermarked_image_int=uint8(watermarked_image);
imwrite(watermarked_image_int,'dct2_watermarked.bmp','bmp');

% display processing time
elapsed_time=cputime-start_time,

% display watermarked image
figure(2)
imshow(watermarked_image,[])
title('Watermarked Image')



et voici le 2éme programme(récupération de la mark):
clear all;

% save start time
start_time=cputime;

blocksize=8; % set the dct blocksize

midband=[ 0,0,0,1,1,1,1,0; % defines the mid-band frequencies of an 8x8 dct
0,0,1,1,1,1,0,0;
0,1,1,1,1,0,0,0;
1,1,1,1,0,0,0,0;
1,1,1,0,0,0,0,0;
1,1,0,0,0,0,0,0;
1,0,0,0,0,0,0,0;
0,0,0,0,0,0,0,0 ];

% read in the watermarked object
file_name='dct2_watermarked.bmp';
watermarked_image=double(imread(file_name));

% determine size of watermarked image
Mw=size(watermarked_image,1); %Height
Nw=size(watermarked_image,2); %Width

% determine maximum message size based on cover object, and blocksize
max_message=Mw*Nw/(blocksize^2);

% read in original watermark
file_name='Copyright_64x64.bmp';
orig_watermark=double(imread(file_name));

% determine size of original watermark
Mo=size(orig_watermark,1); %Height
No=size(orig_watermark,2); %Width

% read in key for PN generator
file_name='key.bmp';
key=double(imread(file_name));

Km=size(key,1); %Height
Kn=size(key,2); %Width
key=round(reshape(key,Km*Kn,1));
% reset MATLAB's PN generator to state "key"
rand('state',sum(key));

% generate PN sequence
pn_sequence_zero=round(2*(rand(1,sum(sum(midband)))-0.5));

% process the image in blocks
x=1;
y=1;
for (kk = 1:max_message)

% transform block using DCT
dct_block=dct2(watermarked_image(y:y+blocksize-1,x:x+blocksize-1));

% extract the middle band coeffcients
ll=1;
for ii=1:blocksize
for jj=1:blocksize
if (midband(ii,jj)==1)
sequence(ll)=dct_block(ii,jj);
ll=ll+1;
end
end
end

% calculate the correlation of the middle band sequence to pn sequence
correlation(kk)=corr2(pn_sequence_zero,sequence);

% move on to next block. At and of row move to next row
if (y+blocksize) >= Mw
y=1;
x=x+blocksize;
else
y=y+blocksize;
end
end

% if correlation exceeds threshold, set bit to '0', otherwise '1'
for (kk=1:Mo*No)
if (correlation(kk) >= mean(correlation(1:Mo*No)))
message_vector(kk)=0;
else
message_vector(kk)=1;
end
end

% reshape the embeded message
message=reshape(message_vector(1:Mo*No),Mo,No);

% display processing time
elapsed_time=cputime-start_time,

% display recovered message
figure(2)
imshow(message)
title('Recovered Message')


aider moi s'il vous plait!!!
merci.
A voir également:

1 réponse

Utilisateur anonyme
29 avril 2012 à 16:02
Une fonction sur matlab s'écrit comme ça:

function sortie = nom_de_la_fonction (paramètre1, paramètre2, ...)
... ;
... ;
sortie = quelque_chose;
end


Elle s'utilise comme ça:

variable = nom_de_la_fonction(params);


A noter que si on désire écrire ses fonctions dans le même fichier que le code appelant, il faut également englober le code principal dans une fonction du type:

function nom_du_fichier()
... ;
appel de fonctions;
...;
end
1