-
Notifications
You must be signed in to change notification settings - Fork 4
/
Main.m
260 lines (247 loc) · 12.6 KB
/
Main.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
close all; clc, clear variables
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Ver.1.0 2009, 1.2 2020, 1.2.x 2021, 2023 (see .MLX Example)
% Author: Nikola Jovanovic
% Repo: https://github.com/etfovac/watermark
% https://www.mathworks.com/matlabcentral/fileexchange/78790-digital-watermarking-comparison-of-dct-and-dwt-methods
% Use the simple Menu to control the program flow:
% Steps 1-3 are mandatory on init, steps 3-7 are repeatable, 8 - Exits.
%
% Note: Unmarked image has to be grayscale.
% If a color image is selected, it is coverted into a grayscale image.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
nfactor = 1; hdim_wmark = 1; wdim_wmark = 1; % set later to actual vals
K = 14; % "watermark strength" in [%] of coeffs intensity
% ie. percentage of transformation coefficients change
block_dim = 8; % DCT block size is 8x8
Level = 3; % Level of decomposition for Wavelet transformation
% Level = round(log10(block_dim)/log10(2))
step = 0;
the_end = 8;
while step ~= the_end
step = menu('Image Watermarking Procedure',...
'1. Read unmarked intensity image',...
'2. Read the watermark (binary image)',...
'3. Select transformation (DCT / DWT)',...
'4. Mark the image',...
'5. Attack/degrade the marked image',...
'6. Read a marked intensity image',...
'7. Detect and extract the watermark image',...
' Exit ');
% 1. Read unmarked intensity image ----------------------------
if step == 1
[file, path, index] = uigetfile(fullfile(pwd,'input','*.tif;*.png;*.jpg;*.jpeg'),'Input image file selection');
if and(length(file)~=1,length(path)~=1)
img_path = fullfile(path,file);
Unmarked_image = imread(img_path);
if length(size(Unmarked_image)) ~= 2
disp('Image dimensions have to be MxN pixels.');
Unmarked_image = rgb2gray(Unmarked_image);
% Enter intensity/grayscale image
end
figure('Name',strcat("Unmarked image: ",file)), imshow(Unmarked_image), title('Unmarked image')
[Image, Vs, Ss] = adj_image(Unmarked_image, block_dim);
nfactor = norm_factor(Image);
Image = double(Image)/nfactor;
% Watermark dimensions
hdim_wmark = Vs/block_dim;
wdim_wmark = Ss/block_dim;
hw_wmark = hdim_wmark * wdim_wmark;
end
end
% 2. Read watermark (binary image with pixels 0 or 1) ---------------------------------
if step == 2
[file, path, index] = uigetfile(fullfile(pwd,'input','*.jpeg;*.png;*.jpg;*.tif'),'Input watermark image file selection');
if and(length(file)~=1,length(path)~=1)
watermark_path = fullfile(path,file); %'input\\watermark.jpeg';
tmp_wmark = imread(watermark_path);
orig_wmark = zeros(size(tmp_wmark));
tmp_wmark = tmp_wmark/max(max(tmp_wmark));
above = find(tmp_wmark >= 0.5);
orig_wmark(above) = ones(size(above));
figure('Name',strcat("Original watermark: ",file)), imshow(orig_wmark), title('Original watermark')
watermark = adj_wmark(orig_wmark, hdim_wmark, wdim_wmark);
end
end
% 3. Select method (DCT or Wavelet) ---------------------------------
if step == 3
method = 'None';
m = 0;
while m ~= 3
m = menu('Select method',...
'DCT', 'Wavelet', 'OK');
if m == 1
method = 'DCT';
break
end
if m == 2
method = 'DWT';
break
end
end
fprintf('Method: %s\n', method);
end
% 4. Mark an image (incorporate the watermark) ----------------------
if step == 4
if evalin( 'base', 'exist(''hw_wmark'',''var'') == 1' )
% check if image file/var is loaded.
if evalin( 'base', 'exist(''watermark'',''var'') == 1' )
% check if watermark file/var is loaded.
if evalin( 'base', 'exist(''method'',''var'') == 1' )
% check if method is selected.
% Key for PSS generator
key = 1682004; %TODO: make it an input
% MATLAB's pseudorandom number generator (PRNG) is set to init state def by the key
rng(key);
% Permute the watermark
a1 = randperm(hw_wmark);
clear key; % delete key
a2 = reshape(a1, hdim_wmark, wdim_wmark);
scrambled_wmark = watermark(a2);
scrambled_wmark_norm = (scrambled_wmark - 0.5)/0.5; % % range [-1,1]
% scrambled_wmark_norm is type double
file_name = 'Marked_image';
if strcmp(method, 'DCT')
Marked_image = embed_DCT(Image, scrambled_wmark_norm, block_dim, K);
elseif strcmp(method, 'DWT')
Marked_image = embed_DWT(Image, scrambled_wmark_norm, Level, K);
else
disp('Not marked. Watermark method: None.')
Marked_image = Image;
end
default_file = fullfile(fullfile(fullfile(pwd,'output'),'marked'),strcat(file_name,'_', method, '.tif'));
[file, path, index] = uiputfile(('*.tif;*.png;*.jpg;*.jpeg'),'Marked image file Save As', default_file);
if and(length(file)~=1,length(path)~=1)
file_path = fullfile(path,file);
figure('Name',strcat("Marked image: ",file)), imshow(Marked_image), title('Marked image')
Marked_image_uint8 = uint8(Marked_image * nfactor);
imwrite(Marked_image_uint8, file_path);
end
else
disp('Method not selected.')
end
else
disp('Watermark file not loaded.')
end
else
disp('Image file not loaded.')
end
end
% 5. Attak/degrade the image --------------------------------------------------
% no breaks so that combinations are possible
if step == 5
if evalin( 'base', 'exist(''Marked_image'',''var'') == 1' )
% check if image file/var is loaded.
attack = 'Attack: None';
k = 0;
while k ~= 7
k = menu('Attak/degrade the image',...
'JPEG compression', 'Brightness',...
'Contrast','Cropping','Filtering',...
'Noise','OK');
if k == 1
output_folder = uigetdir(fullfile(pwd,'output'),'Output folder selection');
attack = compression(Marked_image_uint8, output_folder);
%generates images that start with: JPEG_Mkd_img_
end
if k == 2
output_folder = uigetdir(fullfile(pwd,'output'),'Output folder selection');
attack = brightness(Marked_image, output_folder, nfactor);
%generates images that start with: Bright_Mkd_img_
end
if k == 3
output_folder = uigetdir(fullfile(pwd,'output'),'Output folder selection');
attack = contrast(Marked_image, output_folder, nfactor);
%generates images that start with: Mcon_Mkd_img_
%generates images that start with: Hcon_Mkd_img_
end
if k == 4
output_folder = uigetdir(fullfile(pwd,'output'),'Output folder selection');
attack = cropping(Marked_image_uint8, output_folder);
%generates images that start with: Vcrop_Mkd_img_
%generates images that start with: VScrop_Mkd_img_
end
if k == 5
output_folder = uigetdir(fullfile(pwd,'output'),'Output folder selection');
attack = filtering(Marked_image_uint8, output_folder);
%generates images that start with: Filt_Mkd_img_
end
if k == 6
output_folder = uigetdir(fullfile(pwd,'output'),'Output folder selection');
attack = noise(Marked_image_uint8, output_folder);
%generates images that start with: Noise_Mkd_img_
end
end
disp(attack);
end
end
% 6. Read marked intensity/grayscale image --------------------------
if step == 6
[file, path, index] = uigetfile(fullfile(pwd,'output','*.tif;*.png;*.jpg;*.jpeg'),'Marked intensity/grayscale image selection');
if and(length(file)~=1,length(path)~=1)
full_path = fullfile(path,file);
disp(full_path);
Marked_image = imread(full_path);
if length(size(Marked_image)) ~= 2
disp('Image dimensions have to be MxN pixels.');
Marked_image = rgb2gray(Marked_image); % convert to grayscale
end
nfactor = norm_factor(Marked_image);
Marked_image = double(Marked_image)/nfactor;
% Dimensions of unmarked and marked image are the same.
figure('Name',strcat("Marked image (read): ",file)),imshow(Marked_image), title('Marked image (read)')
end
end
% 7. Detect watermark -----------------------
if step == 7
if evalin( 'base', 'exist(''hw_wmark'',''var'') == 1' )
% check if image file/var is loaded.
if evalin( 'base', 'exist(''watermark'',''var'') == 1' )
% check if watermark file/var is loaded.
if evalin( 'base', 'exist(''method'',''var'') == 1' )
% check if method is selected.
% Key for MATLAB's pseudorandom number generator (PRNG)
key = 1682004;
%key = input('\n Enter the password: ');
rng(key);
% Undo the permutation of the watermark.
b1 = randperm(hw_wmark);
clear key; % delete key
b2 = reshape(b1, hdim_wmark, wdim_wmark);
if strcmp(method, 'DCT')
recovered_watermark = extract_DCT(Image, Marked_image, b2, block_dim, hdim_wmark, wdim_wmark);
elseif strcmp(method, 'DWT')
recovered_watermark = extract_DWT(Image, Marked_image, b2, Level, nfactor, hdim_wmark, wdim_wmark);
else
disp('Not detected. Watermark method: None.')
Marked_image = Image;
recovered_watermark = watermark*0;
end
figure('Name',strcat("Detected watermark from: ",file)),imshow(recovered_watermark),title('Detected watermark')
CC_wmark = corr2(watermark, recovered_watermark);
fprintf('\n Correlation Coefficient of the watermarks %f', CC_wmark)
NC_wmark = sum(sum(recovered_watermark .* watermark))/sum(sum(watermark.^2));
fprintf('\n Normalized Correlation of the watermarks %f', NC_wmark)
CC_img = corr2(Image, Marked_image);
fprintf('\n Correlation Coefficient of the images %f', CC_img)
NC_img = sum(sum(Marked_image .* Image))/sum(sum(Image.^2));
fprintf('\n Normalized Correlation of the images %f \n', NC_img)
error = abs(recovered_watermark - watermark);
TotErrBits = sum(sum(error));
fprintf('\n Num of bit errors in detected watermark %i ', TotErrBits)
TotErrBitsPercent = (TotErrBits/hw_wmark)*100;
fprintf('\n BER [%%] for detected watermark %f \n', TotErrBitsPercent)
disp('*******************************************************')
else
disp('Method not selected.')
end
else
disp('Watermark file not loaded.')
end
else
disp('Image file not loaded.')
end
end
% THE END -----------------------------------------------------
end
clc