1 view (last 30 days)
Destaw Masresha on 13 Dec 2020
Edited: trong nguyen on 17 Jun 2021
k=20; % set the watermark strength
blocksize=8; % set the block size of the image to be 8
midband=[0,0,0,1,1,1,1,0; % define the frequency coefficients of 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];
Mm=size(message,1); % calculate the height of image
Nm=size(message,2); % calculate the width of image
Qm=size(message,3); % the number of image channels
n=Mm*Nm;
% n = logical(n);
%transform the watermarked image into one-dimensional row vector
message=round(reshape(message,1,n*Qm)./256);
%read the original host image and convert it into a double-precision array
Mc=size(cover_object,1);
Nc=size(cover_object,2);% calculate the height and width of original image
c=Mc/8;
% c=round(c);
% c = logical(c);
d=Nc/8;
% d = logical(d);
%d=round(d);
m=c*d; % compute blocks for image segmentation
% m=logical(m);
% calculate the variance of each piece of host image
xx=1;
for j=1:c
for i=1:d
pjhd(xx)=1/64*sum(sum(cover_object((1+(j-1)*8):j*8,(1+(i-1)*8):i*8)));
fc(xx)=1/64*sum(sum((cover_object((1+(j-1)*8):j*8,(1+(i-1)*8):i*8)-pjhd(xx)).^2));
xx=xx+1;
end
end
A=sort(fc);
B=A((c*d-n+1):c*d); % selete the top n of the variance
% B=logical(B);
% embed the watermark information into the former n block with the largest variance
fc_o=ones(1,c*d);
for g=1:n
for h=1:c*d
if B(g)==fc(h)
fc_o(h)=message(g);
h=c*d;
end
end
end
message_vector=fc_o;
watermarked_image=cover_object;
% set the MATlAB random number generator state J as the system key K
rand('state',7);
% based on current random number generator state J,a pseudo-random sequence of 0,1 is generated
pn_sequence_zero=round(rand(1,sum(sum(midband))));
% embed the watermark
x=1;y=1;
for (kk = 1:m)
% block DCT transform
dct_block=dct2(cover_object(y:y+blocksize-1,x:x+blocksize-1));
ll=1;
if (message_vector(kk)==0)
for ii=1:blocksize
for jj=1:blocksize
if (midband(jj,ii)==1)
dct_block(jj,ii)=dct_block(jj,ii)+k*pn_sequence_zero(ll);
ll=ll+1;
end
end
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);
% generate and output the image embeded with watermark
imwrite(watermarked_image_int,'dct2_watermarked.bmp','bmp');
% show the PSNR
xsz=255*255*Mc*Nc/sum(sum((cover_object-watermarked_image).^2));
psnr=10*log10(xsz)
% show the image after embedding the watermark
figure(1)
imshow(watermarked_image_int,[])
title('Watermarked Image')
trong nguyen on 17 Jun 2021
Hello Destaw Masresha,
I have face the same proplem with you at line 45:
B=A((c*d-n+1):c*d); % selete the top n of the variance
So you have to choose cover_object image 512x512x3 like lena and the message you must choose small 32x32x1. So c*d = 64x64=4096, otherwise n = 1024 (32x32) and result will not negative.
That's all for this proplem!
You can try file in attachments

Jan on 26 May 2021
The error message means, that the elements of (c*d-n+1):c*d) are either not integers or some elements are <= 0. Use the debugger to test this: Set a breakpoint in this line and check the values of the indiced.
trong nguyen on 27 May 2021
That's great ieal. Thank you so much!