Error in using "Rand" function while embedding a watermark in an image.

1 view (last 30 days)
I am a M.Tech student and I am pursuing my thesis work in Image Watermarking domain. I found a code over the internet and tried to implement it But I encountered an error while doing so. The error is with random function.
The error is as follows:-
*Error using rand*
*State must be a double scalar or the output of RAND('state').*
*Error in dwt_cdma_recover_section (line 15)*
*rand('state',key);*
*Error in cdma_dwt_watermark_section (line 54)*
*messageR = dwt_cdma_recover_section(watermarkR, message_imageR, key);*
I searched a lot about this error on the internet but could not find any appropriate solution.
The code is:-
if true
% code
*%cdma_dwt_watermark_section.m*
%This is the image we want to watermark
original_image = 'lena_color.bmp';
%This is the image we want to embed in the watermark
message_image = 'testmessage.bmp';
message_image_sections = double(imread(message_image));
[Mx My] = size(message_image_sections);
%This is for embeding messages in RGB spectrum
message_imageR = message_image_sections(:, 1:(My*(1/3)));
message_imageG = message_image_sections(:, (My*(1/3)):(My*(2/3)));
message_imageB = message_image_sections(:, (My*(2/3)):My);
%This is the key that will be used with both the encrypter and decrypter
key = 'key_working.bmp';
figure
imshow(key);
%This is the name of the output watermark image
watermarked_name = 'watermarked_image.bmp';
%This is used to set the gain
k = 2;
%Set to 1 to test RGB watermark encoding
color = 1;
% Read in image
file_name = original_image;
image_object=double(imread(file_name));
%Display UnWatermarked Image
figure
imshow(original_image);
title('Original Image');
%Used for testing
if color == 1
%Seperate the image to its RGB
image_objectR = image_object(:, :, 1);
image_objectG = image_object(:, :, 2);
image_objectB = image_object(:, :, 3);
%Run the watermark embed on each spectrum
watermarkR = dwt_cdma_embed_section(image_objectR, message_imageR, key, k);
watermarkG = dwt_cdma_embed_section(image_objectG, message_imageG, key, k);
watermarkB = dwt_cdma_embed_section(image_objectB, message_imageB, key, k);
%Combine the individual watermarked RGB into one image
watermark(:, :, 1) = watermarkR;
watermark(:, :, 2) = watermarkG;
watermark(:, :, 3) = watermarkB;
%Display the final watermarked image
figure
imshow(watermark, []);
title('Watermarked Image Color');
imwrite(watermark, watermarked_name,'bmp');
%Recover the Watermark message from the Red
messageR = dwt_cdma_recover_section(watermarkR, message_imageR, key);
figure
imshow(messageR, []);
title('Hidden Message Red');
%Recover the Watermark message from the Green
messageG = dwt_cdma_recover_section(watermarkG, message_imageG, key);
figure
imshow(messageG, []);
title('Hidden Message Green');
%Recover the Watermark message from the Blue
messageB = dwt_cdma_recover_section(watermarkB, message_imageB, key);
figure
imshow(messageB, []);
title('Hidden Message Blue');
message_full(:, 1:(My*(1/3))) = messageR;
message_full(:, (My*(1/3)):(My*(2/3))) = messageG;
message_full(:, (My*(2/3)):My) = messageB;
figure
imshow(message_full, []);
title('Rebuilt Hidden Message');
else
%Embed Message into Watermarked Image
watermark = dwt_cdma_embed(image_object, message_image, key, k);
figure
imshow(watermark, []);
title('Watermarked Image');
message = dwt_cdma_recover(watermark, message_image, key);
figure
imshow(message, []);
title('Hidden Message');
imwrite(watermark, watermarked_name,'bmp');
end
*%dwt_cdma_recover_section.m*
function [message] = dwt_cdma_recover_section(watermarked_image, message_image, key_image)
%clear all;
thresh = 0.5; % used for testing threshold
% determine size of watermarked image
[Mw Nw] = size(watermarked_image);
% read in the message
orig_watermark = message_image;
% determine size of original watermark
[Mo No] = size(orig_watermark);
% read in key for PN generator
key=double(imread(key_image))./256;
% reset MATLAB's PN generator to state "key"
rand('state',key);
% initalize message to all ones
message_vector=ones(1,Mo*No);
[LL,HL,LH,HH] = dwt2(watermarked_image,'haar');
correlation_h = zeros(message_vector);
correlation_v = zeros(message_vector);
correlation = zeros(message_vector);
% add pn sequences to HL and LH componants when message = 0
for kk=1:length(message_vector)
pn_sequence_h=round(2*(rand(Mw/2,Nw/2)-thresh));
pn_sequence_v=round(2*(rand(Mw/2,Nw/2)-thresh));
correlation_h(kk)=corr2(HL,pn_sequence_h);
correlation_v(kk)=corr2(LH,pn_sequence_v);
correlation(kk)=(correlation_h(kk)+correlation_v(kk))/2;
end
for kk=1:length(message_vector)
if (correlation(kk) > mean(correlation))
message_vector(kk)=0;
end
end
message=reshape(message_vector,Mo,No);
return
Key (key_working2.bmp)
key =
0.8242
0.5391
0.7813
0.5625
0.5586
0.2734
0.2773
0.4805
0.6250
0.5391
0.4648
0.5859
0.4375
0.5508
0.4297
0.4648
0.4414
0.5273
0.5234
0.3398
0.7344
0.6836
0.4063
0.6875
0.3594
0.8555
0.4688
0.3594
0.0430
0.5820
0.6133
0.5547
0.5156
0.8711
0.6719
I have written only the functions which are giving error. this is not the complete code.
Please help me.
  3 Comments
