无法进行N个最佳匹配的OpenCV模板匹配 [英] OpenCV Template Matching for N best matches not working

查看:201
本文介绍了无法进行N个最佳匹配的OpenCV模板匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为此苦苦挣扎了很长一段时间,似乎在这里找不到问题.让我尝试引导您完成整个过程.

I have been struggling with this for quite a long while and can't seem to find the problem here. Let me try to walk you through the process.

我正在尝试为模板匹配获得10个最佳边界框.

I am trying to obtain the 10 best bounding boxes for my template matching.

这里不会显示整个图像,但是这是我的cv::mat,当我循环遍历cv::mat以搜索最低值(结果)

Not going to show the entire image here, but here's my cv::mat when I loop through the cv::mat to search for the lowest values (results)

 int a,b;
    for ( a = 0; a < final_image_height; a++){
      for ( b = 0; b < final_image_width; b++){
        if (result_scores_mat.at<float>(a, b) < 70 ){
          printf("%.2f ", result_scores_mat.at<float>(a, b));
        }
      }
    }

这给了我一些cv::mat中带有较低值"的值

This gives me some values present in the cv::mat with "lower values"

68.50 68.93 54.50 68.92 64.62 57.12 62.69 65.86 63.52 68.35 68.65 61.93 69.18 67.69 

然后进入一个循环,通过该循环调用minMaxLoc()来找到minVal (using TM_SQDIFF, so minVal)

I then move into a loop whereby I called minMaxLoc() to find the minVal (using TM_SQDIFF, so minVal)

for ( i = 0; i < 10; i++){
      minMaxLoc(result_scores_mat, &minVal, &maxVal, &minLoc, &maxLoc, cv::Mat());

      cout<<minVal<<endl;

      // for match_method TM_SQDIFF we take lowest values
      matchLoc = minLoc;
      drawRadius = result_radius_mat.at<float>(matchLoc.x, matchLoc.y); 

      // display source image and result matrix , draw rectangle around highest possible matching area
      cv::rectangle( img_display_mat, matchLoc, cv::Point( matchLoc.x + 2*drawRadius, matchLoc.y + 2*drawRadius), cv::Scalar::all(255), 2, 8, 0);  

      result_scores_mat.at<float>(minLoc.x, minLoc.y)= 255;
    }

我在最后一行中所做的是替换minLoc处的float值,以便我在minMaxLoc中运行的下一个循环找不到该点,并在cv::mat.

What I am doing in the last line is replacing the value of the float at the minLoc, so that the next loop I run through minMaxLoc doesn't locate that point and gives me the next minimum value in the cv::mat.

但是,我一直通过cout<<minVal<<endl;

54.5003
54.5003
54.5003
54.5003
54.5003
54.5003
54.5003
54.5003
54.5003
54.5003

它正确检测到cv::mat中存在的54.50,但我希望循环中所有最小值.

It detects rightly the 54.50 that is present in the cv::mat, but I want all the minimum values in the loop.

我在做错什么吗,还是解决这个问题?非常感谢!

Is there something wrong I'm doing, or any way around this? Thank you so much!

推荐答案

似乎您以错误的方式用255替换了最小值,这意味着您正确检测了最小值,但是在错误的位置替换了255,应该在行下方替换

It seems that you are replacing minimum value with 255 in wrong way, it means that you detect minimum value correctly, but you replace 255 in wrong position, you should replace below line

result_scores_mat.at<float>(minLoc.x, minLoc.y)= 255;

通过此行:

result_scores_mat.at<float>(minLoc.y, minLoc.x)= 255;

要知道为什么我们要互相替换此行,可以看一下

For knowing that why we replace this lines by each other, you can take a look at the source code of .at method.It is like below

template<typename _Tp> _Tp& at(int row, int col);
/** @overload
@param row Index along the dimension 0
@param col Index along the dimension 1
*/

这篇关于无法进行N个最佳匹配的OpenCV模板匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