??? Index exceeds matrix dimensions.

3 views (last 30 days)
I think I have a problem with my matrices but I can't figure out where to fix it. The code that I have written is:
??? Index exceeds matrix dimensions.
Error in ==> dctwatermark at 75 dct_block=dct2(cover_object(y:y+blocksize-1,x:x+blocksize-1));
  2 Comments
manjusha tikariha
manjusha tikariha on 21 May 2013
Edited: Walter Roberson on 21 May 2013
start_time=cputime;
k=20; % 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='P.jpg';
b=double(imread(file_name));
cover_object=rgb2gray(b);
% determine size of cover image
Mc=size(cover_object,1); %Height
Nc=size(cover_object,2); %Width
% determine maximum message size based on cover object, and blocksize
max_message=Mc*Nc/(blocksize^2);
% read in the message image
file_name='Desert.jpg';
c=double(imread(file_name));
message=rgb2gray(c);
Mm=size(message,1); %Height
Nm=size(message,2); %Width
% reshape the message to a vector
message=round(reshape(message,Mm*Nm,1)./512);
% 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='water.jpg';
key=double(imread(file_name))./256;
% reset MATLAB's PN generator to state "key"
% rand('state',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 = length(message_vector):1)
% 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(jj,ii)==1)
dct_block(jj,ii)=dwt_block(jj,ii)+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 and of row move to next row
if (x+blocksize) >= Nc
x=1;
y=y+blocksize;
else
x=x+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 psnr of watermarked image
p=PSNR(cover_object,watermarked_image_int);
% display watermarked image
figure(1)
imshow(watermarked_image,[])
title('Watermarked Image')

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 21 May 2013
I speculate that your cover_object is not a multiple of blocksize in one (or both) directions, and your code only testing whether the x or y starts in the object instead of testing whether the end would also be in the object.
  1 Comment
Walter Roberson
Walter Roberson on 21 May 2013
At the MATLAB command line give the command
dbstop if error
and run the program. When it stops, examine x and y and blocksize and size(cover_obj)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!