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

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')

 採用された回答

Walter Roberson
Walter Roberson 2013 年 2 月 27 日

0 投票

Put in a breakpoint and check the value of max_message

19 件のコメント

Lester
Lester 2013 年 2 月 27 日
I tweaked the max_message. Now the output is getting cropped.
Walter Roberson
Walter Roberson 2013 年 2 月 27 日
What did you tweak it to?
Did you resolve the problem with index out of range?
Lester
Lester 2013 年 2 月 27 日
I have no clue how to do with the index problem sir. I tweaked it to this -->
Mc=size(cover_object,1); Nc=size(cover_object,1);
max_message= Mc*Nc/(blocksize^2);
Walter Roberson
Walter Roberson 2013 年 2 月 27 日
編集済み: Walter Roberson 2013 年 2 月 27 日
Leave Nc=size(cover_object,2); like you had before, but use
max_message = floor(Mc/blocksize) * floor(Nc/blocksize);
This will also help correct your problem with index out of range.
Lester
Lester 2013 年 2 月 27 日
Thanks a lot sir. It worked. The watermark is invisible. How to make it visible? Is it possible to get the output image in colored format?
Walter Roberson
Walter Roberson 2013 年 2 月 27 日
Instead of converting to grayscale, you can embed the watermark into one of the color planes, and then put the color planes back together.
Lester
Lester 2013 年 2 月 27 日
Any idea to have a slightly visible watermark sir? As you know the watermark is invisible in our case.
Walter Roberson
Walter Roberson 2013 年 2 月 27 日
No idea. I do not look at the code about how the watermark is calculated or embedded.
Lester
Lester 2013 年 2 月 27 日
編集済み: Walter Roberson 2013 年 5 月 20 日
Sir we have done the changes that you had suggested about color planes but what changes should we do in the watermark extraction code so that we get the colored output.
clc;
clear all;
start_time=cputime;
blocksize=8;
file_name='dct1_watermarked_circuit.jpg';
watermarked_image=double(imread(file_name));
Mw=size(watermarked_image,1);
Nw=size(watermarked_image,2);
max_message=Mw*Nw/(blocksize^2);
file_name='lena.jpg';
orig_watermark=double(rgb2gray(imread(file_name)));
Mo=size(orig_watermark,1);
No=size(orig_watermark,2);
x=1;
y=1;
for (kk = 1:max_message)
dct_block=dct2(watermarked_image(y:y+blocksize-1,x:x+blocksize-1));
if dct_block(5,2) > dct_block(4,3)
message_vector(kk)=0;
else
message_vector(kk)=1;
end
if (x+blocksize) >= Nw
x=1;
y=y+blocksize;
else
x=x+blocksize;
end
end
message=reshape(message_vector(1:Mo*No),Mo,No);
elapsed_time=cputime-start_time,
subplot(212);imshow(message,[]);title('Recovered Message')
subplot(211);imshow(watermarked_image,[]);title('Watermarked Image')
Walter Roberson
Walter Roberson 2013 年 2 月 27 日
No idea. I do not pay attention to how watermarks are calculated or embedded.
Davinderjeet
Davinderjeet 2013 年 5 月 20 日
編集済み: Walter Roberson 2013 年 5 月 20 日
this code resulted in the error written below
??? subplot(212);imshow(message,[]);title('Recovered Message') subplot(211);imshow(watermarked_image,[]);title('Watermarked Image')
|
Error: Unexpected MATLAB expression.
please help me rectify it
Walter Roberson
Walter Roberson 2013 年 5 月 20 日
On the line
elapsed_time=cputime-start_time,
change the comma to semi-colon
elapsed_time=cputime-start_time;
Davinderjeet
Davinderjeet 2013 年 5 月 24 日
thank you Walter Roberson for your timely response.
Davinderjeet
Davinderjeet 2013 年 5 月 24 日
編集済み: Davinderjeet 2013 年 5 月 24 日
Walter can you please provide code for DWT watermarking of images.
Davinderjeet
Davinderjeet 2013 年 5 月 24 日
編集済み: Davinderjeet 2013 年 5 月 24 日
and Walter if you could suggest a good topic related to watermarking that can be persued for m.tech?
Walter Roberson
Walter Roberson 2013 年 5 月 24 日
As I indicated above, "I do not pay attention to how watermarks are calculated or embedded."
Davinderjeet
Davinderjeet 2013 年 5 月 25 日
can you please suggest some websites for getting the code???
Davinderjeet
Davinderjeet 2013 年 5 月 29 日
編集済み: Davinderjeet 2013 年 5 月 29 日
walter roberson please if you could suggest some topic for thesis work or websites which could be of some help. thank you

サインインしてコメントする。

その他の回答 (1 件)

Lester
Lester 2013 年 2 月 27 日

0 投票

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

5 件のコメント

Walter Roberson
Walter Roberson 2013 年 2 月 27 日
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
Lester 2013 年 2 月 27 日
編集済み: Lester 2013 年 2 月 27 日
Thank you sir. It worked perfectly fine. You solved all my problems.
Amanurdeep Ka
Amanurdeep Ka 2014 年 4 月 11 日
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.
Nova Sri Wahyuni
Nova Sri Wahyuni 2016 年 7 月 2 日
how to recover watermarking color image?

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by