如何在频域中添加水印?
How to do watermarking in the Frequency Domain?
我是 Matlab 的新手,我有一个作业要求使用 DCT 变换为图像加水印:
- Read Lin.jpg color image and apply DCT.
- Threshold the logo.jpg (watermark) into binary and ten times of its strength, and then add it to the coefficient of transformed Lin image.
这是两张图片:
我有三个问题:
- 我应该把 Lin.jpg 分成 8x8 块和 logo.jpg 分成 2x2 块还是没必要?
- “强度的十倍”是什么意思?这只是乘以 10 吗?
- 如何得到变换后的Lin.jpg图像的系数?
这是我尝试过的:
img = imread('Lin.jpg');
wImg = imread('njit_logo.jpg');
wImgBinary = imbinarize(wImg) * 10;
[rows, cols] = size(img(:,:,1));
[Wrows, Wcols] = size(wImgBinary);
% make the watermark image as large as the original
watermark = zeros(size(img), 'uint8');
for column = 1:cols
for row = 1:rows
watermark(row, column) = wImgBinary(mod(row,Wrows)+1, mod(column,Wcols)+1);
end
end
watermark = watermark(1:rows, 1:cols);
% apply dct and add with watermark at each channel
for i = 1:3
imgDct = dct2(img(:,:,i));
C = imgDct + double(watermark);
Iw(:,:,i) = round(real(idct2(C)));
end
IIw = uint8(Iw);
figure, imshow(IIw), title('watermarked image');
通过组合离散余弦变换分量对图像加水印
在这种情况下,我发现在此示例中使用 30 的 Watermark_Strength
(强度因子)会显示更突出的结果。添加水印的流水线如下:
• 使用 padarray()
函数对水印图像进行零填充以匹配要添加水印的图像的大小,或者放大图像。
• 将图像和水印图像拆分为它们的RGB channels/components。
• 使用dct2()
函数对所有颜色通道进行离散余弦变换(DCT)。
• 将带水印图像的离散余弦变换(DCT) 分量乘以强度因子。
• 基于颜色通道添加相应的离散余弦变换(DCT) 组件。
• 使用idct2()
函数对3 个合成的离散余弦变换(DCT) 分量取反。
• 组合逆向分量以在空间域中创建带水印的图像。
Image = imread('Lin.jpg');
Watermark = imread('njit_logo.jpg');
%Grabbing the image and watermark dimensions%
[Image_Height,Image_Width,~] = size(Image);
[Watermark_Height,Watermark_Width,~] = size(Watermark);
%Padding the watermark to match the size of the image to be watermarked%
Side_Padding = (Image_Width - Watermark_Width)/2;
Top_And_Bottom_Padding = (Image_Height - Watermark_Height)/2;
Watermark_Padded = padarray(Watermark,[Top_And_Bottom_Padding Side_Padding],0,'both');
%Binary image of watermark%
Watermark_Binary = imbinarize(Watermark_Padded);
%Converting the watermark image to frequency domain using DCT%
Watermark_Strength = 30;
Red_Channel_Watermark_DCT = Watermark_Strength*dct2(Watermark_Binary(:,:,1));
Blue_Channel_Watermark_DCT = Watermark_Strength*dct2(Watermark_Binary(:,:,2));
Green_Channel_Watermark_DCT = Watermark_Strength*dct2(Watermark_Binary(:,:,3));
%Converting the image to frequency domain using DCT%
Red_Channel_Image_DCT = dct2(Image(:,:,1));
Blue_Channel_Image_DCT = dct2(Image(:,:,2));
Green_Channel_Image_DCT = dct2(Image(:,:,3));
%Adding the frequency components together%
Combined_Red_Channel = Red_Channel_Watermark_DCT + Red_Channel_Image_DCT;
Combined_Blue_Channel = Blue_Channel_Watermark_DCT + Blue_Channel_Image_DCT;
Combined_Green_Channel = Green_Channel_Watermark_DCT + Green_Channel_Image_DCT;
%Inversing the combined frequency domain image%
Combined_Image(:,:,1) = idct2(real(Combined_Red_Channel));
Combined_Image(:,:,2) = idct2(real(Combined_Blue_Channel));
Combined_Image(:,:,3) = idct2(real(Combined_Green_Channel));
%Displaying combined image%
Combined_Image = uint8(Combined_Image);
imshow(Combined_Image);
运行 使用 MATLAB R2019b
我是 Matlab 的新手,我有一个作业要求使用 DCT 变换为图像加水印:
- Read Lin.jpg color image and apply DCT.
- Threshold the logo.jpg (watermark) into binary and ten times of its strength, and then add it to the coefficient of transformed Lin image.
这是两张图片:
我有三个问题:
- 我应该把 Lin.jpg 分成 8x8 块和 logo.jpg 分成 2x2 块还是没必要?
- “强度的十倍”是什么意思?这只是乘以 10 吗?
- 如何得到变换后的Lin.jpg图像的系数?
这是我尝试过的:
img = imread('Lin.jpg');
wImg = imread('njit_logo.jpg');
wImgBinary = imbinarize(wImg) * 10;
[rows, cols] = size(img(:,:,1));
[Wrows, Wcols] = size(wImgBinary);
% make the watermark image as large as the original
watermark = zeros(size(img), 'uint8');
for column = 1:cols
for row = 1:rows
watermark(row, column) = wImgBinary(mod(row,Wrows)+1, mod(column,Wcols)+1);
end
end
watermark = watermark(1:rows, 1:cols);
% apply dct and add with watermark at each channel
for i = 1:3
imgDct = dct2(img(:,:,i));
C = imgDct + double(watermark);
Iw(:,:,i) = round(real(idct2(C)));
end
IIw = uint8(Iw);
figure, imshow(IIw), title('watermarked image');
通过组合离散余弦变换分量对图像加水印
在这种情况下,我发现在此示例中使用 30 的 Watermark_Strength
(强度因子)会显示更突出的结果。添加水印的流水线如下:
• 使用 padarray()
函数对水印图像进行零填充以匹配要添加水印的图像的大小,或者放大图像。
• 将图像和水印图像拆分为它们的RGB channels/components。
• 使用dct2()
函数对所有颜色通道进行离散余弦变换(DCT)。
• 将带水印图像的离散余弦变换(DCT) 分量乘以强度因子。
• 基于颜色通道添加相应的离散余弦变换(DCT) 组件。
• 使用idct2()
函数对3 个合成的离散余弦变换(DCT) 分量取反。
• 组合逆向分量以在空间域中创建带水印的图像。
Image = imread('Lin.jpg');
Watermark = imread('njit_logo.jpg');
%Grabbing the image and watermark dimensions%
[Image_Height,Image_Width,~] = size(Image);
[Watermark_Height,Watermark_Width,~] = size(Watermark);
%Padding the watermark to match the size of the image to be watermarked%
Side_Padding = (Image_Width - Watermark_Width)/2;
Top_And_Bottom_Padding = (Image_Height - Watermark_Height)/2;
Watermark_Padded = padarray(Watermark,[Top_And_Bottom_Padding Side_Padding],0,'both');
%Binary image of watermark%
Watermark_Binary = imbinarize(Watermark_Padded);
%Converting the watermark image to frequency domain using DCT%
Watermark_Strength = 30;
Red_Channel_Watermark_DCT = Watermark_Strength*dct2(Watermark_Binary(:,:,1));
Blue_Channel_Watermark_DCT = Watermark_Strength*dct2(Watermark_Binary(:,:,2));
Green_Channel_Watermark_DCT = Watermark_Strength*dct2(Watermark_Binary(:,:,3));
%Converting the image to frequency domain using DCT%
Red_Channel_Image_DCT = dct2(Image(:,:,1));
Blue_Channel_Image_DCT = dct2(Image(:,:,2));
Green_Channel_Image_DCT = dct2(Image(:,:,3));
%Adding the frequency components together%
Combined_Red_Channel = Red_Channel_Watermark_DCT + Red_Channel_Image_DCT;
Combined_Blue_Channel = Blue_Channel_Watermark_DCT + Blue_Channel_Image_DCT;
Combined_Green_Channel = Green_Channel_Watermark_DCT + Green_Channel_Image_DCT;
%Inversing the combined frequency domain image%
Combined_Image(:,:,1) = idct2(real(Combined_Red_Channel));
Combined_Image(:,:,2) = idct2(real(Combined_Blue_Channel));
Combined_Image(:,:,3) = idct2(real(Combined_Green_Channel));
%Displaying combined image%
Combined_Image = uint8(Combined_Image);
imshow(Combined_Image);
运行 使用 MATLAB R2019b