I am new in Matlab and I have an assignment that is asking for watermarking an image using DCT transform:
- 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.
Here are the two images:
I have three questions:
Here is what I tried:
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');
In this case, I found that using a Watermark_Strength
(strength factor) of 30 in this example shows more prominent results. A pipeline for adding a watermark is as follows:
• Zeropad the watermark image to match the size of the image to be watermarked using the padarray()
function or alternatively enlarge the image.
• Split the image and watermark image to their RGB channels/components.
• Take the Discrete Cosine Transform (DCT) of all the colour channels using the dct2()
function.
• Multiply the Discrete Cosine Transform (DCT) components of the watermarked image by a strength factor.
• Add the corresponding Discrete Cosine Transform (DCT) components based on colour channel.
• Take the inverse of 3 resultant Discrete Cosine Transform (DCT) components using the idct2()
function.
• Combine the inversed components to create the watermarked image in the spatial domain.
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);
Ran using MATLAB R2019b