-
Notifications
You must be signed in to change notification settings - Fork 0
/
haar.cpp
399 lines (312 loc) · 7.88 KB
/
haar.cpp
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
// Targeter - target identification software for EUCALL workpackage 6
// Licensed under the GPL License. See LICENSE file in the project root for full license information.
// Copyright(C) 2017 David Watts
#include "FocusStack.h"
#include <cmath>
#include <QObject>
#include "opencv2/opencv.hpp"
#include "opencv/highgui.h"
#include "haar.h"
double getEnergy(double dVal, double divisor, bool bSquare)
{
if (bSquare)
return (dVal*dVal) / divisor;
else
return fabs(dVal) / divisor;
}
/**
*
* Get energy image (summed coefficient magnitude) from haar wavelet pyramid
*
* @author David Watts
* @since 2017/03/08
*
* FullName Haar::HaarEnergy
* Qualifier
* @param cv::Mat data
* @param cv::Mat & energyImage
* @param int width
* @param int height
* @param int iterations
* @param bool laplace = true
* @param bool bSquare = true
* @return void
* Access private
*/
void Haar::HaarEnergy(cv::Mat data, cv::Mat &energyImage, int width, int height, int iterations, bool laplace, bool bSquare)
{
int ii, ij, oi, oj, ind;
double divisor, dVal, dNormVal;
for (int k = 0; k < iterations; k++)
{
ind = k + 1;
oi = data.cols >> ind;
oj = data.rows >> ind;
divisor = 1;// pow(4.0, k + 1.0);
for (int i = 0; i < width; i++)
{
ii = i >> k;
for (int j = 0; j < height; j++)
{
ij = j >> k;
//DXDY
dVal = data.ptr<double>(oj + ij)[oi + ii];
energyImage.ptr<double>(j)[i] += getEnergy(dVal, divisor, bSquare); // DXDY
if (!laplace) // wavelet SXDY & SYDX
{
dVal = data.ptr<double>(oj + ij)[ii];
energyImage.ptr<double>(j)[i] += getEnergy(dVal, divisor, bSquare);
dVal = data.ptr<double>(ij)[ii];
energyImage.ptr<double>(j)[i] += getEnergy(dVal, divisor, bSquare);
}
}
}
}
}
/**
*
* perform Laplacian pyramid
*
* @author David Watts
* @since 2017/03/08
*
* FullName Haar::LaplacianPyramid
* Qualifier
* @param cv::Mat im
* @param int levels
* @param bool includeScale
* @return cv::Mat
* Access private
*/
cv::Mat Haar::LaplacianPyramid(cv::Mat im, int levels, bool includeScale)
{
int l;
cv::Mat imOut = cv::Mat(im.rows * 2, im.cols * 2, im.type(), cv::Scalar(0));
cv::Mat currentImg = im;
for (l = 0; l<levels; l++) {
cv::Mat down, up;
cv::pyrDown(currentImg, down);
cv::pyrUp(down, up, currentImg.size());
cv::Mat lap = currentImg - up;
int y = imOut.rows >> (l + 1);
int x=0;
if (includeScale)
{
cv::Rect r = cv::Rect(x, y, lap.cols, lap.rows);
up.copyTo(imOut(r));
}
x = imOut.cols >> (l + 1);
cv::Rect r = cv::Rect(x, y, lap.cols, lap.rows);
lap.copyTo(imOut(r));
currentImg = down;
}
/*
if (includeScale)
{
int y = imOut.rows >> (levels + 1);
int x = imOut.cols >> (levels + 1);
cv::Rect r = cv::Rect(x, y, currentImg.cols, currentImg.rows);
currentImg.copyTo(imOut(r));
}
*/
return imOut;
}
/**
*
* Get OpenCV image of best (modal) values from Pyramid composition
*
* @author David Watts
* @since 2017/03/08
*
* FullName Haar::PyramidLevels
* Qualifier
* @param cv::Mat data
* @param int width
* @param int height
* @param int iterations
* @param const int NoFocusImages
* @return cv::Mat
* Access private
*/
cv::Mat Haar::PyramidLevels(cv::Mat data, int width, int height, int iterations, const int NoFocusImages)
{
int ii, ij, oi, oj, c, x, y, lev;
int* valueArray = new int[NoFocusImages];
cv::Mat bestlevels = cv::Mat(height, width, CV_8SC1, cv::Scalar(0));
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
memset(valueArray, 0, sizeof(int)*NoFocusImages);
for (int k = 0; k < iterations; k++) // index into wavelet image
{
ii = i >> (k + 1);
ij = j >> (k + 1);
oi = width >> (k + 1);
oj = height >> (k + 1);
if (data.ptr<char>(oj + ij)[oi + ii] >= 0 && data.ptr<char>(oj + ij)[oi + ii] < NoFocusImages)
valueArray[data.ptr<char>(oj + ij)[oi + ii]]++; // DXDY
if (data.ptr<char>(oj + ij)[ii] >= 0 && data.ptr<char>(oj + ij)[ii] < NoFocusImages)
valueArray[data.ptr<char>(oj + ij)[ii]]++; // SXDY
if (data.ptr<char>(ij)[oi + ii] >= 0 && data.ptr<char>(ij)[oi + ii] < NoFocusImages)
valueArray[data.ptr<char>(ij)[oi + ii]]++; // SYDX
}
int bestLevel = 0;
// get maximum value in value histogram (more frequent level value)
for (int k = 0; k < NoFocusImages; k++)
{
c = valueArray[k];
if (c > bestLevel)
{
bestLevel = c;
lev = k;
}
}
// should at least count one observation
if (bestLevel>0)
bestlevels.ptr<char>(j)[i] = lev;
else
bestlevels.ptr<char>(j)[i] = -1;
}
}
delete[] valueArray;
return bestlevels;
}
float** Haar::getHaarCooc(cv::Mat data, cv::Mat coocImage, int width, int height, int iterations, float maxEnergy)
{
int ii, ij, oi, oj, ind;
double divisor, dVal, dNormVal;
for (int k = 0; k < iterations; k++)
{
ind = k + 1;
oi = data.cols >> ind;
oj = data.rows >> ind;
divisor = 1;// pow(4.0, k + 1.0);
for (int i = 0; i < width; i++)
{
ii = i >> k;
for (int j = 0; j < height; j++)
{
ij = j >> k;
//DXDY
dVal = data.ptr<double>(oj + ij)[oi + ii];
dVal = data.ptr<double>(oj + ij)[ii];
dVal = data.ptr<double>(ij)[ii];
coocImage.ptr<double>(j)[i] = getEnergy(dVal, divisor, true);
}
}
}
return NULL;
}
/**
*
* Perform 2D Haar wavelet pyramid transform with OpenCV image
*
* @author David Watts
* @since 2017/03/08
*
* FullName Haar::Haar2
* Qualifier
* @param cv::Mat & data
* @param int iterations (no of levels)
* @return void
* Access private
*/
void Haar::Haar2(cv::Mat& data, int iterations)
{
bool zeroSmooth = false;
if (data.channels() == 1)
{
int width = data.cols;
int height = data.rows;
double* row;
double* col;
for (int k = 0; k < iterations; k++)
{
int levHeight = height >> k;
int levWidth = width >> k;
row = new double[levWidth];
for (int j = 0; j < levHeight; j++)
{
for (int i = 0; i < levWidth; i++)
row[i] = (double)data.ptr<double>(j)[i];
Haar::Haar1(row, levWidth);
for (int i = 0; i < levWidth; i++)
data.ptr<double>(j)[i] = row[i];
}
col = new double[levHeight];
for (int i = 0; i < levWidth; i++)
{
for (int j = 0; j < levHeight; j++)
col[j] = (double)data.ptr<double>(j)[i];
Haar::Haar1(col, levHeight);
for (int j = 0; j < levHeight; j++)
data.ptr<double>(j)[i] = col[j];
}
delete[] row;
delete[] col;
}
if (zeroSmooth)
{
int levHeight = height >> (iterations);
int levWidth = width >> (iterations);
for (int j = 0; j < levHeight + 3; j++)
for (int i = 0; i < levWidth + 3; i++)
data.ptr<double>(j)[i] = 0;
}
}
else
{
std::cout << " " << __FUNCTION__ << ": error processing image with more than one channel" << std::endl;
}
}
/*
void LaplacianEnergy(QList<Mat>& arrIM, Mat& smallestLevel, int levels)
{
foreach(Mat im , arrIM) {
int valueArray[NoFocusImages];
Mat bestlevels = cv::Mat(height, width, CV_8SC1, cv::Scalar(0));
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
memset(valueArray, 0, sizeof(int)*NoFocusImages);
// bottom left SYDX
indi = i; indj = j;
startx = indi - fs1; starty = indj - fs1;
endx = indi + fs1; endy = indj + fs1;
// center
if(data[indi+indj*width]>=0)
valueArray[data[indi+indj*width]]+=15; // center pixel add equal to surround
// fs1 area
for(x=startx; x<endx; x++)
for(y=starty; y<endy; y++)
{
if(x>=0 && y>=oj && x<oi && y<oj+oj)
{
// surround
if(im.ptr(y)[x]>=0)
valueArray[im.ptr(y)[x]]++;
}
}
int bestLevel = 0;
// get maximum value in value histogram (more frequent level value)
for (int k = 0; k < NoFocusImages; k++)
{
c = valueArray[k];
if(c > bestLevel)
{
bestLevel = c;
lev = k;
}
}
// should at least count one observation
if(bestLevel>0)
bestlevels.ptr<char>(j)[i] = lev;
else
bestlevels.ptr<char>(j)[i] = -1;
}
}
return bestlevels;
}
*/