1
+ #include < iostream>
2
+ #include < opencv2/core.hpp>
3
+ #include < opencv2/core/utility.hpp>
4
+ #include < opencv2/highgui.hpp>
5
+ #include < opencv2/imgproc.hpp>
6
+ #include < opencv2/ximgproc.hpp>
7
+ #include < opencv2/ximgproc/color_match.hpp>
8
+
9
+ using namespace std ;
10
+ using namespace cv ;
11
+
12
+
13
+
14
+ static void AddSlider (String sliderName, String windowName, int minSlider, int maxSlider, int valDefault, int *valSlider, void (*f)(int , void *), void *r)
15
+ {
16
+ createTrackbar (sliderName, windowName, valSlider, 1 , f, r);
17
+ setTrackbarMin (sliderName, windowName, minSlider);
18
+ setTrackbarMax (sliderName, windowName, maxSlider);
19
+ setTrackbarPos (sliderName, windowName, valDefault);
20
+ }
21
+
22
+ struct SliderData {
23
+ Mat img;
24
+ int thresh;
25
+ vector<Point > pRef;
26
+ };
27
+
28
+ static void UpdateThreshImage (int , void *r)
29
+ {
30
+ SliderData *p = (SliderData*)r;
31
+ Mat dst,labels,stats,centroids;
32
+
33
+ threshold (p->img , dst, p->thresh , 255 , THRESH_BINARY);
34
+
35
+ connectedComponentsWithStats (dst, labels, stats, centroids, 8 );
36
+ if (centroids.rows < 10 )
37
+ {
38
+ cout << " **********************************************************************************\n " ;
39
+ for (int i = 0 ; i < static_cast <int >(p->pRef .size ()); i++)
40
+ {
41
+ cout << p->pRef [i] << " \n " ;
42
+ }
43
+ cout << " ---\n " ;
44
+ for (int i = 0 ; i < centroids.rows ; i++)
45
+ {
46
+ cout << dst.cols - centroids.at <double >(i, 0 ) << " " ;
47
+ cout << dst.rows - centroids.at <double >(i, 1 ) << " \n " ;
48
+ }
49
+ cout << " ----------------------------------------------------------------------------------\n " ;
50
+ }
51
+ flip (dst, dst, -1 );
52
+
53
+ imshow (" Max Quaternion corr" ,dst);
54
+ }
55
+
56
+ int main (int , char *[])
57
+ {
58
+ #define TESTMATCHING
59
+
60
+ #ifdef TESTMATCHING
61
+ Mat imgLogo = imread (" g:/lib/opencv/samples/data/opencv-logo.png" , IMREAD_COLOR);
62
+ Mat fruits = imread (" g:/lib/opencv/samples/data/lena.jpg" , IMREAD_COLOR);
63
+ fruits = fruits ;
64
+ // resize(fruits,fruits, Size(), 0.5, 0.5);
65
+ Mat img,colorTemplate;
66
+ imgLogo (Rect (0 , 0 , imgLogo.cols , 580 )).copyTo (img);
67
+ resize (img, colorTemplate, Size (), 0.05 , 0.05 );
68
+ vector<Mat> colorMask (4 );
69
+ inRange (colorTemplate, Vec3b (255 , 255 , 255 ), Vec3b (255 , 255 , 255 ), colorMask[0 ]);
70
+ // colorTemplate.setTo(Scalar(0,0,0), colorMask[0]);
71
+
72
+ inRange (colorTemplate, Vec3b (255 , 0 , 0 ), Vec3b (255 , 0 , 0 ), colorMask[0 ]);
73
+ inRange (colorTemplate, Vec3b (0 , 255 , 0 ), Vec3b (0 ,255 , 0 ), colorMask[1 ]);
74
+ inRange (colorTemplate, Vec3b ( 0 , 0 ,255 ), Vec3b ( 0 , 0 ,255 ), colorMask[2 ]);
75
+ colorMask[3 ] = Mat (colorTemplate.size (), CV_8UC3, Scalar (255 ));
76
+ SliderData ps;
77
+ RNG r;
78
+ for (int i = 0 ; i < 16 ; i++)
79
+ {
80
+ Point p (i / 4 * 130 + 10 +r.uniform (-10 ,10 ), (i % 4 ) * 130 + 10 +r.uniform (-10 , 10 ));
81
+ Mat newLogo= colorTemplate.clone ();
82
+ if (i % 6 != 5 )
83
+ {
84
+ newLogo.setTo (Scalar (r.uniform (0 , 256 ), r.uniform (0 , 256 ), r.uniform (0 , 256 )), colorMask[i % 4 ]);
85
+ newLogo.setTo (Scalar (r.uniform (0 , 256 ), r.uniform (0 , 256 ), r.uniform (0 , 256 )), colorMask[(i + 1 ) % 4 ]);
86
+ }
87
+ else
88
+ ps.pRef .push_back (p);
89
+ newLogo.copyTo (fruits (Rect (p.x , p.y , newLogo.cols , newLogo.rows )));
90
+
91
+ }
92
+ #else
93
+ Mat fruits = imread (" c2.png" , IMREAD_COLOR);
94
+ Mat colorTemplate = imread (" c1.png" , IMREAD_COLOR);
95
+ Mat img= colorTemplate;
96
+ #endif
97
+ imshow (" Image" , fruits);
98
+ imshow (" opencv_logo" , colorTemplate);
99
+
100
+ if (img.empty ())
101
+ {
102
+ cout << " Cannot load image file\n " ;
103
+ return 0 ;
104
+ }
105
+ Mat imgcorr;
106
+ ximgproc::colorMatchTemplate (fruits, colorTemplate, imgcorr);
107
+ imshow (" quaternion correlation real" , imgcorr);
108
+ normalize (imgcorr, imgcorr,1 ,0 ,NORM_MINMAX);
109
+ imgcorr.convertTo (ps.img , CV_8U, 255 );
110
+ imshow (" quaternion correlation" , imgcorr);
111
+ AddSlider (" Level" , " quaternion correlation" , 0 , 255 , ps.thresh , &ps.thresh , UpdateThreshImage, &ps);
112
+ int code = 0 ;
113
+ while (code != 27 )
114
+ {
115
+ code = waitKey (50 );
116
+ }
117
+
118
+ FileStorage fs (" corr.yml" , FileStorage::WRITE);
119
+ fs<<" Image" << imgcorr;
120
+ fs.release ();
121
+ waitKey (0 );
122
+ return 0 ;
123
+ }
0 commit comments