-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathdwtdct.m
221 lines (192 loc) · 6.43 KB
/
dwtdct.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
function [watermrkd_img,recmessage,PSNR,IF,NCC1] = dwtdct(cover_object,message,k)
h=msgbox('Processing');
pn_sequence_search='T';
% k=10;
% k=input('Enter the value:');
blocksize=8;
midband=[ 0,0,0,1,1,1,1,0; % defines the mid-band frequencies of an 8x8 dct
0,0,1,1,1,1,0,0;
0,1,1,1,1,0,0,0;
1,1,1,1,0,0,0,0;
1,1,1,0,0,0,0,0;
1,1,0,0,0,0,0,0;
1,0,0,0,0,0,0,0;
0,0,0,0,0,0,0,0 ];
% % read in the cover object
% file_name='low_key.jpg';
% cover_object=double(imread(file_name));
% determine size of watermarked image
Mc=size(cover_object,1); %Height
Nc=size(cover_object,2); %Width
max_message=Mc*Nc/(blocksize^2);
message1=message;
% % read in the message image and reshape it into a vector
% file_name='_copyright_small.bmp';
% message1=(imread(file_name));
if (length(message1) > max_message)
error('Message too large to fit in Cover Object')
end
Mm=size(message1,1); %Height
Nm=size(message1,2); %Width
message=round(reshape(message1,Mm*Nm,1)./256);
message_vector=ones(1,max_message);
message_vector(1:length(message))=message;
% read in key for PN generator
file_name='_key.bmp';
key=double(imread(file_name))./256;
% reset MATLAB's PN generator to state "key"
j = 1;
for i =1:length(key)
rand('state',key(i,j));
end
[cA1,cH1,cV1,cD1] = dwt2(cover_object,'haar');
%................ perform DCT on cH1......................................
% generate PN sequences for "1" and "0"
pn_sequence_one=round(2*(rand(1,sum(sum(midband)))-0.5));
pn_sequence_zero=round(2*(rand(1,sum(sum(midband)))-0.5));
% find two highly un-correlated PN sequences
if (pn_sequence_search=='T')
while (corr2(pn_sequence_one,pn_sequence_zero) > -0.55)
pn_sequence_one=round(2*(rand(1,sum(sum(midband)))-0.5));
pn_sequence_zero=round(2*(rand(1,sum(sum(midband)))-0.5));
end
end
% process the image in blocks
x=1;
y=1;
for kk = 1:length(message_vector)
% transform block using DCT
dct_block=dct2(cH1(y:y+blocksize-1,x:x+blocksize-1));
% if message bit contains zero then embed pn_sequence_zero into the mid-band
% componants of the dct_block
ll=1;
if (message_vector(kk)==0)
for ii=1:blocksize
for jj=1:blocksize
if (midband(jj,ii)==1)
dct_block(jj,ii)=dct_block(jj,ii)+k*pn_sequence_zero(ll);
ll=ll+1;
end
end
end
% otherwise, embed pn_sequence_one into the mid-band componants of dct_block
else
for ii=1:blocksize
for jj=1:blocksize
if (midband(jj,ii)==1)
dct_block(jj,ii)=dct_block(jj,ii)+k*pn_sequence_one(ll);
ll=ll+1;
end
end
end
end
% transform block back into spatial domain
cH1(y:y+blocksize-1,x:x+blocksize-1)=idct2(dct_block);
% move on to next block. At and of row move to next row
if (x+blocksize) >= Nc/2
x=1;
y=y+blocksize;
if y>=256
break
end
else
x=x+blocksize;
end
end
x=1;
y=1;
for kk = 1:length(message_vector)
% ................ perform DCT on cV1......................................
% transform block using DCT
dct_block_1=dct2(cV1(y:y+blocksize-1,x:x+blocksize-1));
% if message bit contains zero then embed pn_sequence_zero into the mid-band
% componants of the dct_block
ll=1;
if (message_vector(kk)==0)
for ii=1:blocksize
for jj=1:blocksize
if (midband(jj,ii)==1)
dct_block_1(jj,ii)=dct_block_1(jj,ii)+k*pn_sequence_zero(ll);
ll=ll+1;
end
end
end
% otherwise, embed pn_sequence_one into the mid-band componants of dct_block
else
for ii=1:blocksize
for jj=1:blocksize
if (midband(jj,ii)==1)
dct_block_1(jj,ii)=dct_block_1(jj,ii)+k*pn_sequence_one(ll);
ll=ll+1;
end
end
end
end
% transform block back into spatial domain
cV1(y:y+blocksize-1,x:x+blocksize-1)=idct2(dct_block_1);
% move on to next block. At and of row move to next row
if (x+blocksize) >= Nc/2
x=1;
y=y+blocksize;
if y>=256
break
end
else
x=x+blocksize;
end
end
watermarked_image = idwt2(cA1,cH1,cV1,cD1,'haar',[Mc,Nc]);
% convert back to uint8
watermarked_image_uint8=uint8(watermarked_image);
% % calculate the PSNR
I0 = double(cover_object);
I1 = double(watermarked_image_uint8);
Id = (I0-I1);
signal = sum(sum(I0.^2));
noise = sum(sum(Id.^2));
MSE = noise./numel(I0);
peak = max(I0(:));
PSNR = 10*log10(peak^2/MSE(:,:,1))
% Image Fiedilty
IF = imfed(I0,Id);
IF = mean(IF);
watermrkd_img=watermarked_image_uint8;
imshow(watermrkd_img)
% %% NOise addition
% sel=input('Do u want to add noise:Enter 1 for Yes');
% if sel==1
% J = imnoise(watermrkd_img,'gaussian',0.05);
% J1 = imnoise(watermrkd_img,'poisson');
% J2 = imnoise(watermrkd_img,'salt & pepper');
% J3 = imnoise(watermrkd_img,'speckle');
% imwrite(J,'gaussian_noise_watermarked43.bmp','bmp');
% figure
% imshow('gaussian_noise_watermarked43.bmp')
% title('guassian noise')
% watermrkd_img=J;
% else
% display('---------------No Noise Added--------------')
% end
%% retrieval
[message_vector1,Mo,No] = retriv1(watermrkd_img,message1);
recmessage=reshape(message_vector1,Mo,No);
% figure
% imshow(recmessage,[])
% title('Recovered Watermark')
NCC1=ncc(double(message1),recmessage);
% watermrkd_img=J1;
% [message_vector1,Mo,No] = retriv1(watermrkd_img,message1);
% recmessage=reshape(message_vector1,Mo,No);
% NCC2=ncc(double(message1),recmessage);
%
% watermrkd_img=J2;
% [message_vector1,Mo,No] = retriv1(watermrkd_img,message1);
% recmessage=reshape(message_vector1,Mo,No);
% NCC3=ncc(double(message1),recmessage);
%
% watermrkd_img=J3;
% [message_vector1,Mo,No] = retriv1(watermrkd_img,message1);
% recmessage=reshape(message_vector1,Mo,No);
% NCC4=ncc(double(message1),recmessage);
close(h)
end