-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
color_match_template.cpp
96 lines (85 loc) · 2.84 KB
/
color_match_template.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
#include <iostream>
#include <fstream>
#include <opencv2/core.hpp>
#include <opencv2/core/utility.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/ximgproc.hpp>
#include <opencv2/ximgproc/color_match.hpp>
using namespace std;
using namespace cv;
static void AddSlider(String sliderName, String windowName, int minSlider, int maxSlider, int valDefault, int *valSlider, void(*f)(int, void *), void *r)
{
createTrackbar(sliderName, windowName, valSlider, 1, f, r);
setTrackbarMin(sliderName, windowName, minSlider);
setTrackbarMax(sliderName, windowName, maxSlider);
setTrackbarPos(sliderName, windowName, valDefault);
}
struct SliderData {
Mat img;
int thresh;
};
static void UpdateThreshImage(int , void *r)
{
SliderData *p = (SliderData*)r;
Mat dst,labels,stats,centroids;
threshold(p->img, dst, p->thresh, 255, THRESH_BINARY);
connectedComponentsWithStats(dst, labels, stats, centroids, 8);
if (centroids.rows < 10)
{
cout << "**********************************************************************************\n";
for (int i = 0; i < centroids.rows; i++)
{
cout << dst.cols - centroids.at<double>(i, 0) << " ";
cout << dst.rows - centroids.at<double>(i, 1) << "\n";
}
cout << "----------------------------------------------------------------------------------\n";
}
flip(dst, dst, -1);
imshow("Max Quaternion corr",dst);
}
int main(int argc, char *argv[])
{
cv::CommandLineParser parser(argc, argv,
"{help h | | match color image }{@colortemplate | | input color template image}{@colorimage | | input color image}");
if (parser.has("help"))
{
parser.printMessage();
return -1;
}
string templateName = parser.get<string>("@colortemplate");
if (templateName.empty())
{
parser.printMessage();
parser.printErrors();
return -2;
}
string colorImageName = parser.get<string>("@colorimage");
if (templateName.empty())
{
parser.printMessage();
parser.printErrors();
return -2;
}
Mat imgLogo = imread(templateName, IMREAD_COLOR);
Mat imgColor = imread(colorImageName, IMREAD_COLOR);
imshow("Image", imgColor);
imshow("template", imgLogo);
// OK NOW WHERE IS OPENCV LOGO ?
Mat imgcorr;
SliderData ps;
ximgproc::colorMatchTemplate(imgColor, imgLogo, imgcorr);
imshow("quaternion correlation real", imgcorr);
normalize(imgcorr, imgcorr,1,0,NORM_MINMAX);
imgcorr.convertTo(ps.img, CV_8U, 255);
imshow("quaternion correlation", imgcorr);
ps.thresh = 0;
AddSlider("Level", "quaternion correlation", 0, 255, ps.thresh, &ps.thresh, UpdateThreshImage, &ps);
int code = 0;
while (code != 27)
{
code = waitKey(50);
}
waitKey(0);
return 0;
}