代码
clear;clc
size=512;
block=8;
blockno=size/block;
LENGTH=size*size/64;
Alpha1=0.02; Alpha2=0.02;
T1=100;
I=zeros(size,size);
D=zeros(size,size);
BW=zeros(size,size);
block_dct1=zeros(block,block);
i=imread('watermark64by64.png'); %水印原图
mark=reshape(i,1,LENGTH);
figure;imshow(i);title('水印');
%subplot(3,2,1);plot(mark);title('水印');
%显示原图
figure;I=imread('lena512512.jpg'); %需要加水印的原图
I0=I;
imshow(I);title('原始图像');
% BW=edge(I,'canny');figure;;imshow(BW);
% title('edge of origine image');
%嵌入水印
k=1;
for m=1:blockno
for n=1:blockno
x=(m-1)*block+1; y=(n-1)*block+1;
block_dct1=I(x:x+block-1,y:y+block-1);
block_dct1=dct2(block_dct1);
BW_8_8=BW(x:x+block-1,y:y+block-1);
if m<=1|n<=1
T=0;
else
T=sum(BW_8_8); T=sum(T);
end
if T>T1
Alpha=Alpha2;
else
Alpha=Alpha1;
end
block_dct1(1,1)=block_dct1(1,1)*(1+Alpha*mark(k));
block_dct1=idct2(block_dct1);
D(x:x+block-1,y:y+block-1)=block_dct1;
k=k+1;
end
end
figure;imshow(D,[]);title('嵌入水印后的图像');
%提取水印
k=1;
mark1=[];
for m=1:blockno
for n=1:blockno
x=(m-1)*block+1; y=(n-1)*block+1;
block_dct1=D(x:x+block-1,y:y+block-1);
block_dct0=I0(x:x+block-1,y:y+block-1);
block_dct1=dct2(block_dct1);
block_dct0=dct2(block_dct0);
BW_8_8=BW(x:x+block-1,y:y+block-1);
if m<=1|n<=1
T=0;
else
T=sum(BW_8_8); T=sum(T);
end
if T>T1
Alpha=Alpha2;
else
Alpha=Alpha1;
end
mark1(k)=(block_dct1(1,1)/block_dct0(1,1)-1)/Alpha;
k=k+1;
end
end
mark1=reshape(mark1,64,64);
figure
imshow(mark1)
title('提取的水印')
运行结果