马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?我要加入
x
嵌入算法:
function [J, W,Index,DCTI] = CoxWmk111(I, alpha, N, W)
imagew=imread('dmg2.tif');%读取水印图像 dmg2.tif'为一个二值图像
imagew=double(imagew)./256;
s=size(imagew);
W1=round(reshape(imagew,1,s(1)*s(2)));
if (nargin == 1)
N = s(1)*s(2);
% alpha = 0.1;
alpha = 0.01;
% We modify the value of alpha to achieve better visual quality
W = W1;
end
if (nargin == 2)
N = s(1)*s(2);
W = W1;
end
if (nargin == 3)
W = W1;
end
I=imread(I);
subplot(2,2,1);
imshow(I);
title('载体图像');
sI = size(I);
if ((sI(1) * sI(2)) < N)
error('Image too small or too many coefficients.');
end
if (isrgb(I))%载体图像转化为灰度图像
I=rgb2gray(I);
end
% Compute the DCT of the image
DCTI = dct2(I);
% Find the N largest coefficients in the DCT matrix
% Better if the extraction of the N largest was done
% at the same time than the computation of the DCT...
Index = FindNLargest(abs(DCTI), N);
% Modify these coefficients
for i = 1:N
DCTI(Index(1,i),Index(2,i)) = DCTI(Index(1,i),Index(2,i)) * (1 + alpha * W(i));
end
% Simply take the inverse DCT of the modifyied matrix
% J = idct2(DCTI);
IconMarca = idct2(DCTI);
% We save the inverse DCT results in an auxiliar matrix
% We change the values above 255 to 255 in order to avoid posible
% distortions caused by the double representation of MatLab
J=IconMarca;
%J=abs(J);
J=uint8(J);
subplot(2,2,2);
imshow(J,[])
title('嵌入后图像');
imwrite(J,'watermark.jpg')
提取算法:
%CoxExtract.m
function X= CoxExtract111(I, J, alpha, N)
imagew=imread('dmg2.tif');%读取水印图像目的只是为了求维数,由提取出的一维水印变化到二维,
s=size(imagew);
if (nargin == 2)
N = s(1)*s(2);
alpha = 0.01;
end
if (nargin == 3)
N = s(1)*s(2);
end
I=imread(I);
J=imread(J);
subplot(1,2,1);imshow(I);title('原始图像');
if (isrgb(I))
I=rgb2gray(I);
end
sI = size(I);
if ((sI(1) * sI(2)) < N)
error('Image too small or too many coefficients');
end
sJ = size(J);
if ((sI(1) ~= sJ(1)) || (sI(2) ~= sJ(2)))
error('The images have different size.');
end
% Compute the DCT of the images
DCTI = dct2(I);
DCTJ = dct2(double(J));
subplot(1,2,2);imshow(J);title('水印图像');
% Find the N largest coefficients in the DCT matrix
Index = FindNLargest(abs(DCTI), N);
% Extract the watermark
for i = 1:N
X(i) = (DCTJ(Index(1, i), Index(2, i)) / DCTI(Index(1, i), Index(2, i)) - 1) / alpha;
end
message=reshape(X,s(1),s(2)).*256;
figure;
imshow(message);
title('Recovered Message')
为什么提取不出水印?请教高手我算法的问题出在哪里,非常感谢。 |