-
Notifications
You must be signed in to change notification settings - Fork 22
/
retriv1.m
143 lines (118 loc) · 4.01 KB
/
retriv1.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
function [message_vector,Mo,No] = retriv1(watermrkd_img,message)
pn_sequence_search='T';
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 ];
watermarked_image= watermrkd_img;
% determine size of watermarked image
Mw=size(watermarked_image,1); %Height
Nw=size(watermarked_image,2); %Width
% read in original watermark
orig_watermark=double(message);
% determine size of original watermark
Mo=size(orig_watermark,1); %Height
No=size(orig_watermark,2); %Width
% 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
message_vector=ones(1,Mo*No);
[cA1,cH1,cV1,cD1] = dwt2(watermarked_image,'haar');
%................ perform DCT on cH1......................................
pn_sequence_one=round(2*(rand(1,sum(sum(midband)))-0.5));
pn_sequence_zero=round(2*(rand(1,sum(sum(midband)))-0.5));
% pn_sequence_one=round(2*(rand(blocksize,blocksize)-0.5));
% pn_sequence_zero=round(2*(rand(blocksize,blocksize)-0.5));
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)
sequence(ll)=dct_block(jj,ii);
ll=ll+1;
end
end
end
% end
% calculate the correlation of the middle band sequence with pn_sequences
% and choose the value with the higher correlation for message
cor_zero_h(kk)=corr2(pn_sequence_zero,sequence);
cor_one_h(kk)=corr2(pn_sequence_one,sequence);
% move on to next block. At and of row move to next row
if (x+blocksize) >= Nw/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)
% transform block using DCT
dct_block=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)
sequence(ll)=dct_block(jj,ii);
ll=ll+1;
end
end
end
% end
% calculate the correlation of the middle band sequence with pn_sequences
% and choose the value with the higher correlation for message
cor_zero_v(kk)=corr2(pn_sequence_zero,sequence);
cor_one_v(kk)=corr2(pn_sequence_one,sequence);
% move on to next block. At and of row move to next row
if (x+blocksize) >= Nw/2
x=1;
y=y+blocksize;
if y>=256
break
end
else
x=x+blocksize;
end
end
for kk = 1:length(message_vector)
correlation_one(kk)=(cor_one_h(kk)+cor_one_v(kk))/2;
correlation_zero(kk)=(cor_zero_h(kk)+cor_zero_v(kk))/2;
if correlation_zero(kk) > correlation_one(kk)
message_vector(kk)=0;
else
message_vector(kk)=1;
end
end
end