ddd hhh
ddd hhh on 11 Jun 2013
Edited: ddd hhh on 11 Jun 2013
Sir the embedding code is as follows:-
if true
%This is the image we want to watermark
original_image = 'lena_color.bmp';
%This is the image we want to embed in the watermark
message_image = 'testmessage.bmp';
message_image_sections = double(imread(message_image));
[Mx My] = size(message_image_sections);
%This is for embeding messages in RGB spectrum
message_imageR = message_image_sections(:, 1:(My*(1/3)));
message_imageG = message_image_sections(:, (My*(1/3)):(My*(2/3)));
message_imageB = message_image_sections(:, (My*(2/3)):My);
%This is the key that will be used with both the encrypter and decrypter
key = 'key_working2.bmp';
%This is the name of the output watermark image
watermarked_name = 'watermarked_image.bmp';
%This is used to set the gain
k = 2;
%Set to 1 to test RGB watermark encoding
color = 1;
% Read in image
file_name = original_image;
image_object=double(imread(file_name));
%Display UnWatermarked Image
figure
imshow(original_image);
title('Original Image');
%Used for testing
if color == 1
%Seperate the image to its RGB
image_objectR = image_object(:, :, 1);
image_objectG = image_object(:, :, 2);
image_objectB = image_object(:, :, 3);
%Run the watermark embed on each spectrum
watermarkR = dwt_cdma_embed_section(image_objectR, message_imageR, key, k);
watermarkG = dwt_cdma_embed_section(image_objectG, message_imageG, key, k);
watermarkB = dwt_cdma_embed_section(image_objectB, message_imageB, key, k);
%Combine the individual watermarked RGB into one image
watermark(:, :, 1) = watermarkR;
watermark(:, :, 2) = watermarkG;
watermark(:, :, 3) = watermarkB;
%Display the final watermarked image
figure
imshow(watermark, []);
title('Watermarked Image Color');
imwrite(watermark, watermarked_name,'bmp');
%Recover the Watermark message from the Red
messageR = dwt_cdma_recover_section(watermarkR, message_imageR, key);
figure
imshow(messageR, []);
title('Hidden Message Red');
%Recover the Watermark message from the Green
messageG = dwt_cdma_recover_section(watermarkG, message_imageG, key);
figure
imshow(messageG, []);
title('Hidden Message Green');
%Recover the Watermark message from the Blue
messageB = dwt_cdma_recover_section(watermarkB, message_imageB, key);
figure
imshow(messageB, []);
title('Hidden Message Blue');
message_full(:, 1:(My*(1/3))) = messageR;
message_full(:, (My*(1/3)):(My*(2/3))) = messageG;
message_full(:, (My*(2/3)):My) = messageB;
figure
imshow(message_full, []);
title('Rebuilt Hidden Message');
else
%Embed Message into Watermarked Image
watermark = dwt_cdma_embed(image_object, message_image, key, k);
figure
imshow(watermark, []);
title('Watermarked Image');
message = dwt_cdma_recover(watermark, message_image, key);
figure
imshow(message, []);
title('Hidden Message');
imwrite(watermark, watermarked_name,'bmp');
end end
dwt_cdma_embed.m
if true
function [watermarked] = dwt_cdma_embed(image_object, message_image, key_image, k)
thresh = 0.5; %Used for testing threshold
% determine size of watermarked image
[Mw Nw]=size(image_object);
% read in the message image
file_name= message_image;
message=double(imread(file_name));
[Mm Nm] = size(message);
message_vector=round(reshape(message,Mm*Nm,1)./256);
% read in key for PN generator
%key = imread(key_image);
key=double(imread(key_image))./256;
% reset MATLAB's PN generator to state "key"
rand('state',key);
[LL,HL,LH,HH] = dwt2(image_object,'haar');
%[LL,HL,LH,HH] = dwt2(cover_object, 'db1');
% add pn sequences to HL and LH componants when message = 0
for kk=1:length(message_vector)
pn_sequence_h=round(2*(rand(Mw/2,Mw/2)-thresh));
pn_sequence_v=round(2*(rand(Mw/2,Mw/2)-thresh));
if (message(kk) == 0)
HL=HL+k*pn_sequence_h;
LH=LH+k*pn_sequence_v;
end
end
% perform IDWT
watermarked_image = idwt2(LL,HL,LH,HH,'haar',[Mw,Nw]);
% convert back to uint8
watermarked_image_uint8=uint8(watermarked_image);
watermarked = watermarked_image_uint8;
return
end
dwt_cdma_embed_section.m
if true
function [watermarked] = dwt_cdma_embed_section(image_object, message_image, key_image, k)
thresh = 0.5; %used for testing of different thresholds
% determine size of watermarked image
[Mw Nw]=size(image_object);
% read in the message image
message = message_image;
[Mm Nm] = size(message);
message_vector=round(reshape(message,Mm*Nm,1)./256);
% read in key for PN generator
%key = imread(key_image);
key=double(imread(key_image))./256;
% reset MATLAB's PN generator to state "key"
rand('state',key);
[LL,HL,LH,HH] = dwt2(image_object,'haar');
%[LL,HL,LH,HH] = dwt2(cover_object, 'db1');
% add pn sequences to HL and LH componants when message = 0
for kk=1:length(message_vector)
pn_sequence_h=round(2*(rand(Mw/2,Mw/2)-thresh));
pn_sequence_v=round(2*(rand(Mw/2,Mw/2)-thresh));
if (message(kk) == 0)
HL=HL+k*pn_sequence_h;
LH=LH+k*pn_sequence_v;
end
end
% perform IDWT
watermarked_image = idwt2(LL,HL,LH,HH,'haar',[Mw,Nw]);
% convert back to uint8
watermarked_image_uint8=uint8(watermarked_image);
watermarked = watermarked_image_uint8;
return
end

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!