From: paul erasto on
Can someone please help me to debug the code below, I need to use it as soon as possible:

thresh=0.5; alpha =0.01;

% read in the cover object file_name='_lena_std_bw.bmp';
cover_object=double(imread('C:\Users\Guest\Desktop\baby.bmp'));

% 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='C:\Users\Guest\Desktop\key.jpg';
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+blcksize-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;
From: Walter Roberson on
paul erasto wrote:
> Can someone please help me to debug the code below, I need to use it as
> soon as possible:

What problems do *you* see in the code?

I do not use Windows, so I cannot run the code, but if you describe particular
errors and your interpretation of what is going on, someone may be able to
assist you.
From: paul erasto on

reply:
I find the following errors:
1. In the watermark embedding module
??? Error using ==> mtimes
Inner matrix dimensions must agree.

Error in ==> trial at 115
correlation(kk)=diag(ca2s1-cA2s)'*diag(ca2s1-cA2s)/(alpha*alpha)/(diag(cA2s)*diag(cA2s));


2. In the extraction of the watermark module
??? Error using ==> mtimes
Inner matrix dimensions must agree.

Error in ==> extract_trial at 35
correlation(kk)=diag(ca2s1-cA2s)*diag(ca2s1-cA2s)/(alpha*alpha)/(diag(cA2s)*diag(cA2s));

>> test=diag(ca2s1-cA2s)*diag(ca2s1-cA2s)/(alpha*alpha)/(diag(cA2s)*diag(cA2s));
??? Error using ==> mtimes
Inner matrix dimensions must agree.


Please someone help, am using this for my project needed urgently. Thanks in advance. You can e-mail me @ erastopaul(a)gmail.com

The code is below:..............................................................................................

thresh=0.5; alpha =0.01;

% read in the cover object file_name='_lena_std_bw.bmp';
cover_object=double(imread('C:\Users\Guest\Desktop\baby.bmp'));

% 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='C:\Users\Guest\Desktop\key.jpg';
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),[]));
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);
% 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=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;
From: Walter Roberson on
paul erasto wrote:
>
> reply:
> I find the following errors: 1. In the watermark embedding module
> ??? Error using ==> mtimes
> Inner matrix dimensions must agree.
>
> Error in ==> trial at 115
>
> correlation(kk)=diag(ca2s1-cA2s)'*diag(ca2s1-cA2s)/(alpha*alpha)/(diag(cA2s)*diag(cA2s));

In your code, cA2s is a matrix. diag() applied to a matrix returns a
column vector, an n x 1 matrix. In the lost term of your expression, you
multiply the diagonal of cA2s by itself, which is going to be an n x 1
matrix multiplied by an n x 1 matrix. When matrix multiplication is
used, the second dimension of the first matrix must be the same as the
first dimension of the second matrix, but in your case they are not, as
the 1 which is the second dimension of the first matrix does not match
the n that is the first dimension of the second matrix.

The first part before the first division is okay because it is
multiplying the transpose of an n x 1 matrix by an n x 1 matrix, which
is 1 x n times n x 1 which will result in a 1 x 1 matrix.

Note that transposing the first diag(cA2s) would result in a value that
was the same as sum(diag(cA2s).^2) if that is what you were looking for
at that point.


I think you had better take a careful look at your section of code
starting with

for kk = 1:length(message_vector)

as it seems very unlikely to be what you want. For example, why bother
to do the svd of a matrix when you already know the svd from the
previous iteration of the loop (the svd will be the same as the previous
iteration's final cA2u cA2s cA2v)
From: paul erasto on
Hi Walter, Thanx for the ideas. I have done what you suggested as seen in the lines below:

[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));


however, i think svd(cA2....) is different from svd(ca2....).

i get the following errors:
??? Index exceeds matrix dimensions.

Error in ==> trialcum at 112
[cA2u cA2s cA2v]=svd(cA2(y:y+blocksize-1,x:x+blocksize-1));

Am also still new to matlab, got this code from somewhere on the net. If possible could u please debug it for me.....I'll be so greatful. erastopaul(a)gmail.com