该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
%水印嵌入代码
M = 256;%原始图像长度
N = 32; %水印图像长度
K = 8;
I = zeros(M, M);
J = zeros(N, N);
BLOCK = zeros(K, K);
%显示原始图像
subplot(221);
I = imread('C:\Users\Rocky\Documents\MATLAB\cameraman.bmp');
imshow(I);
title('原始图像');
%显示水印图像
subplot(222);
J = imread('C:\Users\Rocky\Documents\MATLAB\matlab.bmp');
imshow(J);
title('水印图像');
%嵌入水印
temp = 1;
for p = 1 : N;
for q = 1 : N;
x = (p - 1) * K + 1;
y = (q - 1) * K + 1;
BLOCK = I(x: x + K - 1, y: y + K - 1);
BLOCK = dct2(BLOCK);
if J(p, q) == 0;
a = -1;
else
a = 1;
end
BLOCK(2, 1) = BLOCK(2, 1) * (1 + a * 0.01);
BLOCK = idct2(BLOCK);
I(x: x + K - 1,y: y + K - 1) = BLOCK;
end
end
%显示嵌入水印后的图像
subplot(223);
imshow(I);
title('嵌入水印后的图像');
imwrite(I,'watermarked.tif','tif');
%从嵌入水印的图像中提取水印
I = imread('cameraman.tif');
J = imread('watermarked.tif');
for p = 1 : N;
for q = 1 : N;
x = (p - 1) * K + 1;
y = (q - 1) * K + 1;
BLOCK1 = I(x: x + K - 1,y: y + K - 1);
BLOCK2 = J(x: x + K - 1,y: y + K - 1);
BLOCK1 = idct2(BLOCK1);
BLOCK2 = idct2(BLOCK2);
a = BLOCK2(1,1) / BLOCK1(1, 1) - 1;
if a < 0
W(p, q) = 0;
else
W(p, q) = 1;
end
end
end
%显示提取的水印
subplot(224);
imshow(W);
title('从含水印图像中提取水印');