La coincidencia de varias plantillas solo detecta una coincidencia
Estoy tratando de hacer coincidir esta imagen.
en esta imagen
Sin embargo, no puedo encontrar más de un jefe enemigo. ¿Qué necesito hacer para encontrar a los demás?
Carga de imagen
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);
Comparación de plantillas
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;
}