For the whole day, I tried a lot to get all the associated matches (with the matchtemplate function) in the sub-Image, which is the ROI that I already extracted from the original image using the mousecallback function. So my code below is for Matchingfunction
////Matching Function 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 resize(ref,mod_ref,Size(),0.5,0.5,CV_INTER_AREA); Mat tpl =imread("Template.jpg"); // TEMPLATE IMAGE cvSetMouseCallback("reference",find_mouseHandler,0); Mat aim=roiImg1.clone(); // SUB_IMAGE FROM ORIGINALIMAGE // aim variable contains the ROI matrix // next, want to perform template matching in that ROI // and display results on original image 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) { rectangle( aim, maxloc, 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.)); }else break; } } ////Bounding box for ROI selection with mouse rectangle(mod_ref, rect2, CV_RGB(255, 0, 0), 1, 8, 0); // rect2 is ROI // my idea is to get all the matches in ROI with bounding boxes // no need to mark any matches outside the ROI //Clearly i want to process only ROI imshow("reference", mod_ref); // show the image with the results waitKey(10); } //cvReleaseMat(&mod_ref); destroyWindow("reference"); } /// ImplementMouse Call Back 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); rect1 = Rect(point1.x,point1.y,x-point1.x,y-point1.y); drag = 0; roiImg1 = mod_ref(rect1); //SUB_IMAGE MATRIX imshow("reference", img4); } if (event == CV_EVENT_LBUTTONUP) { /* ROI selected */ select_flag1 = 1; drag = 0; } }
The build and debugging process completed successfully. But when I click the βMatchβ button in the dialog box, I get an error message:
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 comparing with TEMPLATE IMAGE and show the final result (matching with the bounding boxes) in the ORIGINAL IMAGE itself.
Someone help me in this regard! Help would be greatly appreciated.