内容:
一个基于奇异值分解的数字水印源代码,其中可以实现混沌之乱和奇异之分解的数字水印嵌入
clc
close all
clear all
% 保存开始时间
start_time=cputime;
iTimes=4; %置乱次数
alpha=0.2;
blocksize=8; % 设置块的大小
% 读入原图像
file_name='_lena_std_bw.bmp';
cover_ =double(imread(file_name));
% 原图像的行数与列数
Mc=size(cover_ ,1); %原图像行数
Nc=size(cover_ ,2); %原图像列数
%k=Mc*Nc/64;
% 最大嵌入信息量
max_message=Mc*Nc/(blocksize^2);
% 读入水印图像
file_name='xj.bmp';
I=imread(file_name);
message=double(I);
%%水印图像的行数与列数
Mm=size(message,1); %水印行数
Nm=size(message,2); %水印列数
message_vector=reshape(message,1,Mm*Nm);
% 检查水印信息是否过大
if (Mm*Nm> max_message)
error('水印太大')
end
%对水印图像进行arnold置乱
% if Mm~=Nm
% error('水印矩阵必须为方阵');
% end
% if Mm~=48
% error('必须为40*40大小,或者修改置乱次数');
% end
%
% tempImg=message;
% for n=1:iTimes % 次数
% for u=1:Mm
% for v=1:Nm
% temp=tempImg(u,v);
% ax=mod((u-1)+(v-1),Mm)+1;
% ay=mod((u-1)+2*(v-1),Nm)+1;
% outImg(ax,ay)=temp;
% end
% end
% tempImg=outImg;
% end
% message_vector=reshape(outImg,1,Mm*Nm);
message_vector=reshape(message,1,Mm*Nm);
% sequence1=round(rand(1,7));
% sequence2=round(rand(1,7));
sequence1=[1 0 1 1 0 0 0];
sequence2=[0 0 1 0 0 1 1];
% sequence1=rand(1,7);
% sequence2=rand(1,7);
% for i=1:7
% if sequence1(i)<0.333
% sequence1(i)=-1;
%计算总个数及编码后长度f 0.333
% sequence2(i)=0;
% else 0.666
% sequence2(i)=1;
% end
% end
%
k=1;
x=1;
y=1;
for (kk = 1:length(message_vector))
% 对块进行SVD变换
[u,s,v] = svd(cover_ (x:x+blocksize-1,y:y+blocksize-1));
s_orig(x:x+blocksize-1,y:y+blocksize-1)=s;
s1(1,1)=s(1,1);
if (message_vector(kk) == 1)
for i=2:8
s1(i,i)=s(i,i)+alpha*sequence1(i-1);
end
elseif (message_vector(kk) == 0)
for i=2:8
s1(i,i)=s(i,i)+alpha*sequence2(i-1);
end
end
%重新排序,并记录位置
% s1=diag(s1)';a=s1(2:8);
% s1=sort(s1);
% s1=fliplr(s1);
% for i=2:8
% index(i-1)=find(s1(i)==a);
% end
% order(:,k)=index';
% s1=diag(s1);
[u1,s11,v1]=svd(s1);
watermarked_image(x:x+blocksize-1,y:y+blocksize-1)=u*s11*v';
u_orig(x:x+blocksize-1,y:y+blocksize-1)=u;
v_orig(x:x+blocksize-1,y:y+blocksize-1)=v;
u1_orig(x:x+blocksize-1,y:y+blocksize-1)=u1;
v1_orig(x:x+blocksize-1,y:y+blocksize-1)=v1;
k=k+1;
% 移动到下一块
if (x+blocksize) >= Nc
x=1;
y=y+blocksize;
else
x=x+blocksize;
end
end
watermarked_image_uint=uint8(watermarked_image);
imwrite(watermarked_image_uint,'svd_watermarked.bmp','bmp');
figure
subplot(1,2,1);
imshow(watermarked_image,[]);
('原始图像');
subplot(1,2,2);
imshow(cover_ ,[])
('嵌入水印图像');
figure
imshow(I);
iTimes=8; %置乱次数
% 保存开始时间
start_time=cputime;
blocksize=8; % 设置块的大小
% 读入嵌入水印图像
file_name='svd_watermarked.bmp';
watermarked_image=double(imread(file_name));
% 嵌入水印图像的行数与列数
Mw=size(watermarked_image,1); %嵌入水印图像的行数
Nw=size(watermarked_image,2); %嵌入水印图像的列数
% 将图像分块提取水印
x=1;
y=1;
k=1;
for (kk = 1:max_message)
% 对块进行svd变换
[u11,s111,v11] = svd(watermarked_image(x:x+blocksize-1,y:y+blocksize-1));
d=u1_orig(x:x+blocksize-1,y:y+blocksize-1)*s111*v1_orig(x:x+blocksize-1,y:y+blocksize-1)';
sequence3=diag((d-s_orig(x:x+blocksize-1,y:y+blocksize-1)))';
sequence3=fix(sequence3(2:8));
% ss=u(x:x+blocksize-1,y:y+blocksize-1)'*watermarked_image(x:x+blocksize-1,y:y+blocksize-1)*v(x:x+blocksize-1,y:y+blocksize-1);
% ss=diag(s);
% sequence3=ss(2:8)';
%
% % sequence1=sequence1(order(:,k)');
% % sequence2=sequence2(order(:,k)');
% %
relation1=corr2(sequence3,sequence1);
relation2=corr2(sequence3,sequence2);
if relation1>relation2
message_vector(kk)=1;
else
message_vector(kk)=0;
end
% 移动到下一块
k=k+1;
if (x+blocksize) >= Mw
x=1;
y=y+blocksize;
else
x=x+blocksize;
end
end
% arnold置乱
% tempImg=reshape(message_vector(1:Mm*Nm),Mm,Nm);
% %tempImg=message_vector;
% message_arnold=tempImg;
% for n=1:iTimes % 次数
% for u=1:Mm
% for v=1:Nm
% temp=tempImg(u,v);
% ax=mod((u-1)+(v-1),Mm)+1;
% ay=mod((u-1)+2*(v-1),Nm)+1;
% outImg(ax,ay)=temp;
% end
% end
% tempImg=outImg;
% end
% message=outImg;
% 将message重新排列
message_vector=message_vector(1,1:Mm*Nm);
message=reshape(message_vector,Mm,Nm);
% 显示运行时间
elapsed_time=cputime-start_time,
% 显示提取水印与原始水印
figure
subplot(1,2,1);
imshow(message,[]);
('提取水印');
subplot(1,2,2);
imshow(I);
('原始水印');
p=corr2(I,message)
p1=corr2(cover_ ,watermarked_image)
psnr=psnr(cover_ ,watermarked_image)
for i=1:50
zaosheng=round(rand(48,48));
p(i)=corr2(I,zaosheng);
p(10)=corr2(I,message);
end
figure
plot(p);