Iterate over cv :: Points contained in cv :: Mat

I am using OpenCV pattern matching to find an image in another image.

In particular, matchTemplate() , which returns cv::Mat containing the match map of matches.

Is there a way to sort through cv::Point contained in this cv::Mat , also using minMaxLoc() ?

  minMaxLoc(result, &minVal, &maxVal, &minLoc, &maxLoc); 

I tried:

  cv::Mat_<uchar>::iterator it = result.begin<uchar>(); cv::Mat_<uchar>::iterator end = result.end<uchar>(); for (; it != end; ++it) { cv::Point test(it.pos()); } 

With limited success.

+4
source share
1 answer

If you understand correctly, you want to sort the pixels by their coincidence, and then get the points corresponding to these pixels in sorted order. You can do this by changing the result one line, and then call cv::sortIdx() to get the pixel indices in sorted cv::sortIdx() you can use the indices as offsets from the beginning of the result to get the position of that particular pixel.

UPDATE: I noticed one possible problem in your code. It looks like you think that result contains uchar data. cv::matchTemplate() requires that result contain float data.

 cv::Mat_<int> indices; cv::sortIdx(result.reshape(0,1), indices, CV_SORT_DESCENDING + CV_SORT_EVERY_ROW); cv::Mat_<float>::const_iterator begin = result.begin<float>(); cv::Mat_<int>::const_iterator it = indices.begin(); cv::Mat_<int>::const_iterator end = indices.end(); for (; it != end; ++it) { cv::Point pt = (begin + *it).pos(); // Do something with pt... } 
+3
source

Source: https://habr.com/ru/post/1494538/


All Articles