Description of the parameter "result" in cvmatchtemplate

Source: Internet
Author: User

First, see. Assume that the large brown image is the image to be tested, and the small red image is the template image.

1. Meaning of data in result.

The template matching function cvmatchtemplate calculates the similarity between the template and the overlapping area of the image to be tested in sequence, and stores the result into the mapped image result. That is to say, the value of each vertex in the result image represents a Similarity comparison result.

2. Size of result.

We can see that the template moves one pixel horizontally or vertically on the image to be tested, and performs a comparison calculation. Therefore, the horizontal comparison is w-w + 1, compare the values of H-H + 1 vertically to obtain a (w-w + 1) × (H-H + 1) dimension result matrix, result is a matrix expressed by an image. Therefore, the size of the result is (w-w + 1) × (H-H + 1 ).

3. How to obtain the best matching region in result

Use the cvminmaxloc (result, & min_val, & max_val, & min_loc, & max_loc, null) function to extract the maximum value (highest similarity) from the result) and the location of the maximum value (that is, the Coordinate Position of the maximum value max_val In the result max_loc, that is, the coordinate in the upper left corner of the template sliding, similar to the coordinate in the figure (x, y ).)

The following result shows that rect = cvrect (max_loc.x, max_loc.y, TMP-> width, TMP-> height); the rectangular area indicated by rect is the best matching area.


I tested it through a program, and there was an error in the above description. The rectangle of the best matching area should be determined by using min_loc, that is:

Rect = cvrect (max_loc.x, max_loc.y, TMP-> width, TMP-> height );

As follows:

Template: Graph to be matched: matching result:

int main() {IplImage *src, *temp1, *ftmp;if ((src = cvLoadImage("/home/murphy/Pictures/1.jpg", 1)) == 0) {return -1;}if ((temp1 = cvLoadImage("/home/murphy/Pictures/Screenshot-3.png", 1)) == 0) {return -1;}int iwidth = src->width - temp1->width + 1;int iheight = src->height - temp1->height + 1;ftmp = cvCreateImage(cvSize(iwidth, iheight), 32, 1);double min_val;double max_val;CvPoint min_loc;CvPoint max_loc;cvMatchTemplate(src, temp1, ftmp, 0);cvMinMaxLoc(ftmp, &min_val, &max_val, &min_loc, &max_loc, NULL);cvRectangle(src, cvPoint(min_loc.x, min_loc.y), cvPoint((min_loc.x + temp1->width),(min_loc.y + temp1->height)), CV_RGB(0,255,0), 1);cvNamedWindow("src",1);cvShowImage("src",src);cvWaitKey(0);return 0;}

Yesterday, I found in another post that max_loc is used to determine the position of the rectangle. Today I checked the information. It turns out that this is related to the method parameters used for matching, as shown below:

About the parameter method:

Cv_tm_sqdiff square difference matching method: This method uses square difference for matching; the best matching value is 0; the worse the matching, the larger the matching value.

Cv_tm_ccorr correlation matching method: This method adopts multiplication. A greater value indicates a better matching degree.

Cv_tm_ccoeff correlation coefficient matching method: 1 indicates perfect matching;-1 indicates the worst matching.

Cv_tm_sqdiff_normed normalized squared difference matching method

Cv_tm_c1__normed normalization Correlation Matching Method

Cv_tm_ccoeff_normed normalization correlation coefficient matching method

Thank you,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.