Matlab Image watermarking question , using both SVD and DWT
- by Georgek
Hello all . here is a code that i got over the net ,and it is supposed to embed a watermark of size(50*20) called _copyright.bmp in the Code below . the size of the cover object is (512*512), it is called _lena_std_bw.bmp.What we did here is we did DWT2 2 times for the image , when we reached our second dwt2 cA2 size is 128*128. You should notice that the blocksize and it equals 4, it is used to determine the max msg size based on cA2 according to the following code:max_message=RcA2*CcA2/(blocksize^2). in our current case max_message would equal 128*128/(4^2)=1024. i want to embed a bigger watermark in the 2nd dwt2 and lets say the size of that watermark is 400*10(i can change the dimension using MS PAINT), what i have to do is change the size of the blocksize to 2.
so max_message=4096.Matlab gives me 3 errors and they are :
??? Error using == plus
Matrix dimensions must agree.
Error in == idwt2 at 93
x = upsconv2(a,{Lo_R,Lo_R},sx,dwtEXTM,shift)+ ... % Approximation.
Error in == two_dwt_svd_low_low at 88
CAA1 = idwt2(cA22,cH2,cV2,cD2,'haar',[RcA1,CcA1]);
The origional Code is (the origional code where blocksize =4):
%This algorithm makes DWT for the whole image and after that make DWT for
%cH1 and make SVD for cH2 and embed the watermark in every level after SVD
%(1) -------------- Embed Watermark ------------------------------------
%Add the watermar W to original image I and give the watermarked image in J
%--------------------------------------------------------------------------
% set the gain factor for embeding and threshold for evaluation
clc;
clear all;
close all;
% save start time
start_time=cputime;
% set the value of threshold and alpha
thresh=.5; alpha =0.01;
% read in the cover object
file_name='_lena_std_bw.bmp';
cover_object=double(imread(file_name));
% determine size of watermarked image
Mc=size(cover_object,1); %Height
Nc=size(cover_object,2); %Width
% read in the message image and reshape it into a vector
file_name='_copyright.bmp';
message=double(imread(file_name));
T=message;
Mm=size(message,1); %Height
Nm=size(message,2); %Width
% perform 1-level DWT for the whole cover image
[cA1,cH1,cV1,cD1] = dwt2(cover_object,'haar');
% determine the size of cA1
[RcA1 CcA1]=size(cA1)
% perform 2-level DWT for cA1
[cA2,cH2,cV2,cD2] = dwt2(cA1,'haar');
% determine the size of cA2
[RcA2 CcA2]=size(cA2)
% set the value of blocksize
blocksize=4
% reshape the watermark to a vector
message_vector=round(reshape(message,Mm*Nm,1)./256);
W=message_vector;
% determine maximum message size based on cA2, and blocksize
max_message=RcA2*CcA2/(blocksize^2)
% 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
%----------------------- process the image in blocks ----------------------
x=1;
y=1;
for (kk = 1:length(message_vector))
[cA2u cA2s cA2v]=svd(cA2(y:y+blocksize-1,x:x+blocksize-1));
% if message bit contains zero, modify S of the original image
if (message_vector(kk) == 0)
cA2s = cA2s*(1 + alpha);
% otherwise mask is filled with zeros
else
cA2s=cA2s;
end
cA22(y:y+blocksize-1,x:x+blocksize-1)=cA2u*cA2s*cA2v;
% move to next block of mask along x; If at end of row, move to next row
if (x+blocksize) >= CcA2
x=1;
y=y+blocksize;
else
x=x+blocksize;
end
end
% perform IDWT
CAA1 = idwt2(cA22,cH2,cV2,cD2,'haar',[RcA1,CcA1]);
watermarked_image= idwt2(CAA1,cH1,cV1,cD1,'haar',[Mc,Nc]);
% convert back to uint8
watermarked_image_uint8=uint8(watermarked_image);
% write watermarked Image to file
imwrite(watermarked_image_uint8,'dwt_watermarked.bmp','bmp');
% display watermarked image
figure(1)
imshow(watermarked_image_uint8,[])
title('Watermarked Image')
%(2) ----------------------------------------------------------------------
%---------- Extract Watermark from attacked watermarked image -------------
%--------------------------------------------------------------------------
% read in the watermarked object
file_name='dwt_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
% perform 1-level DWT for the whole watermarked image
[ca1,ch1,cv1,cd1] = dwt2(watermarked_image,'haar');
% determine the size of ca1
[Rca1 Cca1]=size(ca1);
% perform 2-level DWT for ca1
[ca2,ch2,cv2,cd2] = dwt2(ca1,'haar');
% determine the size of ca2
[Rca2 Cca2]=size(ca2);
% process the image in blocks
% for each block get a bit for message
x=1;
y=1;
for (kk = 1:length(message_vector))
% sets correlation to 1 when patterns are identical to avoid /0 errors
% otherwise calcluate difference between the cover image and the
% watermarked image
[cA2u cA2s cA2v]=svd(cA2(y:y+blocksize-1,x:x+blocksize-1));
[ca2u1 ca2s1 ca2v1]=svd(ca2(y:y+blocksize-1,x:x+blocksize-1));
correlation(kk)=diag(ca2s1-cA2s)'*diag(ca2s1-cA2s)/(alpha*alpha)/(diag(cA2s)*diag(cA2s));
% move on to next block. At and of row move to next row
if (x+blocksize) >= Cca2
x=1;
y=y+blocksize;
else
x=x+blocksize;
end
end
% if correlation exceeds average correlation
correlation(kk)=correlation(kk)+mean(correlation(1:Mm*Nm));
for kk = 1:length(correlation)
if (correlation(kk) > thresh*alpha);%thresh*mean(correlation(1:Mo*No)))
message_vector(kk)=0;
end
end
% reshape the message vector and display recovered watermark.
figure(2)
message=reshape(message_vector(1:Mm*Nm),Mm,Nm);
imshow(message,[])
title('Recovered Watermark')
% display processing time
elapsed_time=cputime-start_time,
please do help,its my graduation project and i have been trying this code for along time but failed miserable.
Thanks in advance