MATLAB程序上面显示错误,水印嵌入总是出不来,求大神指点,帮忙修改!毕设急用!

2024年11月21日 00:26
有1个网友回答
网友(1):

您好,这样的:
%% ---------------------------------------------------------------------%%
% 基于小波变换的扩频图象数字水印-->水印嵌入
%% ---------------------------------------------------------------------%%
start_time=cputime;
k=0.6;
file_name='E:\zy\lena.bmp';
iCover = imread(file_name);
dCover = double(iCover);
dOrigCover = dCover;
Mc=size(iCover,1);
Nc=size(iCover,2);
file_name='E:\zy\copyright.bmp';
iWatermark = imread(file_name);
dWatermark = double(iWatermark);
Mm=size(iWatermark,1);
Nm=size(iWatermark,2);
dWatermarkVector=round(reshape(dWatermark,Mm*Nm,1)./256);
file_name='key.bmp';
key=double(imread(file_name))./256;
rand('state',key);
h = waitbar(0,'Embedding, please wait...');
[cA1,cH1,cV1,cD1] = dwt2(dCover,'haar');
[cA2,cH2,cV2,cD2] = dwt2(cH1,'haar');
[cA3,cH3,cV3,cD3] = dwt2(cV1,'haar');
iLength = length(dWatermarkVector);
for (kk=1:iLength)
pn_sequence_h2=round(2*(rand(Mc/4,Nc/4)-0.5));
pn_sequence_v2=round(2*(rand(Mc/4,Nc/4)-0.5));

pn_sequence_h3=round(2*(rand(Mc/4,Nc/4)-0.5));
pn_sequence_v3=round(2*(rand(Mc/4,Nc/4)-0.5));

if (dWatermarkVector(kk) == 0)
cH2=cH2+k*pn_sequence_h2;
cV2=cV2+k*pn_sequence_v2;
cH3=cH3+k*pn_sequence_h3;
cV3=cV3+k*pn_sequence_v3;
end
waitbar(kk/iLength,h);
end
close(h);
cH1 = idwt2(cA2,cH2,cV2,cD2,'haar',[Mc/2,Nc/2]);
cV1 = idwt2(cA3,cH3,cV3,cD3,'haar',[Mc/2,Nc/2]);
dEmbed = idwt2(cA1,cH1,cV1,cD1,'haar',[Mc,Nc]);
uEmbed = uint8(dEmbed);
imwrite(uEmbed,'dwt.bmp','bmp');
PSNR = psnr(dOrigCover,dEmbed);
elapsed_time=cputime-start_time
figure(1),imshow(iCover,[]);
title('Original Image');
figure(2),imshow(uEmbed,[]);
title(strcat('Watermarked Image',',PSNR=',num2str(PSNR)));
%% END------------------------------------------------------------------%%
%% ---------------------------------------------------------------------%%
% 基于小波变换的扩频图象数字水印-->水印检测
%% ---------------------------------------------------------------------%%
start_time=cputime;
k=0.6;
file_name='~dwt.bmp';
iEmbed = imread(file_name);
dEmbed = double(iEmbed);
Mc=size(iEmbed,1);
Nc=size(iEmbed,2);
file_name='copyright.bmp';
iWatermark = imread(file_name);
dWatermark = double(iWatermark);
ddWatermark=dWatermark;
Mw=size(iWatermark,1);
Nw=size(iWatermark,2);
file_name='key.bmp';
key=double(imread(file_name))./256;
rand('state',key);
MessageVector=ones(1,Mw*Nw);
h = waitbar(0,'Detecting, please wait...');
[cA1,cH1,cV1,cD1] = dwt2(dEmbed,'haar');
[cA2,cH2,cV2,cD2] = dwt2(cH1,'haar');
[cA3,cH3,cV3,cD3] = dwt2(cV1,'haar');
iLength = length(MessageVector);
for (kk=1:iLength)
pn_sequence_h2=round(2*(rand(Mc/4,Nc/4)-0.5));
pn_sequence_v2=round(2*(rand(Mc/4,Nc/4)-0.5));

pn_sequence_h3=round(2*(rand(Mc/4,Nc/4)-0.5));
pn_sequence_v3=round(2*(rand(Mc/4,Nc/4)-0.5));

correlation_h2(kk)=corr2(cH2,pn_sequence_h2);
correlation_v2(kk)=corr2(cV2,pn_sequence_v2);
correlation_h3(kk)=corr2(cH3,pn_sequence_h3);
correlation_v3(kk)=corr2(cV3,pn_sequence_v3);

correlation(kk)=(correlation_h2(kk)+correlation_h3(kk)+correlation_v2(kk)+correlation_v3(kk))/4;
waitbar(kk/iLength,h);
end
for (kk=1:iLength)
if (correlation(kk)> mean(correlation))
MessageVector(kk)=0;
end
end
close(h);
DDetect=reshape(255*MessageVector,Mw,Nw);
Detect = uint8(DDetect);
imwrite(Detect,'watermark jiancai.bmp','bmp');
PSNR = psnr(ddWatermark,DDetect);
elapsed_time=cputime-start_time
CN=corr2(ddWatermark,DDetect);
figure(1),imshow(iWatermark,[]);
title('Original Watermark');
figure(2),imshow(Detect,[])
title(strcat('Detected Watermark',',PSNR=',num2str(PSNR),'CN=',num2str(CN)));
%% END------------------------------------------------------------------%%
% Function:计算PSNR
function PSNR = psnr(ImageA,ImageB)
if (size(ImageA,1) = size(ImageB,1)) or (size(ImageA,2) = size(ImageB,2))
error('ImageA <> ImageB');
dPSNR = 0;
return ;
end
M = size(ImageA,1);
N = size(ImageA,2);

d = 0 ;
for i = 1:M
for j = 1:N
d = d + (ImageA(i,j) - ImageB(i,j)).^2 ;
end
end

dPSNR = -10*log10(d/(255*255*M*N)) ;
return