-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_interest_points.cpp
632 lines (522 loc) · 17.7 KB
/
image_interest_points.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
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
#include "image_interest_points.h"
#include <wx/busyinfo.h>
#include "wx/msgdlg.h"
#include "filesys.h"
#include <fstream>
#include <matplot/matplot.h>
void CImageComponentsDescriptorBase::detectRegions(int mode1, int mode2)
{
Mat src = original_image.clone();
// Convert image to grayscale
Mat gray;
if (isGrayScaleImage(src) == false)
{
cvtColor(src, gray, COLOR_BGR2GRAY);
}
else
{
gray = src.clone();
}
Mat canny_output;
Canny(gray, canny_output, 50, 255);
// Find all the contours in the thresholded image
findContours(canny_output, raw_contourns, mode1, mode2);
}
void CImageComponentsDescriptorNormal::getObjectsInfo()
{
for (const auto& c : raw_contourns)
{
// declare the region
std::vector<Point> original = c;
cv::Moments momInertia = cv::moments(cv::Mat(original));
ImageComponentsDescriptor.region = original;
ImageComponentsDescriptor.momInertia = momInertia;
ImageComponentsDescriptor.convex = isContourConvex(original);
Objects.push_back(ImageComponentsDescriptor);
}
}
void CImageComponentsDescriptorHull::getObjectsInfo()
{
for (const auto& c : raw_contourns)
{
// declare the region
std::vector<Point> hull;
convexHull(c, hull);
Moments momInertiaHull = cv::moments(cv::Mat(hull));
ImageComponentsDescriptor.region = hull;
ImageComponentsDescriptor.momInertia = momInertiaHull;
ImageComponentsDescriptor.convex = isContourConvex(hull);
Objects.push_back(ImageComponentsDescriptor);
}
}
void CImageComponentsDescriptorAprox::getObjectsInfo()
{
for (const auto& c : raw_contourns)
{
// declare the region
std::vector<Point> Aprox;
double epsilon = 0.1 * arcLength(c, true);
approxPolyDP(c, Aprox, epsilon, true);
cv::Moments momInertiaAprox = cv::moments(cv::Mat(Aprox));
ImageComponentsDescriptor.region = Aprox;
ImageComponentsDescriptor.momInertia = momInertiaAprox;
ImageComponentsDescriptor.convex = isContourConvex(Aprox);
Objects.push_back(ImageComponentsDescriptor);
}
}
namespace image_info
{
ObjectsCollection getContournInfo(const Mat& img)
{
CImageComponentsDescriptorHull hull(img);
hull.detectRegions(CHAIN_APPROX_SIMPLE);
hull.getObjectsInfo();
return hull.getImageFullInformation();
}
std::pair<int, int> getCentroid(cv::Moments& momInertia)
{
int cx = momInertia.m10 / momInertia.m00;
int cy = momInertia.m01 / momInertia.m00;
std::pair<int, int> p(cx, cy);
return p;
}
double getArea(std::vector<cv::Point>& region)
{
return contourArea(region);
}
double getPerimeter(std::vector<cv::Point>& region, bool closed)
{
return arcLength(region, closed);
}
double getRoundNess(std::vector<cv::Point>& region)
{
double Area = getArea(region);
double Perimeter = getPerimeter(region,true);
return 4 * CV_PI * (Area / pow(Perimeter, 2));
}
double getOrientation(cv::Moments& momInertia)
{
double u11 = momInertia.m11;
double u20 = momInertia.m20;
double u02 = momInertia.m02;
double factor = (2 * u11) / (u20 - u02);
double angle = 0.5 * atan(factor);
double degrees = angle * (180.0 / CV_PI);
if (degrees < 0)
{
degrees = 180 + degrees;
}
return degrees;
}
void getHuMoments(std::vector<cv::Point>& region, double* huh)
{
if (huh != nullptr)
{
Moments moments = cv::moments(region, false);
HuMoments(moments, huh);
for (int i = 0; i < 7; i++)
{
huh[i] = -1 * copysign(1.0, huh[i]) * log10(abs(huh[i]));
}
}
}
std::string getHuhMomentsLine(Mat& img)
{
std::stringstream os;
Mat clone = convertograyScale(img);
// Calculate Moments
Moments moments = cv::moments(clone, false);
// Calculate Hu Moments
double huMoments[7];
HuMoments(moments, huMoments);
for (int i = 0; i < 7; i++)
{
huMoments[i] = -1 * copysign(1.0, huMoments[i]) * log10(abs(huMoments[i]));
}
for (const auto& h : huMoments)
{
os << h << ",";
}
os << std::endl;
return os.str();
}
std::string convertWxStringToString(const wxString wsx)
{
std::stringstream s;
s << wsx;
return s.str();
}
int readCSV2( std::vector<std::vector<double>>& obs,
int nfields,
bool ignoreheader,
std::string& filename)
{
std::ifstream myFile;
myFile.open(filename, std::ios_base::in);
if (myFile.is_open())
{
while (myFile.good())
{
std::string Line;
getline(myFile, Line);
if (Line[0] == '#')
{
continue;
}
if (ignoreheader == true)
{
ignoreheader = false;
continue;
}
if (Line.length() == 0)
{
break;
}
int pos = 0;
int start = 0;
std::vector<double> ob;
int cnt = 0;
while (pos != -1)
{
if (cnt < nfields)
{
pos = static_cast<int>(Line.find(',', start));
std::string tmp = Line.substr(start, pos - start);
start = pos + 1;
double f = stof(tmp);
ob.push_back(f);
cnt++;
}
else
{
break;
}
}
obs.push_back(ob);
}
myFile.close();
}
else
{
return -1;
}
return 0;
};
std::string loadDescriptorFile()
{
wxFileDialog openFileDialog(nullptr,
wxEmptyString,
wxEmptyString,
wxEmptyString,
"csv file (*.csv)|*.csv|All Files (*.*)|*.*", wxFD_OPEN|wxFD_FILE_MUST_EXIST);
std::string spath;
if (openFileDialog.ShowModal() == wxID_OK)
{
wxString path = openFileDialog.GetPath();
spath = convertWxStringToString(path);
}
return spath;
}
}
namespace fast_algo
{
std::vector < cv::KeyPoint > ApplyFAST(const Mat& img)
{
Mat gray = convertograyScale(img);
cv::Ptr<cv::FeatureDetector> ptrDetector; // generic detector
ptrDetector = cv::FastFeatureDetector::create(80);
std::vector<cv::KeyPoint> keypoints;
// Keypoint detection
ptrDetector->detect(gray, keypoints);
return keypoints;
}
void ApplyAndCompareFAST( std::vector<Mat>& images,
std::vector<std::string>& filenames)
{
}
}
namespace op_busy
{
wxBusyInfo* ProgramBusy()
{
std::mutex mtex;
mtex.lock();
wxWindowDisabler disableAll;
wxBusyInfo* wait = new wxBusyInfo("Please wait, working...");
mtex.unlock();
return wait;
}
void Stop(wxBusyInfo* wait)
{
wxYield();
if (nullptr != wait)
{
delete wait;
}
}
}
namespace sift_algo
{
void createCSV(std::vector < cv::KeyPoint >& descriptors, std::string fname)
{
std::ofstream myfile(fname);
if (myfile.is_open())
{
myfile << "x,y,size,angle,response,octave,class_id" << std::endl;
for (const auto& descriptor : descriptors)
{
// write fields to s
std::stringstream s;
s << descriptor.pt.x << ",";
s << descriptor.pt.y << ",";
s << descriptor.size << ",";
s << descriptor.angle << ",";
s << descriptor.response << ",";
s << descriptor.octave << ",";
s << descriptor.class_id << std::endl;
myfile << s.str();
}
myfile.close();
}
}
std::vector < cv::KeyPoint> ApplySift(const Mat& img, Mat& descriptors)
{
Mat gray = convertograyScale(img);
//auto sift = SIFT::create();
cv::Ptr<cv::Feature2D> sift = SIFT::create();
std::vector<cv::KeyPoint> keypoints;
sift->detect(gray, keypoints);
sift->detectAndCompute(gray, noArray(), keypoints, descriptors);
return keypoints;
}
Mat getMatchedImage( Mat& descriptor1,
Mat& descriptor2,
std::vector < cv::KeyPoint >& kp1,
std::vector < cv::KeyPoint >& kp2,
Mat& img1,
Mat& img2,
int option)
{
Mat result;
std::vector< DMatch > matches;
if (option == 0)
{
cv::BFMatcher matcher(cv::NORM_L2, true);
matcher.match(descriptor1, descriptor2, matches);
// extract the show_matches best matches
int show_matches = min(static_cast<int>(matches.size()), 10);
try
{
std::nth_element(matches.begin(), matches.begin() + show_matches, matches.end());
matches.erase(matches.begin() + show_matches, matches.end());
}
catch (...)
{
}
}
else
if (option == 1)
{
std::vector<std::vector<cv::DMatch>> matches2D;
cv::BFMatcher matcher(NORM_L2);
matcher.knnMatch(descriptor1, descriptor2, matches2D, 2); // find the k best match
double ratio = 0.85;
std::vector<std::vector<cv::DMatch>>::iterator it;
for (it = matches2D.begin(); it != matches2D.end(); ++it)
{
// first best match/second best match
if ((*it)[0].distance / (*it)[1].distance < ratio)
{
// it is an acceptable match
matches.push_back((*it)[0]);
}
}
}
else
if (option == 2)
{
double maxDist = 0.4;
std::vector<std::vector<cv::DMatch>> matches2D;
cv::BFMatcher matcher(NORM_L2);
// maximum acceptable distance
// between the 2 descriptors
matcher.radiusMatch(descriptor1, descriptor2, matches2D, maxDist);
matches = matches2D[1];
}
drawMatches(
img1, // InputArray img1,
kp1, // const std::vector< KeyPoint > & keypoints1,
img2, // InputArray img2,
kp2, // const std::vector< KeyPoint > & keypoints2,
matches, // const std::vector< DMatch > & matches1to2,
result, // InputOutputArray outImg
1 // const int matchesThickness
);
return result;
}
std::string convertWxStringToString(const wxString wsx)
{
std::stringstream s;
s << wsx;
return s.str();
}
void saveCSV(std::vector < cv::KeyPoint >& kp1)
{
if (wxYES == wxMessageBox(wxT("Save file?"),
wxT("Save file?"),
wxNO_DEFAULT | wxYES_NO | wxCANCEL | wxICON_INFORMATION,
nullptr))
{
wxFileDialog saveFileDialog(nullptr,
wxEmptyString,
wxEmptyString,
"sift.csv",
"Text Files (*.csv)|*.csv|All Files (*.*)|*.*",
wxFD_SAVE);
if (saveFileDialog.ShowModal() == wxID_OK)
{
wxString spath = saveFileDialog.GetPath();
std::string path = convertWxStringToString(spath);
sift_algo::createCSV(kp1, path);
}
}
}
Mat ApplyAndCompareSIFT( std::vector<Mat>& images,
std::vector<std::string>& filenames)
{
Mat& img1 = images[0];
Mat& img2 = images[1];
int width_01 = img1.size().width;
int height_01 = img1.size().height;
resize(img2, img2, Size(width_01, height_01), INTER_LINEAR);
Mat descriptor1;
Mat descriptor2;
std::vector < cv::KeyPoint > kp1 = ApplySift(img1, descriptor1);
std::vector < cv::KeyPoint > kp2 = ApplySift(img2, descriptor2);
saveCSV(kp1);
saveCSV(kp2);
Mat result = getMatchedImage(descriptor1, descriptor2, kp1, kp2, img1, img2);
return result;
}
}
namespace template_matching
{
// https://docs.opencv.org/3.4/de/da9/tutorial_template_matching.html
std::pair<Mat,Mat> ApplyTemplateMatching( const Mat& BigImage,
Mat& templ)
{
Mat result;
Mat img_display;
BigImage.copyTo(img_display);
Mat segmented1 = ApplyCannyAlgoFull(BigImage);
templ = ApplyCannyAlgoFull(templ);
int result_cols = BigImage.cols - templ.cols + 1;
int result_rows = BigImage.rows - templ.rows + 1;
result.create(result_rows, result_cols, CV_32FC1);
matchTemplate(segmented1, templ, result, TM_CCORR);
normalize(result, result, 0, 1, NORM_MINMAX, -1, Mat());
double minVal; double maxVal; Point minLoc; Point maxLoc;
Point matchLoc;
minMaxLoc(result, &minVal, &maxVal, &minLoc, &maxLoc, Mat());
matchLoc = maxLoc;
rectangle( img_display,
matchLoc,
Point( matchLoc.x + templ.cols,
matchLoc.y + templ.rows),
Scalar::all(0),
2,
8,
0);
std::pair<Mat, Mat> p(img_display, result);
return p;
}
template<typename F, typename...Args>
Mat
ApplyTemplateMatchingFull(const Mat& BigImage,
std::vector<Mat>& templ,
int mode,
F& f,
Args&&... args)
{
Mat segmented1 = f(BigImage, args...);
Mat img_display;
BigImage.copyTo(img_display);
for (auto& tmpt : templ)
{
Mat result;
tmpt = f(tmpt,args...);
int result_cols = BigImage.cols - tmpt.cols + 1;
int result_rows = BigImage.rows - tmpt.rows + 1;
result.create(result_rows, result_cols, CV_32FC1);
matchTemplate(segmented1, tmpt, result, mode);
normalize(result, result, 0, 1, NORM_MINMAX, -1, Mat());
double minVal;
double maxVal;
Point minLoc;
Point maxLoc;
Point matchLoc;
minMaxLoc(result, &minVal, &maxVal, &minLoc, &maxLoc, Mat());
if ( abs(minVal) > 10e-7)
{
continue;
}
if (mode == TM_SQDIFF || mode == TM_SQDIFF_NORMED)
{
if (abs(minVal) > 10e-7)
{
continue;
}
matchLoc = minLoc;
}
else
{
double err = 10e-5;
if ( maxVal >= 1-err && maxVal<=1)
{
matchLoc = maxLoc;
}
else
{
continue;
}
}
rectangle( img_display,
matchLoc,
Point(
matchLoc.x + tmpt.cols,
matchLoc.y + tmpt.rows),
Scalar::all(0),
2,
8,
0);
}
return img_display;
}
namespace canny_matching
{
Mat ApplyTemplateMatchingFull_TM_SQDIFF(const Mat& BigImage, std::vector<Mat>& templ, int t1, int t2)
{
return template_matching::ApplyTemplateMatchingFull(BigImage, templ, TM_SQDIFF, ApplyCannyAlgoFull, t1, t2);
}
Mat ApplyTemplateMatchingFull_TM_SQDIFF_NORMED(const Mat& BigImage, std::vector<Mat>& templ, int t1, int t2)
{
return template_matching::ApplyTemplateMatchingFull(BigImage, templ, TM_SQDIFF_NORMED, ApplyCannyAlgoFull, t1, t2);
}
Mat ApplyTemplateMatchingFull_TM_CCORR(const Mat& BigImage, std::vector<Mat>& templ, int t1, int t2)
{
return template_matching::ApplyTemplateMatchingFull(BigImage, templ, TM_CCORR, ApplyCannyAlgoFull, t1, t2);
}
Mat ApplyTemplateMatchingFull_TM_CCORR_NORMED(const Mat& BigImage, std::vector<Mat>& templ, int t1, int t2)
{
return template_matching::ApplyTemplateMatchingFull(BigImage, templ, TM_CCORR_NORMED, ApplyCannyAlgoFull, t1, t2);
}
Mat ApplyTemplateMatchingFull_TM_CCOEFF(const Mat& BigImage, std::vector<Mat>& templ, int t1, int t2)
{
return template_matching::ApplyTemplateMatchingFull(BigImage, templ, TM_CCOEFF, ApplyCannyAlgoFull, t1, t2);
}
Mat ApplyTemplateMatchingFull_TM_CCOEFF_NORMED(const Mat& BigImage, std::vector<Mat>& templ, int t1, int t2)
{
return template_matching::ApplyTemplateMatchingFull(BigImage, templ, TM_CCOEFF_NORMED, ApplyCannyAlgoFull, t1, t2);
}
}
}