Multiple template matching only detects one match

有些话、适合烂在心里 提交于 2019-12-04 02:23:52

问题


I'm trying to match this image

in this image

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

Image Loading

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);

Template Matching

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;
}

回答1:


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 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;
}

Result:



来源:https://stackoverflow.com/questions/32041063/multiple-template-matching-only-detects-one-match

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!