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

asked 2018-05-15 04:03:54 -0500

vettab gravatar image

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

berak gravatar image

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<<"\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];

    }
}
edit retag flag offensive close merge delete

Comments

"My template size is 610*417" -- that's huge ! (esp. if your video feed is 640x480)

you'll only have a small room left at the left/top to look for it.

berak gravatar imageberak ( 2018-05-15 04:44:52 -0500 )edit

ok, what should I give instead of AVCaptureSessionPreset640x480

vettab gravatar imagevettab ( 2018-05-15 05:19:43 -0500 )edit

sorry, i have no idea about your setup. just saying, that using a large template will take a lot of time.

are you even sure, it is fully visible in your scene, and has exactly the same size as in your video feed ?

(if not so, it won't scale by magic, it will just not detect anything)

berak gravatar imageberak ( 2018-05-15 06:16:50 -0500 )edit