clear clc
M =256;%原图像长度
N =32;%水印图像长度
K =8;
%生成M*M的矩阵
I=zeros(M,M);
J=zeros(N,N);
BLOCK=zeros(K,K);
subplot(3,3,1);%将图像放在一张2*3的图上,此图在第一行第一列
I=imread('C:\Documents and Settings\Administrator\桌面\图像.jpg','jpg');
imshow(I);
title('原始图像'); %显示原图像
subplot(3,3,2);
T=rgb2gray(I);
imshow(T);
title('灰度图');%灰度图像
%%%%%%%%图像二值化%%%%%%%%%
subplot(3,3,3);
T=I;
J=find(T<150);
T(J)=0;
J=find(T>=150);
T(J)=255;
bw=im2bw(T,0.5);%选取阈值为0.5
imshow(bw) %显示二值图象
title('图像二值化');
subplot(3,3,4);
J=imread('C:\Documents and Settings\Administrator\桌面\水印.jpg','jpg');
imshow(J);
title ('水印图像'); %显示水印图像
%%%%%%%%%%%%水印嵌入,使用dct离散余弦变换算法%%%%%%%%%%%%%%%%%%
for p=1:N
for q=1:N
x=(p-1)*K+1;
y=(q-1)*K+1;
BLOCK =I(x:x+K-1,y:y+K-1);
BLOCK=dct2(BLOCK);%dct变换输出
if J(p,q)==0
a=-1;
else
a=1;
end
BLOCK(1,1)=BLOCK(1,1)*(1+a*0.03);
BLOCK=idct2(BLOCK); %逆变换输出
I(x:x+K-1,y:y+K-1)=BLOCK;
end
end
subplot(3,3,5);
imshow(I);
title('嵌入水印后的图像');%显示嵌入水印后的图像
%%%%%%%%%%%%%%%%%%%从嵌入水印的图像中提取水印%%%%%%%%%%%%%%%%%%%%%%5
imwrite(I,'C:\Documents and Settings\Administrator\桌面\图像.jpg','jpg');
I=imread('C:\Documents and Settings\Administrator\桌面\图像.jpg','jpg');
J=imread('C:\Documents and Settings\Administrator\桌面\水印.jpg','jpg');
J=imnoise(J,'gaussian',0,0.01);
subplot(3,3,6);
imshow(J,[]);
title('加入高斯噪声');
for p=1:N%水印攻击
for q=1:N
x=(p-1)*K+1;
y=(q-1)*K+1;
BLOCK1 =I(x:x+K-1,y:y+K-1);%将原图分成8*8的子块
BLOCK2 =J(x:x+K-1,y:y+K-1);
BLOCK1=dct2(BLOCK1);
BLOCK2=dct2(BLOCK2);
a = BLOCK2(1,1)/BLOCK1(1,1)-1;
if a<0
W(p,q)=0;
else
W(p,q)=1;
end
end
end
subplot(3,3,7);
imshow(W);
title('从含水印图像中提取的水印'); %显示提取的水印