c++ - Multiple template matching only detects one match -


i'm trying match image

enter image description here

in image

enter image description here

however, can't find more 1 boss enemy. need find others?

image loading

struct xyposition{   float x;   float y; };  std::vector<cv::mat> bosslist; std::string bossstrings[1] = { "sprites\\boss\\bossup.png" }; (int = 0; < 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 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; } 

i didn't debug code, since doesn't work (probably floodfill messing result matrix), simple working sample.

i iterate on maximum points in result matrix finding blobs values on threshold, , finding highest value within each blob (used mask retrieve actual values in 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);      (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:

enter image description here


Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -