Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Implementation method of template matching in opencv C++

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "the implementation of opencv C++ template matching". In daily operation, I believe many people have doubts about the implementation of opencv C++ template matching. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "opencv C++ template matching". Next, please follow the editor to study!

A simple implementation of # include # includeusing namespace cv;using namespace std;int main () {Mat img = imread ("52.jpg"); Mat templ = imread ("templ.jpg"); Mat result; matchTemplate (img,templ,result,TM_CCOEFF_NORMED); / / original image, template, matching method double maxVal, minVal; Point maxLoc, minLoc / / find the maximum value, the minimum value minMaxLoc (result, & minVal, & maxVal, & minLoc, & maxLoc); / / draw a rectangle. The upper left corner of the rectangle is the position of the maximum value, and the lower right corner is to add the row width and column height rectangle of the template to the maximum value (img,Point (maxLoc.x,maxLoc.y), Point (maxLoc.x+templ.cols,maxLoc.y+templ.rows), Scalar (0prit 0255), 2) / / Red color, 2 imshow ("original", img); imshow ("template", templ); imshow ("result", result); waitKey (0); return 0;}

The results are as follows, respectively, the template diagram, the original drawing (found the matching part with the template, and marked with a rectangular box, the result diagram, temporarily found a problem)

Two functions and principle explanation 1 matchTemplate () parameters detailed explanation of matchTemplate (InputArray image, InputArray templ,OutputArray result, int method)

Image: source image to be matched

Templ: template ima

Result: save the result matrix, we can use minMaxLoc () to determine the location of the maximum and minimum values of the result matrix.

Method: there are six template matching algorithms: enum {TM_SQDIFF=0, TM_SQDIFF_NORMED=1, TM_CCORR=2, TM_CCORR_NORMED=3, TM_CCOEFF=4, TM_CCOEFF_NORMED=5}

The lower the TM_SQDIFF,TM_SQDIFF_NORMED matching value, the better the matching effect, and vice versa.

TM_SQDIFF_NORMED,TM_CCORR_NORMED,TM_CCOEFF_NORMED is a standardized match, the maximum value and the minimum value range from 0 to 1, and the others need to normalize the result matrix.

Different methods will get very different results, and you can choose the most appropriate method through testing.

The smaller the minVal judged by cv::TM_SQDIFF, the better the effect is.

The variance between the template and the target image is calculated. Because it is the sum of the squares of the pixel value difference, the smaller the value, the higher the matching degree.

Cv::TM_SQDIFF_NORMED judges that the closer the minVal is to 0, the better the effect is.

Normalized cv::TM_SQDIFF. The value is between 0 and 1, and the return value of perfect match is 0.

The larger the maxVal judged by cv::TM_CCORR, the better the effect is.

Use dot product to calculate the matching degree, the higher the matching degree, the better.

Cv::TM_CCORR_NORMED judges that the closer the maxVal is to 1, the better the effect is.

Between the normalized cv::TM_CCORR,0-1, I used this.

The larger the maxVal judged by cv::TM_CCOEFF, the better the effect is.

The dot product is calculated by using the average value of the pixel of the template and the target image and the respective image. The higher the positive value, the higher the matching degree, and the larger the negative value, the greater the difference between the image. But if the image has no obvious features (that is, the pixel value in the image is close to the average value), the return value is closer to 0.

Cv::TM_CCOEFF_NORMED judges that the closer the maxVal is to 1, the better the effect is.

Between the normalized cv::TM_CCOEFF,-1 ~ 1.

2 minMaxLoc () function

Find global minimum and maximum sparse array elements and return their values and their location

Void minMaxLoc (const SparseMat& a, double* minVal,double* maxVal, int* minIdx=0, int* maxIdx=0)

A: matching result matrix

& minVal and & maxVal: the minimum and maximum values stored in the matrix result

& minLoc and & maxLoc: the coordinates of the minimum and maximum values in the result matrix.

At this point, the study of "the implementation method of opencv C++ template matching" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report