dct水印嵌入算法的困惑
嵌入算法:function = 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')
为什么提取不出水印?请教高手我算法的问题出在哪里,非常感谢。
少了一个函数,现在补上,哪位高手能帮我调试一下
%FindNLargest.mfunction INDEX = FindNLargest(X, N)
%FINDNLARGEST Find largest values in a matrix or a vector
% INDEX = FINDNLARGEST(X, N) find the N largest values of X
% and returns their indices in X.
% This is a modified version of Fabien Petitcolas' implementation
% of Cox's technique by Gabriela Delfino and Fabian Martinez
%
% Fabien Petitcolas' website: http://www.cl.cam.ac.uk/~fapp2
% Put all the elements of the matrix in a vector
n=size(X);
% We first fix the size of the matrix to make
% the process faster
tmp=zeros(1,n(1)*n(2));
for i=1:n(1)
for j=1:n(2)
tmp(j+(i-1)*n(2)) = X(i,j);
end
end
% The DC coefficient must not be considered into the N largest because % it must not be changed, so we set its value to 0.
tmp(1,1)=0;
% Sort the element of the vector
= sort(tmp);
l = length(index);
% Take the N largest
for k=1:N
j = mod(index(l+1-k),n(2));
if j==0
j = n(2);
end
i = floor((index(l+1-k) - 1) / n(2)) + 1;
INDEX(1,k) = i;
INDEX(2,k) = j;
end
页:
[1]