Ask Your Question

Revision history [back]

Hi , I am not able to match the template with any of the template matching methods

Here is my code. I am using iPhone 6S for testing this functionality. Please correct me if any thing is missing the code and to match the templates correctly. Also please let me know how can I save the captured image into my device or Mac?

My template size is 610*417

videoCamera = [[CvVideoCamera alloc] initWithParentView:imageView]; videoCamera.defaultAVCaptureDevicePosition = AVCaptureDevicePositionBack; // Use the back camera videoCamera.defaultAVCaptureSessionPreset = AVCaptureSessionPreset640x480; videoCamera.defaultAVCaptureVideoOrientation = AVCaptureVideoOrientationLandscapeLeft; // Ensure proper orientation videoCamera.rotateVideo = YES; // Ensure proper orientation videoCamera.defaultFPS = 30; // How often 'processImage' is called, adjust based on the amount videoCamera.delegate = self;

  • (void)processImage:(cv::Mat &)img {

    cv::Mat gimg; // Convert incoming img to greyscale to match template cv::cvtColor(img, gimg, CV_BGR2GRAY);

    // Check for matches with a certain threshold to help with scaling and angles cv::Mat res(gimg.rows-gtpl.rows+1, gimg.cols-gtpl.cols+1, CV_32FC1); cv::matchTemplate(gimg, gtpl, res, CV_TM_SQDIFF_NORMED);

    //cv::matchTemplate(gimg, gtpl, res, CV_TM_SQDIFF_NORMED); // cv::threshold(gimg, gimg, 127, 255, CV_THRESH_TOZERO); //cv::threshold(res, res, 0.5, 1., CV_THRESH_TOZERO);

    //cv::normalize( res, res, 0, 1, NORM_MINMAX, -1, Mat() );

    double minval, maxval; // double threshold = 0.15; cv::Point minloc, maxloc, matchLoc; cv::minMaxLoc(res, &minval, &maxval, &minloc, &maxloc);

    cout << "min: " << minval<<" maxval "<<maxval&lt;&lt;"\n"; cout="" &lt;&lt;="" "minloc:="" "="" &lt;&lt;="" minloc.x&lt;&lt;"="" maxloc="" "&lt;<maxloc;<="" p="">

    // If it's a good enough match if (minval <0.25) {

    // Draw a rectangle for confirmation
    cv::rectangle(img, maxloc, cv::Point(maxloc.x + gtpl.cols, maxloc.y + gtpl.rows), CV_RGB(0,255,0), 2);
    cv::floodFill(res, maxloc, cv::Scalar(0), 0, cv::Scalar(.1), cv::Scalar(1.));
    
    // Call our delegates callback method
    [delegate matchedItem];
    

    } }

click to hide/show revision 2
None

updated 2018-05-15 04:04:57 -0600

berak gravatar image

Hi , I am not able to match the template with any of the template matching methods

Here is my code. I am using iPhone 6S for testing this functionality. Please correct me if any thing is missing the code and to match the templates correctly. Also please let me know how can I save the captured image into my device or Mac?

My template size is 610*417

610*417 videoCamera = [[CvVideoCamera alloc] initWithParentView:imageView]; videoCamera.defaultAVCaptureDevicePosition = AVCaptureDevicePositionBack; // Use the back camera videoCamera.defaultAVCaptureSessionPreset = AVCaptureSessionPreset640x480; videoCamera.defaultAVCaptureVideoOrientation = AVCaptureVideoOrientationLandscapeLeft; // Ensure proper orientation videoCamera.rotateVideo = YES; // Ensure proper orientation videoCamera.defaultFPS = 30; // How often 'processImage' is called, adjust based on the amount videoCamera.delegate = self;

  • self; - (void)processImage:(cv::Mat &)img {

    { cv::Mat gimg; // Convert incoming img to greyscale to match template cv::cvtColor(img, gimg, CV_BGR2GRAY);

    CV_BGR2GRAY); // Check for matches with a certain threshold to help with scaling and angles cv::Mat res(gimg.rows-gtpl.rows+1, gimg.cols-gtpl.cols+1, CV_32FC1); cv::matchTemplate(gimg, gtpl, res, CV_TM_SQDIFF_NORMED);

    CV_TM_SQDIFF_NORMED); //cv::matchTemplate(gimg, gtpl, res, CV_TM_SQDIFF_NORMED); // cv::threshold(gimg, gimg, 127, 255, CV_THRESH_TOZERO); //cv::threshold(res, res, 0.5, 1., CV_THRESH_TOZERO);

    CV_THRESH_TOZERO); //cv::normalize( res, res, 0, 1, NORM_MINMAX, -1, Mat() );

    ); double minval, maxval; // double threshold = 0.15; cv::Point minloc, maxloc, matchLoc; cv::minMaxLoc(res, &minval, &maxval, &minloc, &maxloc);

    &maxloc); cout << "min: " << minval<<" maxval "<<maxval&lt;&lt;"\n"; cout="" &lt;&lt;="" "minloc:="" "="" &lt;&lt;="" minloc.x&lt;&lt;"="" maxloc="" "&lt;<maxloc;<="" p="">

    "<<maxval<<"\n"; cout << "minloc: " << minloc.x<<" maxloc "<<maxloc; // If it's a good enough match if (minval <0.25) {

     {
    // Draw a rectangle for confirmation
     cv::rectangle(img, maxloc, cv::Point(maxloc.x + gtpl.cols, maxloc.y + gtpl.rows), CV_RGB(0,255,0), 2);
     cv::floodFill(res, maxloc, cv::Scalar(0), 0, cv::Scalar(.1), cv::Scalar(1.));
     // Call our delegates callback method
     [delegate matchedItem];
    
    }
    }
    

    } }