📄 watermark.m
字号:
% SVDwatermark.m 将载体图像分块成8*8后,然后SVD
K=8; %k=分块的大小
Q=50; %Q=量化系数
W=imread('Hubeiwuhan.bmp'); %读取水印图像Hubeiwuhan.bmp 64*64像素大小
Mm=size(W,1); %Hubeiwuhan.bmp 64*64像素大小
Nm=size(W,2);
figure(1);
subplot(321);
imshow(W);
title('the orginal watermark');
I=imread('lena512-gray.bmp');
subplot(322);
imshow(I);
title('the cover image');
II=I; % to save the original image(I)
blockrow=Mm;
blockcol=Nm;
for i=1:blockrow
for j=1:blockcol
x=(i-1)*K+1;
y=(j-1)*K+1;
BLOCK=II(x:x+K-1,y:y+K-1);
[U,S,V]=svd(double(BLOCK));
bit=W(i,j); %get the one bit wateramrk=bit
remainder=rem(S(1,1),Q);
if (bit==1) %embedding bit '1'
if (remainder<=Q/4)
S(1,1)=S(1,1)-remainder-Q/4;
else
S(1,1)=S(1,1)-remainder+3*Q/4;
end
else %embedding bit '0'
if (remainder>=3*Q/4)
S(1,1)=S(1,1)-remainder+5*Q/4;
else
S(1,1)=S(1,1)-remainder+Q/4;
end
end
BLOCKW=U*S*V'; %SVD 逆变换还原
II(x:x+K-1,y:y+K-1)=uint8(round(BLOCKW));
end
end
subplot(324);
imshow(II);
title('the watermarked image');
psnr1=PSNR(I,II)
A=double(II)-double(I);
rsm=0;
for i=1:size(A,1)
for j=1:size(A,2)
rsm=rsm+A(i,j)*A(i,j);
end
end
rsm=sqrt(rsm)/(size(A,1)*size(A,2)) % rsm是均方误差
% to extracting the wateramrk from image(II)
for i=1:blockrow
for j=1:blockcol
x=(i-1)*K+1;
y=(j-1)*K+1;
BLOCK=II(x:x+K-1,y:y+K-1);
[U,S,V]=svd(double(BLOCK));
remainder=rem(S(1,1),Q);
if (remainder>Q/2)
EW(i,j)=1;
else
EW(i,j)=0;
end
end
end
subplot(323);
imshow(EW);
title('the extracted wateramrk');
% the watermarked image is attacked
imwrite(uint8(II),'attack.jpg','jpeg','Quality',70); %对图像进行压缩,保存为attack.jpg
II1=imread('attack.jpg');
%中值滤波
%II1=medfilt2(II); %中值滤波
%维纳滤波
%II1=wiener2(II); %维纳滤波
%高斯低通滤波,H为预定义的一个滤波器
%H=fspecial('gaussian',[3,3],0.5);
%II1=imfilter(II,H);
%II1=imnoise(uint8(II),'salt & pepper',0.005); %椒盐加噪
%II1=imnoise(uint8(II),'gaussian',0.005); %高斯加噪
%II1=II;
%II1(1:200,1:200)=255; %左上角剪切100*100
%逆时针旋转5度,然后顺时针旋转5度,以还原原图像
%II1=imrotate(II,10,'bilinear','crop'); % left rotate 5 angle
%II1=imrotate(II1,-10,'bilinear','crop');
%缩放攻击1/2
%II1=imresize(II,0.5);
%II1=imresize(II1,2);
subplot(326);
imshow(II1);
title('the attacked image');
for i=1:blockrow
for j=1:blockcol
x=(i-1)*K+1;
y=(j-1)*K+1;
BLOCK=II1(x:x+K-1,y:y+K-1);
[U,S,V]=svd(double(BLOCK));
remainder=rem(S(1,1),Q);
if (remainder>Q/2)
EW(i,j)=1;
else
EW(i,j)=0;
end
end
end
subplot(325);
imshow(EW);
title('the extracted wateramrk');
corr2=NC(double(W),double(EW)) %攻击后提取的水印与原水印的归一化相关系数值N
psnr2=PSNR(I,II1) %攻击后嵌入水印后的PSNR
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -