多个模板匹配仅检测一个匹配

时间:2015-08-16 23:49:30

标签: c++ opencv image-processing template-matching

我正在尝试匹配此图片

enter image description here

在此图片中

enter image description here

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

图片加载

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

1 个答案:

答案 0 :(得分:3)

我没有调试你的代码,但由于它不起作用(可能floodfill搞乱你的结果矩阵),这是一个简单的工作样本。

迭代结果矩阵中的最大点,查找值超过阈值的blob,并找到每个blob中的最高值(用作掩码以检索结果矩阵中的实际值)。

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

结果:

enter image description here