Jak mogę wykonać proces dopasowania szablonu w SUB-IMAGE wyodrębnionym z ORIGINAL-IMAGE i wyświetlić wyniki w Original Image

Cały dzień wiele próbowałem uzyskać wszystkie powiązane dopasowania (z funkcją matchtemplate) w podobrazie, czyli ROI, które już wyodrębniłem z oryginalnego obrazu za pomocą funkcji powrotu do myszy. Więc mój kod jest poniżej dla funkcji dopasowania

 ////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;
}
}

pomyślnie wykonano proces budowania i debugowania. Ale po kliknięciu przycisku Dopasuj w oknie dialogowym pojawia się błąd:

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

Więc moim pomysłem jest uzyskanie wszystkich dopasowań w podobrazie podczas porównywania ze OBRAZEM SZABLONU i pokazanie końcowego wyniku (pasuje do obwiedni) w samym ORYGINALNYM OBRAZIE.

Ktoś mi pomoże w tym zakresie !! Pomoc będzie bardzo mile widziane !!

questionAnswers(1)

yourAnswerToTheQuestion