clc;
clear all;
close all;
N=64; %水印长度
image1=imread('pic.jpg');
subplot(2,2,1);imshow(image1); title('原图像');
image1=double(image1);
%水印图像
imagew=imread('watermark1.jpg');
thresh = graythresh(imagew); %自动确定二值化阈值
imagew=im2bw(imagew,0.5);
subplot(2,2,2);imshow(imagew);title('水印图像');
%嵌入水印
%小波变换
[ca,ch,cv,cd] = dwt2(image1,'db1');
[cas,chs,cvs,cds] = dwt2(ca,'db1');
for i=1:N
for j=1:N
if imagew(i,j)==0
a=-1;
else
a=1;
end
Ca(i,j)=cas(i,j)*(1+a*0.03);
end
end
%小波逆变换
IM= idwt2(Ca,chs,cvs,cds,'db1') ;
markedimage=double(idwt2(IM,ch,cv,cd,'db1'));
%显示嵌入后水印图像
subplot(2,2,3);imshow(markedimage,[]);title('marked image');
imwrite(markedimage,gray(256),'pic_加水印后的图像.bmp','bmp');
%%
%提取水印
image1=double(image1);
imaged=markedimage;
[ca,ch,cv,cd] = dwt2(image1,'db1'); %小波变换
[cas,chs,cvs,cds]=dwt2(ca,'db1');
[caa,chh,cvv,cdd]=dwt2(imaged,'db1');
[caas,chhs,cvvs,cdds]=dwt2(caa,'db1');
for p=1:N
for q=1:N
a=caas(p,q)/cas(p,q)-1;
if a<0
W(p,q)=0;
else
W(p,q)=255;
end
end
end
%显示提取的水印
subplot(2,2,4);imshow(W,[]);title('从含水印图像中提取的水印');
imwrite(W,'watermark_从含水印图像中提取的水印.bmp');