-
Notifications
You must be signed in to change notification settings - Fork 0
/
HelperFunctions.h
440 lines (354 loc) · 9.54 KB
/
HelperFunctions.h
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
// 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
#ifndef HELPERFUNCTIONS_H
#define HELPERFUNCTIONS_H
#include "opencv2/opencv.hpp"
#include "opencv/highgui.h"
#include <QVector>
#include <QImage>
#include <QJsonObject>
/**
* class for useful helper image manipulation functions
*/
class HelperFunctions {
public:
static cv::Mat displayHistogram(cv::Mat b_hist, int histSize, int hist_w = 512, int hist_h = 400);
static cv::Mat convertFloatToGreyscaleMat(cv::Mat im);
static QVector3D addPointToVector3D(QVector3D vect, QPointF pt);
static bool pointInPoly(QPolygonF poly, QPointF pt);
static bool pointInEllipse(QRectF ellipseRect, QPointF pt);
static int padd(int n, int levels = 3)
{
int power = pow(2, levels);
return (1 + n/power) * power;
};
static QSharedPointer<QRect> getPaddRect(QPoint ptStart, QPoint ptEnd, int levels);
static QJsonObject ObjectFromString(const QString& in);
static cv::Mat putMatScale(cv::Mat im, bool scale = true, bool bRed = true);
static void resizeImagePreserve(cv::Mat &imageIn, cv::Mat &imageOut, int newWidth, int newHeight);
static void DebugPrintMat(cv::Mat mat);
static bool checkMatCompatibility(cv::Mat& in, cv::Mat& out, const int expectedType, bool checkType = true);
static cv::Mat testReturn(cv::Mat& im);
static int reflect(int M, int x);
static QString type2str(int type);
static bool isGrayImage(const cv::Mat& img);
static int* getCImage(const cv::Mat& img);
static QString toDataURL(cv::Mat image);
static QImage makeQImage(cv::Mat im, bool bRGBSwap = true);
static QImage::Format getFormat(int type);
static cv::Scalar QC2S(QColor color);
static QColor S2QC(cv::Scalar color);
static QString getBarcodeStringfromVector(int* vect, int& size);
static void rotate90(cv::Mat &matImage, RotateFlags::RotateFlags rotflag);
template< typename T >
static cv::Mat expandRunLengthImage(T* Image, int length, int width, int height, int type, int channels, bool makeBorder = true, bool bBinary = false)
{
cv::Mat out(height, width, type);
T tmax = 0;
for(int i=0;i<width;i++)
tmax = MAX(Image[i], tmax);
// scale
for (int i = 0; i < width; i++)
Image[i] *= 255 * Image[i] / tmax;
if(length == height)
{
for (int j = 0; j < height; j++)
{
T val = bBinary ? (Image[j]>0 ? 255 : 0) : Image[j];
if(channels ==3)
{
Vec3b color = Vec3b(val, val, val);
for (int i = 0; i < width; i++)
out.at<Vec3b>(j, i) = color;
}
else
{
for (int i = 0; i < width; i++)
out.at<uchar>(j, i) = saturate_cast<uchar>(val);
}
}
}
else if(length == width)
{
for (int i = 0; i < width; i++)
{
T val = bBinary?(Image[i]>0?255:0): Image[i];
if(channels ==3)
{
Vec3b color = Vec3b(val, val, val);
for (int j = 0; j < height; j++)
out.at<Vec3b>(j, i) = color;
}
else
{
for (int j = 0; j < height; j++)
out.at<uchar>(j, i) = saturate_cast<uchar>(val);
}
}
}
if(makeBorder)
{
int border_size = 50;
cv::Mat big_image(height+ border_size*2, width+ border_size*2, type, cvScalar(255));
out.copyTo(big_image(cv::Rect(border_size, border_size, out.cols, out.rows)));
return big_image;
}
else
return out;
}
// template functions cannot be in cpp file!
/**
*
* creates a line plot of data
*
* @author David Watts
* @since 2017/03/08
*
* FullName linePlotImage
* Qualifier
* @param T * data // Y values
* @param int datasize
* @param int binX // interval between data points
* @param int size_w // image width
* @param int size_h // image height
* @return cv::Mat // plot image
* Access public static
*/
template< typename T >
static cv::Mat linePlotImage(QVector<T*> const& data, int datasize, int binX, int size_w=512, int size_h=512)
{
// Draw the histograms for B, G and
int cols[15][3] = {
{ 255, 255, 255 },{ 255, 0, 0 },{ 0, 255, 0 },{ 0, 0, 255 },
{ 255, 255, 0 },{ 0, 255, 255 },{ 255, 0, 255 },{ 192, 192, 192 },
{ 128, 128, 128 },{ 128, 0, 0 },{ 128, 128, 0 },{ 0, 128, 0 },
{ 128, 0, 128 },{ 0, 128, 128 },{ 0, 0, 128 } };
int fontFace = cv::FONT_HERSHEY_TRIPLEX;
int fontScale = 1;
cv::Scalar red(255, 0, 0);
cv::Scalar green(0, 255, 0);
cv::Scalar blue(0, 0, 255);
int maxheight = 0;
cv::String text;
cv::Size textSize;
int baseline = 0;
// create image for histogram
cv::Mat histImage(size_h, size_w, CV_8UC3, cv::Scalar(0, 0, 0));
// bin size in image
double bin_w = (((double)size_w) / ((double)datasize));
// get the x axis tick labels
// 5 ticks
int ticks = 5;
// assume data starts at zero
int maximum = binX*datasize;
int interval = maximum / ticks;
// round to decimal intervals
if (interval > 1000)
interval = interval - interval % 1000;
else if (interval > 100)
interval = interval - interval % 100;
else if (interval > 10)
interval = interval - interval % 10;
double ratioW = double(size_w) / double(maximum);
try
{
/// Draw for each channel
for (int i = 0; i < datasize*binX; i += interval)
{
text = cv::format("%d", i);
textSize = cv::getTextSize(text, fontFace, fontScale, 1, &baseline);
maxheight = max(maxheight, textSize.height);
putText(histImage, text, cv::Point((int)(i*ratioW), size_h), fontFace, fontScale, red, 8, true);
}
maxheight *= 1.5;
// scale data
T tmax= data[0][0], tmin = data[0][0];
for (int j = 0; j < data.length(); j++)
{
// Normalize the result to [ 0, histImage.rows ] - i.e. fit in screen
for (int i = 0; i < datasize; i++)
{
tmax = MAX(tmax, data[j][i]);
tmin = MIN(tmin, data[j][i]);
}
}
for (int j = 0; j < data.length(); j++)
{
double r = cols[j % 15][0];
double g = cols[j % 15][1];
double b = cols[j % 15][2];
cv::Scalar colour = cv::Scalar(r, g, b);
if(datasize>1)
{
/// Draw for each channel
for (int i = 1; i < datasize; i++)
{
T val1 = (T)((double)(data[j][i-1] - tmin))*(size_h / (double)(tmax - tmin));
T val2 = (T)((double)(data[j][i] - tmin))*(size_h / (double)(tmax - tmin));
cv::Point p1 = cv::Point((int)(bin_w*i-1), size_h - maxheight - val1);
cv::Point p2 = cv::Point((int)(bin_w*i), size_h - maxheight - val2);
cv::line(histImage, p1, p2, colour, 1, CV_AA);
}
}
}
}
catch (cv::Exception& e)
{
const char* err_msg = e.what();
DBOUT("exception caught: " << err_msg << std::endl);
}
return histImage;
}
/**
*
* creates a scaled opencv image from int* 1D image array
*
* @author David Watts
* @since 2017/03/08
*
* FullName putImageScale
* Qualifier
* @param T * im
* @param int w
* @param int h
* @return cv::Mat
* Access public static
*/
template< typename T >
static cv::Mat putImageScale(T* im, int w, int h)
{
int i;
uchar val;
T tmin = 999999;
T tmax = -999999;
for (i = 0; i < w*h; i++)
{
if (im[i] < 0)
im[i] = 0;
if (im[i] < tmin)
tmin = im[i];
if (im[i] > tmax)
tmax = im[i];
}
cv::Mat cMat(h, w, CV_8UC3);
for (int j = 0; j < h; j++)
{
for (int i = 0; i < w; i++)
{
val = (uchar)(((double)(im[i + j*w] - tmin))*(255.0 / (double)(tmax - tmin)));
// truncate to grayscale
val = MIN(MAX(0, val), 255);
Vec3b color = Vec3b(val, val, val);
cMat.at<Vec3b>(j, i) = color;
}
}
return cMat;
}
/**
*
* creates a opencv image from int* 1D image array
*
* @author David Watts
* @since 2017/03/08
*
* FullName putImage
* Qualifier
* @param T * im
* @param int width
* @param int height
* @return cv::Mat
* Access public static
*/
template<typename T>
static cv::Mat putImage(T* im, int width, int height)
{
cv::Mat cMat(h, w, CV_8UC3);
for (int j = 0; j < height; j++)
{
for (int i = 0; i < width; i++)
{
cMat->ptr<uchar>(j)[(i * 3)] = im[i + j*width];
cMat->ptr<uchar>(j)[(i * 3) + 1] = im[i + j*width];
cMat->ptr<uchar>(j)[(i * 3) + 2] = im[i + j*width];
}
}
return cMat;
}
/**
*
* Returns a greyscale 1D int* image array from an OpenCV image
*
* @author David Watts
* @since 2017/03/07
*
* FullName HelperFunctions::getImage
* Qualifier
* @param cv::Mat & m
* @return T*
* Access public
*/
template< typename T >
static T* getImage(cv::Mat& m)
{
fname();
T* im = new T[m.cols*m.rows];
if (m.channels() > 1)
{
cv::Mat greyMat;
cv::cvtColor(m, greyMat, cv::COLOR_BGR2GRAY);
for (int j = 0; j < m.rows; j++)
{
for (int i = 0; i < m.cols; i++)
im[i + j*m.cols] = greyMat.ptr<uchar>(j)[i];
}
}
else
{
for (int j = 0; j < m.rows; j++)
{
for (int i = 0; i < m.cols; i++)
im[i + j * m.cols] = m.ptr<uchar>(j)[i];
}
}
return im;
}
/**
*
* rescales image pixel values
*
* @author David Watts
* @since 2017/03/07
*
* FullName HelperFunctions::rescale
* Qualifier
* @param T * im
* @param int w
* @param int h
* @return void
* Access public
*/
template< typename T >
static void rescale(T* im, int w, int h)
{
fname();
int i;
T tmin = 999999;
T tmax = -999999;
for (i = 0; i < w*h; i++)
{
if (im[i] < 0)
im[i] = 0;
if (im[i] < tmin)
tmin = im[i];
if (im[i] > tmax)
tmax = im[i];
}
for (i = 0; i < w*h; i++)
{
im[i] = (T)((double)(im[i] - tmin))*(255.0 / (double)(tmax - tmin));
// std::cout << im[i] << " " ;
}
}
};
#endif