Ask Your Question

Problem with template matching in SUB-IMAGE extracted from ORIGINAL-IMAGE.

asked 2013-03-18 23:59:05 -0500

Sharath gravatar image

updated 2013-03-19 00:00:22 -0500

One whole day I have tried a lot to get all the related templates (with matchtemplate function) in sub-matrix, which i have already extracted from the original image with the mousecallback function. So my code is below for the Matchingfunction,

void CTemplate_MatchDlg::OnBnTemplatematch()

{ namedWindow("reference",CV_WINDOW_AUTOSIZE); while(true) {

 Mat ref = imread("img.jpg");                    //  Original Image   
 mod_ref = cvCreateMat(ref.rows,ref.cols,CV_32F);// resizing the image to fit in picture box

   Mat tpl =imread("Template.jpg"); // TEMPLATE IMAGE  

  Mat aim=roiImg1.clone(); // SUB_IMAGE FROM ORIGINALIMAGE

     if(select_flag1 == 1)

        // imshow("ref",aim);

        Mat res(aim.rows-tpl.rows+1, aim.cols-tpl.cols+1,CV_32FC1);
                    matchTemplate(aim, tpl, res, CV_TM_CCOEFF_NORMED);
        threshold(res, res, 0.8, 1., CV_THRESH_TOZERO);

     while (1) 
    double minval, maxval, threshold = 0.8;
    Point minloc, maxloc;
    minMaxLoc(res, &minval, &maxval, &minloc, &maxloc);

   //// Draw Bound boxes for detected templates in sub matrix

    if (maxval >= threshold)
            Point(maxloc.x + tpl.cols, maxloc.y + tpl.rows), 
            CV_RGB(0,255,0), 1,8,0
        floodFill(res, maxloc, cv::Scalar(0), 0, cv::Scalar(.1), cv::Scalar(1.));
      ////Bounding box for ROI  selection with mouse
      rectangle(mod_ref, rect2, CV_RGB(255, 0, 0), 1, 8, 0);
    imshow("reference", mod_ref);

//cvReleaseMat(&mod_ref); destroyWindow("reference"); }

// Implement mouse callback

void find_mouseHandler(int event, int x, int y, int flags, void* param) { if (event == CV_EVENT_LBUTTONDOWN && !drag) { /* left button clicked. ROI selection begins*/ point1 = Point(x, y); drag = 1;


if (event == CV_EVENT_MOUSEMOVE && drag)
    /* mouse dragged. ROI being selected*/ 
    Mat img3 = mod_ref.clone();
    point2 = Point(x, y);
    rectangle(img3, point1, point2, CV_RGB(255, 0, 0), 1, 8, 0);
    imshow("reference", img3);


if (event == CV_EVENT_LBUTTONUP && drag)

    Mat img4=mod_ref.clone();
            point2 = Point(x, y);
    rect2 = Rect(point1.x,point1.y,x-point1.x,y-point1.y);
            drag = 0;
    roiImg1 = mod_ref(rect2);  //SUB_IMAGE MATRIX
        imshow("reference", img4);

if (event == CV_EVENT_LBUTTONUP)
   /* ROI selected */
    select_flag1 = 1;
    drag = 0;


build and debugging process successfully done. But, when I click the Match button in dialog I m getting the below error,

Unhandled exception at 0x74bf812f in Match.exe: Microsoft C++ exception: cv::Exception at memory location 0x001ae150..

So my idea is to get all the matches in the Sub-image when compare with the TEMPLTE IMAGE and show the final result (matches with bounding boxes) in the ORIGINAL IMAGE itself.

Anyone help me in this regard!! Help would be appreciated greatly!!

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2013-03-19 03:14:40 -0500

Be aware that clone() function creates a new pointer to the exact memory location. If you want to copy the containings of a Mat element to a new Mat element it is better to use the cvCopy() function.

This error basically can exist by two reasons:

  1. The pointer, that wants to access memory does not get access.
  2. The memory location you are pointing to doesn't exist anymore.

So basically I am guessing that in your code, you are cloning a pointer, then removing the original image by closing the function due to C++ garbage collector, creating a dangling pointer, which is then accessed.

edit flag offensive delete link more


Hi Steven , I have changed clone() function with cvCopy now it's not crashing but I am not getting any output. What I want to do is, Perform Template Matching on particular region of interest in original image by selecting ROI with mouse .If you have any idea about this regard.Please help me.Thank you!!

Sharath gravatar imageSharath ( 2013-04-01 19:57:01 -0500 )edit

You should combine functionality to set the rate of interest, combined with a mousehandler coupled to your window. Functions to use are

 namedWindow(windowname, WINDOW_AUTOSIZE);
 setMouseCallback(window_name, on_mouse);

 void on_mouse(int event,int x,int y,int flag, void* param)
    if(event==CV_EVENT_LBUTTONDOWN){ ... }
            if(event==CV_EVENT_MOUSEMOVE){ ... }

Read more about mouse handlers if you want to go with this.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-04-02 02:11:25 -0500 )edit
Login/Signup to Answer

Question Tools


Asked: 2013-03-18 23:59:05 -0500

Seen: 1,075 times

Last updated: Mar 19 '13