1

I am getting error in the code below "matrix dimensions must agree" It occurs in the line of code (Shw=Sh+a*Sw;) The coverImage I am using is a grayscale image, tiff format as is the watermark. Any suggestions what my problem could be?? Thank you

filename='107_3.tif';
coverImage = imread(filename);
Mc=size(coverImage,1);
Nc=size(coverImage,2);

a=10;


watermark = imread('dmg1.tif');

watermark=im2bw(watermark,0.05);
Mn=size(watermark,1);
Nn=size(watermark,2);

[LL,LH,HL,HH] = dwt2(coverImage,'haar');
%[LL1,LH1,HL1,HH1] = dwt2(HH,'haar');

Ih=idwt2([],[],[],HH,'haar');

[Uh,Sh,Vh]=svd(Ih);
[Uw,Sw,Vw]=svd(double(watermark));

Shw=Sh+a*Sw;**%%%%%ERROR OCCURRING HERE%%%%%**
VhT=transpose(Vh);
Ihw=Uh*Shw*VhT;
[LL2,LH2,HL2,HH2]=dwt2(Ihw,'haar');
watermarked_image=idwt2(LL,LH,LH,HH,'haar');
figure;
imshow(watermarked_image,[]);
title('Watermarked Image');
chiliNUT
  • 18,989
  • 14
  • 66
  • 106

1 Answers1

0
[Uh,Sh,Vh]=svd(Ih);
[Uw,Sw,Vw]=svd(double(watermark));

When you run svd, the resultant matrix Sh has the same dimensions as Ih, and resultant Sw has the dimensions as watermark.
http://www.mathworks.com/help/matlab/ref/svd.html

Now,

Shw=Sh+a*Sw;

you are adding 2 matrices together. Matrix addition requires that the matrices you are adding together have the same dimensions (same number of rows, same number of cols) http://en.wikipedia.org/wiki/Matrix_addition

So, what this appears to mean is: Your code expects the watermark to be the same dimensions as the input image. I don't know what images you are using in your situation, but it seems like that is a faulty expectation. I am guessing that the watermark you are using is typically smaller, likely much smaller, than the input image you are embedding it into.

So it appears you either need to

  1. Use a watermark that is the same dimensions as the input or
  2. Modify your code so that the dimensions of the watermark are not dependent on the dimensions of the input image.
chiliNUT
  • 18,989
  • 14
  • 66
  • 106
  • 1
    Thank you for a your detailed explanation..Yes, the watermark image is muh smaller than the cover image. Any ideas on how I could modify the code to hide a smaller watermark image? Thanks – Hitmanpaddy Jan 30 '15 at 13:21
  • I believe that all you have to do is pad out the watermark with 0s to add it to the original image. It is the problem of "adding a matrix to a larger matrix," this page should help: http://www.mathworks.com/matlabcentral/answers/77743-put-a-small-matrix-in-a-bigger-one – chiliNUT Jan 30 '15 at 15:48