%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% author:王宇 %
% object name:基于浮点小波变换的水印方法 %
% data:20/06/07 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clc;
clear;
%读取水印信息
Wfile_name='\shuiyin.jpg';
ma=double(imread(Wfile_name))/255;
%转换成二值图像
X=im2bw(ma,0.7);
%imshow(X);
%水印置乱 再取k=1,N=96,周期为24 这里做12次变换
[m,n]=size(ma);
[matrix,row1,col1]=chaos(X,3,96,12);
%置乱水印大小
[m1,n1]=size(matrix);
%混沌序列生成
randmtx=randCL(m1,n1,0.5,3.6);
%置换的序列同混沌序列做异或运算
for i=1:m1
for j=1:n1
if randmtx(i,j)>0.36
randmtx(i,j)=0;
else
randmtx(i,j)=1;
end
end
end
matrix=xor(matrix,randmtx);
%matrix2=xor(matrix1,randmtx);
%kkk=matrix1-matrix2;
%读取载体信息
file_name='\lena.bmp';
%Sorce=imread(file_name);
cover=double(imread(file_name));
%对载体图像进行小波变化
[cA1,cH1,cV1,cD1] = dwt2(cover,'db4');
%取得高频中的中间平均值kk,ll
%ave=average(cA1);
%[kk,ll]=SearchIndex(cA1,ave);
%设定嵌入的起始位置
%if (kk-m/2>0)&&(ll-n/2>0)
% p1=kk-m/2;
% p2=ll-n/2;
%else
% p1=kk;
% p2=ll;
%end
%高频嵌入水印部分
%取模s=5 ,10,20所得效果不同
p1=22;p2=22;
for i=p1+1:p1+96
for j=p2+1:p2+96
temp(i-p1,j-p2)=mod(cA1(i,j),5);
end
end
%kmin=min(kk);
%for i=1:38025
% if abs(kk(i)-0.0043321)<0.0000001
% kkk=i;
% end
%end
%设定阈值T1,T2 图像效果与T1,T2大小有关,其值越大效果越差
T1=2;T2=4;
%水印嵌入规则
for i=1:96
for j=1:96
if (matrix(i,j)==1)&(cA1(i+p1,j+p2)>=0)
cA1(i+p1,j+p2)=cA1(i+p1,j+p2)-temp(i,j)+T1;
end
if (matrix(i,j)==0)&(cA1(i+p1,j+p2)>=0)
cA1(i+p1,j+p2)=cA1(i+p1,j+p2)-temp(i,j)+T2;
end
if (matrix(i,j)==1)&(cA1(i+p1,j+p2)<0)
cA1(i+p1,j+p2)=cA1(i+p1,j+p2)+temp(i,j)-T1;
end
if (matrix(i,j)==0)&(cA1(i+p1,j+p2)<0)
cA1(i+p1,j+p2)=cA1(i+p1,j+p2)+temp(i,j)-T2;
end
end
end
%cA1 =double( idwt2(cA2,cH2,cV2,cD2,'db4',size(cA1)))/255;
A0 = double(idwt2(cA1,cH1,cV1,cD1,'db4',size(cover)))/255;
%保存水印图片
imwrite(A0,'ImTemp.bmp','bmp');
image=imread('\ImTemp.bmp','bmp');
%image1=imnoise(image1,'gaussian',0.000005);
%imwrite(image1,'ImTemp.bmp','bmp');
%image=imread('\ImTemp.bmp','bmp');
imageY=file_name;
Ppsnr=psnr(cover,double(image));
figure(1);
%subplot(1,2,1);
imshow(image);
title('加入噪声后含水印图片')
%subplot(1,2,2);
figure(2);
imshow(X);title('水印');
figure(3);
imshow(matrix);title('经过混沌处理后置乱水印');
figure(4);
imshow(imageY);title('原图像');