clear all
close all
clc;
%%
I = imread('1.png');
% I = double(rgb2gray(I));
% I = im2double(I);
[WS,map,alpha] = imread('水印01.png'); %读取水印图像
W=double(255-alpha); %alpha 为透明度,此图不透明alpha=0
[mw,nw]=size(W);
W=logical(W);
figure,subplot(121),imshow(I);title('载体图像')
subplot(122),imshow(W);title('水印图像')
%%
ntimes=20;rngseed=59433;
%添加水印
Iw = (setdwtwatermark(I,W,ntimes,rngseed));
%计算水印图像的峰值信噪比
mn=numel(I);
Imax=max(I(:));
% psnr=10*log10(mn*double(Imax)^2/sum((im2double(I(:))-im2double(Iw(:))).^2));
peaksnr=psnr(im2uint8(Iw),I);
%提取水印
Wg=getdwtwatermark(Iw,W,ntimes,rngseed);
nc=sum(Wg(:).*W(:))/sqrt(sum(Wg(:).^2))/sqrt(sum(W(:).^2));
figure,subplot(121),imshow(Iw);title('嵌入水印后的载体图像')
subplot(122),imshow(Wg),title(['从载体图像中提取出的水印,NC=',num2str(nc)]);
%%水印攻击实验
function dwtwatermarkattack(action,Iw,W,ntimes,rngseed)
%action;水印攻击类型
%Iw:嵌入水印的图像
%W:原始水印,用来计算相关性
%ntimes,rngseed:水印算法秘钥
%模拟水印攻击
switch lower(action)
case 'filter'
Ia=imfilter(Iw,ones(3)/9);
case 'gaussian noise'
Ia=imnoise(Iw,'gaussian');
case 'resize'
Ia=imresize(Iw,0.5);
Ia=imresize(Ia,2);
case 'noise'
Ia=imnoise(Iw,'salt & pepper',0.01);
case 'crop'
Ia=Iw;
Ia(50:400,50:400,:)=randn();
case 'rotate'
Ia=imrotate(Iw,45,'nearest','crop');
Ia=imrotate(Ia,-45,'nearest','crop');
case 'motion filter' %运动模糊
PSF = fspecial('motion',20,15);
Ia = imfilter(Iw,PSF,'conv','circular');
case 'gaussian filter' %高斯模糊
G = fspecial('gaussian', [5 5], 2);
Ia = imfilter(Iw,G,'same');
end
unction Iw =setdwtwatermark(I,W,ntimes,rngseed)
%W水印图像,二值图,且长宽相等
%ntimes:秘钥1,Arnold置乱次数
%rngseed:秘钥2,随机数种子
%Iw:添加了水印后的图像
type=class(I);
% I=double(I);
% IR = I(:,:,1); IG = I(:,:,2); IB =( I(:,:,3));
% I=IB;
IHSV=rgb2hsv(I);
IH=IHSV(:,:,1); IS=IHSV(:,:,2); IV=IHSV(:,:,3);
I=IV*255;
[mI,nI]=size(I);
[mW,nW]=size(W);
if mW~=nW
error('SETDWTWATERMARK:ARNOLD','ARNOLD置乱要求水印图像长宽必须相等!')
end
%%
%对载波图像进行水印分解
[ca1,ch1,cv1,cd1]=dwt2(I,'haar');
[ca2,ch2,cv2,cd2]=dwt2(ca1,'haar');
%%
%对水印进行置乱
Wa=W;
H=[1,1;1,2]^ntimes;
for i=1:nW
for j=1:nW
idx=mod(H*[i-1;j-1],nW)+1;
Wa(idx(1),idx(2))=W(i,j);
end
end
%%
%小波数字水印的嵌入
cd2w=cd2;
rng(rngseed);
idx=randperm(numel(cd2),numel(Wa));%随机打乱一个数字序列
for i=1:numel(Wa)
c=cd2(idx(i));
z=mod(c,nW);
if Wa(i)
if z<nW/4
f=c-nW/4-z;
else
f=c+nW*3/4-z;
end
else
if z<nW*3/4
f=c+nW/4-z;
else
f=c+nW*5/4-z;
end
end
cd2w(idx(i))=f;
end
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.