File: mask_tmpl.cpp

package info (click to toggle)
opencv 3.2.0%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 238,480 kB
  • sloc: xml: 901,650; cpp: 703,419; lisp: 20,142; java: 17,843; python: 17,641; ansic: 603; cs: 601; sh: 516; perl: 494; makefile: 117
file content (81 lines) | stat: -rw-r--r-- 1,873 bytes parent folder | download
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
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"

#include <cctype>
#include <iostream>
#include <iterator>
#include <stdio.h>

using namespace std;
using namespace cv;

static void help()
{
    cout << "\nThis program demonstrates template match with mask.\n"
            "Usage:\n"
            "./mask_tmpl -i=<image_name> -t=<template_name> -m=<mask_name>, Default is ../data/lena_tmpl.jpg\n"
            << endl;
}

int main( int argc, const char** argv )
{
    cv::CommandLineParser parser(argc, argv,
        "{help h||}"
        "{ i | ../data/lena_tmpl.jpg | }"
        "{ t | ../data/tmpl.png | }"
        "{ m | ../data/mask.png | }");
    if (parser.has("help"))
    {
        help();
        return 0;
    }
    string filename = parser.get<string>("i");
    string tmplname = parser.get<string>("t");
    string maskname = parser.get<string>("m");
    Mat img = imread(filename);
    Mat tmpl = imread(tmplname);
    Mat mask = imread(maskname);
    Mat res;

    if(img.empty())
    {
        help();
        cout << "can not open " << filename << endl;
        return -1;
    }

    if(tmpl.empty())
    {
        help();
        cout << "can not open " << tmplname << endl;
        return -1;
    }

    if(mask.empty())
    {
        help();
        cout << "can not open " << maskname << endl;
        return -1;
    }

    //int method = CV_TM_SQDIFF;
    int method = CV_TM_CCORR_NORMED;
    matchTemplate(img, tmpl, res, method, mask);

    double minVal, maxVal;
    Point minLoc, maxLoc;
    Rect rect;
    minMaxLoc(res, &minVal, &maxVal, &minLoc, &maxLoc);

    if(method == CV_TM_SQDIFF || method == CV_TM_SQDIFF_NORMED)
        rect = Rect(minLoc, tmpl.size());
    else
        rect = Rect(maxLoc, tmpl.size());

    rectangle(img, rect, Scalar(0, 255, 0), 2);

    imshow("detected template", img);
    waitKey();

    return 0;
}