-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathdwt.hpp
239 lines (197 loc) · 5.57 KB
/
dwt.hpp
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
#pragma once
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <tuple>
/*!
* Performs Haar wavelet decomposition.
*
* \param src Source image.
* \param dst Destination image for the decomposition.
*
* \return Tuple with the horizontal, vertical and diagonal coefficients.
*/
inline std::tuple<std::vector<std::vector<float>>, std::vector<std::vector<float>>, std::vector<std::vector<float>>> cvHaarWavelet(const cv::Mat& src, cv::Mat& dst)
{
using namespace cv;
using namespace std;
auto width = src.cols / 2;
auto height = src.rows / 2;
vector<vector<float>> dhs(height);
vector<vector<float>> dvs(height);
vector<vector<float>> dds(height);
for (int y = 0; y < height; y++)
{
dhs[y] = vector<float>(width);
dvs[y] = vector<float>(width);
dds[y] = vector<float>(width);
for (int x = 0; x<(width);x++)
{
auto c = (src.at<float>(2 * y, 2 * x) + src.at<float>(2 * y, 2 * x + 1) + src.at<float>(2 * y + 1, 2 * x) + src.at<float>(2 * y + 1, 2 * x + 1)) * 0.5;
auto dh = (src.at<float>(2 * y, 2 * x) + src.at<float>(2 * y + 1, 2 * x) - src.at<float>(2 * y, 2 * x + 1) - src.at<float>(2 * y + 1, 2 * x + 1)) * 0.5;
auto dv = (src.at<float>(2 * y, 2 * x) + src.at<float>(2 * y, 2 * x + 1) - src.at<float>(2 * y + 1, 2 * x) - src.at<float>(2 * y + 1, 2 * x + 1)) * 0.5;
auto dd = (src.at<float>(2 * y, 2 * x) - src.at<float>(2 * y, 2 * x + 1) - src.at<float>(2 * y + 1, 2 * x) + src.at<float>(2 * y + 1, 2 * x + 1)) * 0.5;
dst.at<float>(y, x) = c;
dst.at<float>(y, x + width) = dh;
dst.at<float>(y + height, x) = dv;
dst.at<float>(y + height, x + width) = dd;
dhs[y][x] = dh;
dvs[y][x] = dv;
dds[y][x] = dd;
}
}
return make_tuple(dhs, dvs, dds);
}
/*!
* Performs Haar wavelet reconstruction.
*
* \param src Source image.
* \param dst Destination image for the reconstruction.
* \param dhs Horizontal coefficients.
* \param dvs Vertical coefficients.
* \param dds Diagonal coefficients.
*/
inline void cvInvHaarWavelet(const cv::Mat& src, cv::Mat& dst, const std::vector<std::vector<float>>& dhs, const std::vector<std::vector<float>>& dvs, const std::vector<std::vector<float>>& dds)
{
using namespace cv;
using namespace std;
auto width = src.cols / 2;
auto height = src.rows / 2;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
auto dh = dhs[y][x];
auto dv = dvs[y][x];
auto dd = dds[y][x];
auto c = src.at<float>(y, x);
dst.at<float>(y * 2, x * 2) = 0.5 * (c + dh + dv + dd);
dst.at<float>(y * 2, x * 2 + 1) = 0.5 * (c - dh + dv - dd);
dst.at<float>(y * 2 + 1, x * 2) = 0.5 * (c + dh - dv - dd);
dst.at<float>(y * 2 + 1, x * 2 + 1) = 0.5 * (c - dh - dv + dd);
}
}
}
/*!
* Uses discrete wavelet transformation to hide data in the diagonal filter of a channel of an image.
*
* \param img Input image.
* \param text Text to hide.
* \param mode Storage mode, see STORE_* constants.
* \param channel Channel to manipulate.
* \param alpha Encoding intensity.
*
* \return Altered image with hidden data.
*/
inline cv::Mat encode_dwt(const cv::Mat& img, const std::string& text, int mode = STORE_FULL, int channel = 0, float alpha = 0.1)
{
using namespace cv;
using namespace std;
auto i = 0;
auto size = text.length() * 8;
Mat imgfp;
img.convertTo(imgfp, CV_32F, 1.0 / 255);
vector<Mat> planes;
split(imgfp, planes);
for (int y = 0;y < img.cols; y++)
{
for (int x = 0; x < img.cols; x++)
{
auto val = planes[channel].at<float>(y, x);
if (val < alpha)
{
val = alpha;
}
else if (val > 1 - alpha)
{
val = 1 - alpha;
}
planes[channel].at<float>(y, x) = val;
}
}
Mat haar(img.rows, img.cols, CV_32FC1);
auto hwv = cvHaarWavelet(planes[channel], haar);
auto dds = get<2>(hwv);
for (int y = 0; y < dds.size(); y++)
{
for (int x = 0; x < dds[y].size(); x++)
{
if (i >= size)
{
if (mode == STORE_ONCE)
{
break;
}
else if (mode == STORE_REPEAT)
{
i = 0;
}
}
auto val = 0;
if (i < size)
{
val = (text[i / 8] & 1 << i % 8) >> i % 8;
i++;
}
if (val == 1)
{
dds[y][x] += alpha;
}
else
{
dds[y][x] -= alpha;
}
}
if (i >= size && mode == STORE_ONCE)
{
break;
}
}
cvInvHaarWavelet(haar, planes[channel], get<0>(hwv), get<1>(hwv), dds);
Mat mergedfp;
merge(planes, mergedfp);
Mat merged;
mergedfp.convertTo(merged, CV_8U, 255);
return merged;
}
/*!
* Uses discrete wavelet transformation to recover data hidden in the diagonal filter of an image.
*
* \param img Original image without hidden data.
* \param stego Altered image with hidden data.
* \param channel Channel to manipulate.
*
* \return Hidden data extracted form image.
*/
inline std::string decode_dwt(const cv::Mat& img, const cv::Mat& stego, int channel = 0)
{
using namespace cv;
using namespace std;
auto i = 0;
string bits((img.cols / 2) * (img.rows / 2) / 8, 0);
Mat imgfp;
img.convertTo(imgfp, CV_32F, 1.0 / 255);
Mat stegofp;
stego.convertTo(stegofp, CV_32F, 1.0 / 255);
vector<Mat> planes1;
split(imgfp, planes1);
vector<Mat> planes2;
split(stegofp, planes2);
Mat haar1(img.rows, img.cols, CV_32FC1);
Mat haar2(img.rows, img.cols, CV_32FC1);
auto dds1 = get<2>(cvHaarWavelet(planes1[channel], haar1));
auto dds2 = get<2>(cvHaarWavelet(planes2[channel], haar2));
for (int y = 0; y < dds1.size(); y++)
{
for (int x = 0; x < dds1[y].size(); x++)
{
auto val = dds2[y][x] - dds1[y][x];
if (val > 0)
{
bits[i / 8] |= 1 << i % 8;
}
i++;
}
}
return bits;
}