opencv - matchTemplate template matching

Concepts and principles template matching

Template matching is to find in one image and the template image that best matches (like) part.

Specific steps from left to right, matching degree calculation template image with an image area coverage from top to bottom, the greater the degree of match, the greater the possibility of both are the same.

 

Realize template matching: matchTemplate function

void matchTemplate(InputArray image, InputArray temp1, OutputArray result, int method);

  • Image, the image to be searched, and for an 8-bit or 32-bit floating-point image.
  • temp1, search templates, you need the original picture and have the same data type, and size can not be larger than the source image.
  • Result, mapped image comparison result, which must be single-channel, 32-bit floating-point images, the size (image.cols - temp1.cols +1) × (image.rows - temp1.rows +1)
  • method, matching method, there are six kinds of options:
Matching The formula

Matching squared difference

TM_SQDIFF

 

Normalized square difference matching

TM_SQDIFF_NORMED

 

Correlation matching method

TM_CCORR

 

Normalized correlation matching method

TM_CCORR_NORMED

 

Coefficient Matching

TM_CCOEFF

 

among them

Correlation coefficient matching

TM_CCOEFF_NORMED

 

 

Code Example:

#include<opencv.hpp>
#include<iostream>
#include<string>
using namespace std;
using namespace cv;
Mat src, temp;
int method = 0;
void ChangeMethod(int, void*) {
    switch (method){
    case 0:
        method = TM_SQDIFF;
        break;
    case 1:
        method = TM_SQDIFF_NORMED;
        break;
    case 2:
        method = TM_CCORR;
        break;
    case 3:
        method = TM_CCORR_NORMED;
        break;
    case 4:
        method = TM_CCOEFF;
        break;
    case 5:
        method = TM_CCOEFF_NORMED;
        break;
    }
    Mat result;
    matchTemplate(src, temp, result, method);
    imshow("result", result);

    Mat dst = src.clone();
    double mxValue, mnValue;
    Point mxPoint, mnPoint;
    minMaxLoc(result, &mnValue, &mxValue, &mnPoint, &mxPoint);
//https://www.cnblogs.com/bjxqmy/p/12386274.html
if (method == TM_SQDIFF || method == TM_SQDIFF_NORMED) { rectangle(dst, mnPoint, Point(mnPoint.x + temp.cols, mnPoint.y + temp.rows), Scalar(0, 0, 255)); } else{ rectangle(dst, mxPoint, Point(mxPoint.x + temp.cols, mxPoint.y + temp.rows), Scalar(0, 0, 255)); } imshow("dst", dst); } int main() { src = imread("C:/Users/齐明洋/Desktop/1.jpg"); temp = imread("C:/Users/齐明洋/Desktop/a.jpg"); imshow("src", src); imshow("temp", temp); namedWindow("dst"); createTrackbar("method", "ff", &method, 5, ChangeMethod); ChangeMethod(0, 0); waitKey(0); }

Demonstration effect:

Artwork and templates:

 

TM_SQDIFF method

 

TM_SQDIFF_NORMED method

 

TM_CCORR method

 

TM_CCORR_NORMED method

 

TM_CCOEFF method

 

TM_CCOEFF_NORMED method

 

Reference blog: http://www.opencv.org.cn/opencvdoc/2.3.2/html/doc/tutorials/imgproc/histograms/template_matching/template_matching.html

https://www.cnblogs.com/fuzhuoxin/p/12158777.html

 

Guess you like

Origin www.cnblogs.com/bjxqmy/p/12454971.html