Ask Your Question
0

Resize & Template Matching

asked 2015-05-21 01:35:55 -0600

Storiy gravatar image

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;
        }
    }
}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2015-05-22 05:07:39 -0600

Eduardo gravatar image

I tried very quickly your code and it seems that:

  • the detection of the top right template is missing
  • I think that the threshold (0.8) in your first while loop is too low: I don't know why but at the scale where there are hits at thresh=0.8, the correlation for the top right zone is a little lower than 0.8 and you miss this detection
  • check if at the scale where you have hits at 0.8, the template scale is the same than in the reference image
  • if you choose 0.85 in the first while loop, you will have the 3 detections
  • I may be wrong but the match template between the 2 while loops is useless for me ?

Hope this help you a little bit.

edit flag offensive delete link more

Comments

yeah, it just a test code, so i missed a lot of things while trying to make it work. i knew that top right was missing, but couldnt understand why. thanks a lot for those advices!

Storiy gravatar imageStoriy ( 2015-05-26 12:23:36 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-05-21 01:35:55 -0600

Seen: 2,073 times

Last updated: May 22 '15