Search code examples
iphonexcode4opencvcomputer-vision

iphone opencv - template matching


i've implemented this OpenCV build into my iphone project:

http://aptogo.co.uk/2011/09/opencv-framework-for-ios/

It builds successfully and i know how to capture an image with my camera, grayscale it and show it on an imageview.

But what i'd like to do is templatematching with the image that i captured (with the camera) against an template image.

I have both images grayscaled, but i just don't get how the 'matchTemplate' works. This is what i have so far:

cv::Mat grayFrame, grayImg, output;

// This is the template image which i store in a cv::Mat and
// after that i grayscale the image
UIImage *testImage = [UIImage imageNamed:@"qr.png"];
cv::Mat tempMat = [testImage CVMat];
cv::cvtColor(tempMat, grayImg, cv::COLOR_RGB2GRAY);


// Convert captured frame to grayscale
cv::cvtColor(_lastFrame, grayFrame, cv::COLOR_RGB2GRAY);

// Having trouble here...
cv::matchTemplate(grayFrame, grayImg, output, CV_TM_CCORR_NORMED);


// Display result
// This already works for both the captured image and the template image
camView.image = [UIImage imageWithCVMat:grayFrame];

The problem with this code is, is this line:

cv::matchTemplate(grayFrame, grayImg, output, 1);

I'm not sure if i'm doing it right. Because i don't get a result back in 'output' like "MinMax" to get the positions of the found match... Am i even passing the right variables to the matchTemplate function...??

Anyway, can someone please tell me what i'm doing wrong and how to make this work?

  • edit - Added a screenshot of my variable 'output' enter image description here

Thanks for any help!


Solution

  • The matchTemplate() function returns a matrix whose values represent the probability of the match on every pixel of the original image. It contains floating-point data and has the same size (width/height as the input). To extract the position of the most likely match, you must run the minMaxLoc() function over the result.