Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/samples/cpp/mask_tmpl.cpp
16337 views
1
#include "opencv2/imgproc.hpp"
2
#include "opencv2/highgui.hpp"
3
#include <iostream>
4
5
using namespace std;
6
using namespace cv;
7
8
int main( int argc, const char** argv )
9
{
10
CommandLineParser parser(argc, argv,
11
"{ i | ../data/lena_tmpl.jpg |image name }"
12
"{ t | ../data/tmpl.png |template name }"
13
"{ m | ../data/mask.png |mask name }"
14
"{ cm| 3 |comparison method }");
15
16
cout << "This program demonstrates the use of template matching with mask.\n\n";
17
parser.printMessage();
18
19
string filename = parser.get<string>("i");
20
string tmplname = parser.get<string>("t");
21
string maskname = parser.get<string>("m");
22
Mat img = imread(filename);
23
Mat tmpl = imread(tmplname);
24
Mat mask = imread(maskname);
25
Mat res;
26
27
if(img.empty())
28
{
29
cout << "can not open " << filename << endl;
30
return -1;
31
}
32
33
if(tmpl.empty())
34
{
35
cout << "can not open " << tmplname << endl;
36
return -1;
37
}
38
39
if(mask.empty())
40
{
41
cout << "can not open " << maskname << endl;
42
return -1;
43
}
44
45
int method = parser.get<int>("cm"); // default 3 (CV_TM_CCORR_NORMED)
46
matchTemplate(img, tmpl, res, method, mask);
47
48
double minVal, maxVal;
49
Point minLoc, maxLoc;
50
Rect rect;
51
minMaxLoc(res, &minVal, &maxVal, &minLoc, &maxLoc);
52
53
if(method == TM_SQDIFF || method == TM_SQDIFF_NORMED)
54
rect = Rect(minLoc, tmpl.size());
55
else
56
rect = Rect(maxLoc, tmpl.size());
57
58
rectangle(img, rect, Scalar(0, 255, 0), 2);
59
60
imshow("detected template", img);
61
waitKey();
62
63
return 0;
64
}
65
66