Please can anyone explain this code by Majid Farzaneh on Image watermarking (Transform domain) by DCT-SVD?

2 次查看(过去 30 天)
clc
close all
clear
%% Input images
I=imread('fruit.jpg');
I=imresize(I,[512,512]);
logo=randsrc(8,8,[0,1]);
figure
subplot(1,4,1)
imshow(I)
title('Host Image')
subplot(1,4,2)
B=I(:,:,3); % Blue Channel
imshow(logo)
xlabel('Logo (Watermark Image)')
gf=100; % Watermark Strength
n_dct=10; % <=64
zigzag=[1 9 2 3 10 17 25 18 11 4 5 12 19 26 33 41 34 27 20 13 6 7 14 21 28 35 42 49 57 ...
50 43 36 29 22 15 8 16 23 30 37 44 51 58 59 52 45 38 31 24 32 39 46 53 60 61 54 47 40 48 55 62 63 56 64];
dct_idx=zigzag(1:n_dct);
%% key generation:
key=randperm(512*512,64*64);
%% Block Selection (Embedding Step):
c=0;
Watermarked_B=B;
for i=1:64:64*64-63
c=c+1;
block_index=key(i:i+63);
blockB=double(reshape(B(block_index),[8,8]));
% discrete cosine transform:
dct_blockB=dct2(blockB);
% Singular Value Decomposition:
[U,S,V]=svd(dct_blockB(dct_idx));
sigmaB=S(1); % Biggest Singular Value
% embedding watermark image:
if logo(c)==1
S(1)=sigmaB+gf;
else
S(1)=sigmaB-gf;
end
reference{i}=S;
coeffs=U*S*V; % inverse svd
rec_dct_block=zeros(8);
rec_dct_block(dct_idx)=coeffs;
rec_block=idct2(rec_dct_block); % inverse dct
row_block=reshape(rec_block,1,64);
Watermarked_B(block_index)=row_block;
end
Watermarked_image=I;
Watermarked_image(:,:,3)=Watermarked_B;
subplot(1,4,3),
imwrite(uint8(Watermarked_image),'Watermark_jazmine_dct.jpg');
imshow(Watermarked_image);
title('Watermarked Image')
%% Extraction Step
Ex_watermark=zeros(8);
Watermarked_B=Watermarked_image(:,:,3);
c=0;
for i=1:64:64*64-63
c=c+1;
block_index=key(i:i+63);
blockB=double(reshape(B(block_index),[8,8]));
% discrete cosine transform:
dct_blockB=dct2(blockB);
% Singular Value Decomposition:
[Ub,Sb,Vb]=svd(dct_blockB(dct_idx));
Sbw=reference{i};
sigmaB=Sb(1); % Biggest Singular Value - Host
sigmaBW=Sbw(1); % Biggest Singular Value - Watermarked
% Extracting watermark image:
if sigmaBW>sigmaB
Ex_watermark(c)=1;
end
end
subplot(1,4,4),
imshow(Ex_watermark)
xlabel('Extracted Watermark');
%Bit_Error_Rate=sum(sum(Ex_watermark~=logo))/numel(logo)
%Structure_Similarity_Index=ssim(Watermarked_image,I)
%Peak_Signal_toNoise_Ratio=psnr(Watermarked_image,I)

回答(1 个)

Hari
Hari 2025-2-19
Hi Jabani,
The code you provided implements image watermarking using the Discrete Cosine Transform (DCT) and Singular Value Decomposition (SVD) techniques.
I assume you have the necessary images and MATLAB environment set up to run this code.
Input and Preprocessing:
The code reads a host image (fruit.jpg) and resizes it to 512x512 pixels. It also generates a random 8x8 binary logo to be used as a watermark. The blue channel of the host image is extracted for embedding the watermark.
Watermark Embedding:
A zigzag pattern is used to select DCT coefficients, and a key is generated for block selection. For each block, the DCT is applied, followed by SVD. The watermark is embedded by modifying the largest singular value based on the watermark bit (increasing or decreasing it by a strength factor gf).
Watermarked Image Construction:
The modified blocks are used to reconstruct the blue channel of the host image, resulting in the watermarked image. This image is then saved and displayed.
Watermark Extraction:
The extraction process involves applying the same DCT and SVD operations on the watermarked image blocks and comparing singular values to retrieve the watermark bits.
Display and Evaluation:
The code displays the original host image, the watermark, the watermarked image, and the extracted watermark. It also includes commented-out lines for calculating the Bit Error Rate (BER), Structure Similarity Index (SSIM), and Peak Signal-to-Noise Ratio (PSNR) for quality evaluation.
Refer to the documentation of the following functions to know more about their properties:
"imread": https://www.mathworks.com/help/matlab/ref/imread.html
"svd": https://www.mathworks.com/help/matlab/ref/svd.html
"imshow": https://www.mathworks.com/help/images/ref/imshow.html
Hope this helps!

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by