Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

This is a dct code on watermark embedding. Please fix it

Asked by Lester on 27 Feb 2013
Latest activity Commented on by Amanurdeep Ka on 11 Apr 2014

The error I'm encountering is this -->

Warning: Size vector should be a row vector with integer elements.
> In watermarkemb at 27
??? Index exceeds matrix dimensions.
Error in ==> watermarkemb at 36
    dct_block=dct2(cover_object(x:x+blocksize-1,y:y+blocksize-1));

This is the code -->

clc;
clear all;
start_time=cputime;
k=50;
blocksize=8;
file_name='scene.jpg';
cover_object=double(rgb2gray(imread(file_name)));
Mc=size(cover_object,1);	     
Nc=size(cover_object,2);	        
max_message=Mc*Nc/(blocksize^2);
file_name='fishy.jpg';
message=double(rgb2gray(imread(file_name)));
Mm=size(message,1);	    
Nm=size(message,2);	                
message=round(reshape(message,Mm*Nm,1)./256);
if (length(message) > max_message)
    error('Message too large to fit in Cover Object')
end
message_pad=ones(1,max_message);
message_pad(1:length(message))=message;
watermarked_image_r=cover_object;
x=1;
y=1;
for (kk = 1:length(message_pad))
      dct_block=dct2(cover_object(y:y+blocksize-1,x:x+blocksize-1));
      if (message_pad(kk) == 0)
          if (dct_block(5,2) < dct_block(4,3))
                  temp=dct_block(4,3);
                  dct_block(4,3)=dct_block(5,2);
                  dct_block(5,2)=temp;
          end
      elseif (message_pad(kk) == 1)
          if (dct_block(5,2) >= dct_block(4,3))
                  temp=dct_block(4,3);
                  dct_block(4,3)=dct_block(5,2);
                  dct_block(5,2)=temp;
          end
      end
      if dct_block(5,2) > dct_block(4,3)
          if dct_block(5,2) - dct_block(4,3) < k
              dct_block(5,2)=dct_block(5,2)+(k/2);
              dct_block(4,3)=dct_block(4,3)-(k/2);            
          end
      else  
           if dct_block(4,3) - dct_block(5,2) < k
              dct_block(4,3)=dct_block(4,3)+(k/2);  
              dct_block(5,2)=dct_block(5,2)-(k/2);
          end
      end
      watermarked_image(y:y+blocksize-1,x:x+blocksize-1)=idct2(dct_block);    
      if (x+blocksize) >= Nc
          x=1;
          y=y+blocksize;
      else
          x=x+blocksize;
      end
  end
watermarked_image_int=uint8(watermarked_image);
imwrite(watermarked_image_int,'dct1_watermarked_circuit.jpg','jpg');
elapsed_time=cputime-start_time,
subplot(211);imshow(cover_object,[]);title('Original Image')
subplot(212);imshow(watermarked_image,[]);title('Watermarked Image')

0 Comments

Lester

Tags

2 Answers

Answer by Walter Roberson on 27 Feb 2013
Accepted answer

Put in a breakpoint and check the value of max_message

19 Comments

Davinderjeet on 25 May 2013

can you please suggest some websites for getting the code???

Davinderjeet on 29 May 2013

walter roberson please if you could suggest some topic for thesis work or websites which could be of some help. thank you

Walter Roberson
Answer by Lester on 27 Feb 2013

How do you go about embedding the watermark in the color planes? I didn't get you sir

3 Comments

Walter Roberson on 27 Feb 2013

Currently you have

cover_object=double(rgb2gray(imread(file_name)));

Change that to

cover_image = imread(file_name);
cover_object = double(cover_image(:,:,1));  %red plane

Then after you have created watermarked_image but before you write it out,

watermarked_red = cast(watermarked_image, class(cover_image));
watermarked_image = cat(3, watermarked_red, cover_image(:,:,[2 3]));
Lester on 27 Feb 2013

Thank you sir. It worked perfectly fine. You solved all my problems.

Amanurdeep Ka on 11 Apr 2014

hi, m also getting the same error "index exceeds matrix dimensions" in the above code , I have also made the changes told by Walter Roberson i.e.

Currently you have

cover_object=double(rgb2gray(imread(file_name)));

Change that to

cover_image = imread(file_name); cover_object = double(cover_image(:,:,1)); %red plane

Then after you have created watermarked_image but before you write it out,

watermarked_red = cast(watermarked_image, class(cover_image)); watermarked_image = cat(3, watermarked_red, cover_image(:,:,[2 3]));

BUT STILL GETTING THE SAME ERROR. CAN ANYBODY HELP ME.

Lester

Contact us