1、function dwtgl clear all clc; %保存开始时间 start_time=cputime; figure(1); %读出原始图像 subplot(2,2,1); input=imread(image.bmp); imshow(input); title(原始图像); %读出水印 subplot(2,2,2); water=imread(watermark.bmp); imshow(water); title(水印); %三色分离 input=double(input); water=double(water); inputr=input(:,:,1); waterr=w
2、ater(:,:,1); inputg=input(:,:,2); waterg=water(:,:,2); inputb=double(input(:,:,3); waterb=double(water(:,:,3); %系数 r 大,增加鲁棒性,r 小增加透明性 r=0.04; %水印 R 的分解 Cwr,Swr=wavedec2(waterr,1,haar); %图像 R 的分解 Cr,Sr=wavedec2(inputr,2,haar); %水印的嵌入 Cr(1:size(Cwr,2)/16)=. Cr(1:size(Cwr,2)/16)+r*Cwr(1:size(Cwr,2)/16)
3、; k=0; while k=size(Cr,2)/size(Cwr,2)-1 Cr(1+size(Cr,2)/4+k*size(Cwr,2)/4:size(Cr,2)/4+. (k+1)*size(Cwr,2)/4)=Cr(1+size(Cr,2)/4+. k*size(Cwr,2)/4:size(Cr,2)/4+(k+1)*size(Cwr,2)/4)+. r*Cwr(1+size(Cwr,2)/4:size(Cwr,2)/2); Cr(1+size(Cr,2)/2+k*size(Cwr,2)/4:size(Cr,2)/2+. (k+1)*size(Cwr,2)/4)=Cr(1+size(
4、Cr,2)/2+. k*size(Cwr,2)/4:size(Cr,2)/2+(k+1)*size(Cwr,2)/4)+. r*Cwr(1+size(Cwr,2)/2:3*size(Cwr,2)/4); Cr(1+3*size(Cwr,2)/4+k*size(Cwr,2)/4:3*size(Cwr,2)/4+. (k+1)*size(Cwr,2)/4)=Cr(1+3*size(Cr,2)/4+. k*size(Cwr,2)/4:3*size(Cr,2)/4+(k+1)*size(Cwr,2)/4)+. r*Cwr(1+3*size(Cwr,2)/4:size(Cwr,2); k=k+1; en
5、d; Cr(1:size(Cwr,2)/4)=Cr(1:size(Cwr,2)/4)+r*Cwr(1:size(Cwr,2)/4); g=0.02; %水印 G 的分解 Cwg,Swg=WAVEDEC2(waterg,1,haar); %图像 G 的分解 Cg,Sg=WAVEDEC2(inputg,2,haar); %水印的嵌入 Cg(1:size(Cwg,2)/16)=. Cg(1:size(Cwg,2)/16)+g*Cwg(1:size(Cwg,2)/16); k=0; while k=size(Cg,2)/size(Cwg,2)-1 Cg(1+size(Cg,2)/4+k*size(Cw
6、g,2)/4:size(Cg,2)/4+. (k+1)*size(Cwg,2)/4)=Cg(1+size(Cg,2)/4+. k*size(Cwg,2)/4:size(Cg,2)/4+(k+1)*size(Cwg,2)/4)+. g*Cwg(1+size(Cwg,2)/4:size(Cwg,2)/2); Cg(1+size(Cg,2)/2+k*size(Cwg,2)/4:size(Cg,2)/2+. (k+1)*size(Cwg,2)/4)=Cg(1+size(Cg,2)/2+. k*size(Cwg,2)/4:size(Cg,2)/2+(k+1)*size(Cwg,2)/4)+. g*Cwg
7、(1+size(Cwg,2)/2:3*size(Cwg,2)/4); Cg(1+3*size(Cg,2)/4+k*size(Cwg,2)/4:3*size(Cg,2)/4+. (k+1)*size(Cwg,2)/4)=Cg(1+3*size(Cg,2)/4+. k*size(Cwg,2)/4:3*size(Cg,2)/4+(k+1)*size(Cwg,2)/4)+. g*Cwg(1+3*size(Cwg,2)/4:size(Cwg,2); k=k+1; end; Cg(1:size(Cwg,2)/4)=Cg(1:size(Cwg,2)/4)+g*Cwg(1:size(Cwg,2)/4); b=
8、0.16; %水印 B 的分解 Cwb,Swb=WAVEDEC2(waterb,1,haar); %图像 B 的分解 Cb,Sb=WAVEDEC2(inputb,2,haar); %水印的嵌入 Cb(1:size(Cwb,2)/16)+b*Cwb(1:size(Cwb,2)/16); k=0; while k=size(Cb,2)/size(Cwb,2)-1 Cb(1+size(Cb,2)/4+k*size(Cwb,2)/4:size(Cb,2)/4+. (k+1)*size(Cwb,2)/4)=Cb(1+size(Cb,2)/4+. k*size(Cwb,2)/4:size(Cb,2)/4+
9、(k+1)*size(Cwb,2)/4)+. g*Cwb(1+size(Cwb,2)/4:size(Cwb,2)/2); Cb(1+size(Cb,2)/2+k*size(Cwb,2)/4:size(Cb,2)/2+. (k+1)*size(Cwb,2)/4)=Cb(1+size(Cb,2)/2+. k*size(Cwb,2)/4:size(Cb,2)/2+(k+1)*size(Cwb,2)/4)+. b*Cwb(1+size(Cwb,2)/2:3*size(Cwb,2)/4); Cb(1+3*size(Cb,2)/4+k*size(Cwb,2)/4:3*size(Cb,2)/4+. (k+1
10、)*size(Cwb,2)/4)=Cb(1+3*size(Cb,2)/4+. k*size(Cwb,2)/4:3*size(Cb,2)/4+(k+1)*size(Cwb,2)/4)+. b*Cwb(1+3*size(Cwb,2)/4:size(Cwb,2); k=k+1; end; Cb(1:size(Cwb,2)/4)=Cb(1:size(Cwb,2)/4)+b*Cwb(1:size(Cwb,2)/4); %图像的重构 inputr=WAVEREC2(Cr,Sr,haar); inputg=WAVEREC2(Cg,Sg,haar); inputb=WAVEREC2(Cb,Sb,haar);
11、%三色的叠加 temp=size(inputr); pic=zeros(temp(1),temp(2),3); for i=1:temp(1); for j=1:temp(2); pic(i,j,1)=inputr(i,j); pic(i,j,2)=inputg(i,j); pic(i,j,3)=inputb(i,j); end end %转化为 uint8 output=uint8(round(pic); imwrite(output,watermarked.bmp,bmp); %显示时间 elapsed_time=cputime-start_time, %输出结果 %subplot(2,2,3); figure(3); imshow(output); title(水印图像);