多个模板匹配仅检测一个匹配 [英] Multiple template matching only detects one match

查看:692
本文介绍了多个模板匹配仅检测一个匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试匹配此图片

但是,我可以找不到一个老板的敌人。我需要做什么来找到其他人?

However, I can't find more than one boss enemy. What do I need to do to find the others?

图片载入

struct XYposition{
  float X;
  float Y;
};

std::vector<cv::Mat> bossList;
std::string bossStrings[1] = { "sprites\\boss\\bossUp.png" };
for (int i = 0; i < 1; i++){
    cv::Mat pic = cv::imread(bossStrings[i], CV_LOAD_IMAGE_GRAYSCALE);
    bossList.push_back(pic);
}

multipleTemplateMatch(screenImage, bossList);

模板匹配

std::vector<XYposition> multipleTemplateMatch(cv::Mat &img, std::vector<cv::Mat> tplList){

std::vector<XYposition> matches;

cv::Mat convertImg(img.rows, img.cols, CV_8UC3);
cv::cvtColor(img, convertImg, CV_BGRA2GRAY);

double threshold = 0.8;

int imgint = convertImg.type();

for(cv::Mat tpl : tplList){
    int tplint = tpl.type();
    cv::Mat result(convertImg.rows - tpl.rows + 1, convertImg.cols - tpl.cols + 1,
        CV_32FC1); //must be this result type


    cv::matchTemplate(convertImg, tpl, result, CV_TM_CCOEFF_NORMED);
    cv::threshold(result, result, threshold, 1., CV_THRESH_TOZERO);

    while (true)
    {
        double minval, maxval;
        cv::Point minloc, maxloc;
        cv::minMaxLoc(result, &minval, &maxval, &minloc, &maxloc);
        if (maxval >= threshold)
        {
            rectangle(result, maxloc, cv::Point(maxloc.x - tpl.cols, maxloc.y - tpl.rows),
                cv::Scalar(0, 0, 255), 4, 8, 0);
            cv::floodFill(result, maxloc, cv::Scalar(0), 0, cv::Scalar(.1), cv::Scalar(1.));

            XYposition info = {
                maxloc.x - ceil(tpl.cols / 2), maxloc.y - ceil(tpl.rows / 2)
            };
            matches.push_back(info);
        }
        else
            break;
    }
}

return matches;
}


推荐答案

代码,但由于它不工作(可能 floodfill 正在弄乱你的结果矩阵),这是一个简单的工作示例。

I didn't debug your code, but since it doesn't work (probably floodfill is messing up your result matrix), this is a simple working sample.

I遍历结果矩阵中的最大点,找到值超过阈值的块,并在每个块内找到最大值(用作掩码以检索结果矩阵中的实际值)。



I iterate over the maximum points in the result matrix finding the blobs where values are over a threshold, and finding the highest value within each blob (used as a mask to retrieve actual values in the result matrix).

#include <opencv2\opencv.hpp>
#include <vector>
using namespace std;
using namespace cv;

int main()
{
    Mat3b img = imread("path_to_image");
    Mat3b templ = imread("path_to_template");

    Mat1b img_gray;
    Mat1b templ_gray;
    cvtColor(img, img_gray, COLOR_BGR2GRAY);
    cvtColor(templ, templ_gray, COLOR_BGR2GRAY);

    Mat1f result;
    matchTemplate(img, templ, result, TM_CCOEFF_NORMED);

    double thresh = 0.7;
    threshold(result, result, thresh, 1., THRESH_BINARY);

    Mat1b resb;
    result.convertTo(resb, CV_8U, 255);

    vector<vector<Point>> contours;
    findContours(resb, contours, RETR_LIST, CHAIN_APPROX_SIMPLE);

    for (int i=0; i<contours.size(); ++i)
    {
        Mat1b mask(result.rows, result.cols, uchar(0));
        drawContours(mask, contours, i, Scalar(255), CV_FILLED);

        Point max_point;
        double max_val;
        minMaxLoc(result, NULL, &max_val, NULL, &max_point, mask);

        rectangle(img, Rect(max_point.x, max_point.y, templ.cols, templ.rows), Scalar(0,255,0), 2);
    }
    return 0;
}

结果:

这篇关于多个模板匹配仅检测一个匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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