mkto
mkto

Reputation: 4665

object detection in iphone using openCV? template matching or haar?

I need to do an iphone app that can look for a image pattern in an image. (sth like this)

After numerous google searching, i feel that the only option i have in to use template matching function in the opencv which has been ported for objectiveC.

I found an excellent starting point for a simple opencv project in objectiveC from this github code.

But it is only using edge detection and face detection features in the openCV. I need an objectiveC example that uses the template matching function - "cvMatchTemplate" - in objectiveC for iPhone?

Below is the code I have at this moment: (at least it is not giving me error, but this piece of code, return a completely black image, i am expecting a result image where matched area will be brighter?)

    IplImage *imgTemplate = [self CreateIplImageFromUIImage:[UIImage imageNamed:@"laughing_man.png"]];
    IplImage *imgSource = [self CreateIplImageFromUIImage:imageView.image];        
    CvSize sizeTemplate = cvGetSize(imgTemplate);
    CvSize sizeSrc = cvGetSize(imgSource);       
    CvSize sizeResult = cvSize(sizeSrc.width - sizeTemplate.width+1, sizeSrc.height-sizeTemplate.height + 1);
    IplImage *imgResult = cvCreateImage(sizeResult, IPL_DEPTH_32F, 1);
    cvMatchTemplate(imgSource, imgTemplate, imgResult, CV_TM_CCORR_NORMED);
    cvReleaseImage(&imgSource);
    cvReleaseImage(&imgTemplate);        
    imageView.image = [self UIImageFromIplImage:imgResult];
    cvReleaseImage(&imgResult);

p/s: Or, should I try to recognize object using cvHaarDetectObjects?

Upvotes: 3

Views: 4761

Answers (1)

jeff7
jeff7

Reputation: 2182

The result from cvMatchTemplate is a 32-bit floating point image. In order to display the result, you'll need to convert that to an unsigned char, 8-bit image (IPL_DEPTH_8U).

The CV_TM_CCORR_NORMED method produces values between [0, 1] and cvConvertScale provides an easy way to do the scaling and type conversion. Try adding the following to your code:

IplImage* displayImgResult = cvCreateImage( cvGetSize( imgResult ), IPL_DEPTH_8U, 1);    
cvConvertScale( imgResult, displayImgResult, 255, 0 );
imageView.image = [self UIImageFromIplImage:displayImgResult];

Upvotes: 5

Related Questions