Resize & Template Matching
Hi folks. I tried to use this resize function to help me in template matching, since i dont know the size of a template image on my main image, but somewhy it goes wrong and find only 2/3 same images. Still works fine with 1:1 template image. Maybe you guys can help me here? Here's the code and images C:\fakepath\table.png C:\fakepath\temp3.png
int main()
{
const int colDivisor = 21;
const int rowDivisor = 13;
int blacks[rowDivisor][colDivisor];
int blockCount = 0;
Mat imgROI;
Point pointArray[3];
int i = 0;
Mat ref = imread("C:/Dev/QtBuild/try/debug/table.png");
GaussianBlur(ref, ref, Size(5, 5), 0, 0);
Mat tpl = imread("C:/Dev/QtBuild/try/debug/temp5.png");
GaussianBlur(tpl, tpl, Size(5, 5), 0, 0);
if (ref.empty() || tpl.empty())
return -1;
Mat gref, gtpl;
cvtColor(ref, gref, CV_BGR2GRAY);
cvtColor(tpl, gtpl, CV_BGR2GRAY);
Mat res(ref.rows-tpl.rows+1, ref.cols-tpl.cols+1, CV_32FC1);
int x = 0;
int y = 0;
resize(gtpl, gtpl, Size2i(gtpl.cols-1, gtpl.rows-1));
while (true)
{
double minval, maxval, thresh = 0.8;
Point minloc, maxloc;
matchTemplate(gref, gtpl, res, CV_TM_CCOEFF_NORMED);
threshold(res, res, 0.8, 1., CV_THRESH_TOZERO);
minMaxLoc(res, &minval, &maxval, &minloc, &maxloc);
if (maxval < thresh)
{
resize(gtpl, gtpl, Size(gtpl.cols-1, gtpl.rows-1));
}
else
{
break;
}
}
matchTemplate(gref, gtpl, res, CV_TM_CCOEFF_NORMED);
threshold(res, res, 0.8, 1., CV_THRESH_TOZERO);
while (true)
{
double minval, maxval, thresh = 0.8;
Point minloc, maxloc;
minMaxLoc(res, &minval, &maxval, &minloc, &maxloc);
if (maxval >= thresh)
{
floodFill(res, maxloc, Scalar(0), 0, Scalar(.1), Scalar(1.));
if(maxloc.x < 100 && maxloc.y > 500)
{
x = maxloc.x+ gtpl.rows;
y = maxloc.y;
}
else if( maxloc.x < 100 && maxloc.y < 100)
{
x = maxloc.x ;
y = maxloc.y+ gtpl.cols;
}
else
{
x = maxloc.x ;
y = maxloc.y + gtpl.rows;
}
pointArray[i] = Point(x,y);
i++;
circle(ref, Point(x, y), 5, CV_RGB(0, 0, 255));
}
else
{
break;
}
}
}