2014/03/27

OpenCV sample code does not work in iOS

I have been trying to implement some samples of OpenCV to know what OpenCV can do. But some code did not work as I expected.

(1)making CV_8UC3 cv::Mat from UIImage
One sample code needed an image of CV_8UC3, but my function made CV_8UC4 from a UIImage object.
There may be many ways to avoid this fail, I found an easiest way here :
objective c - CGBitmapContextCreate for CV_8UC3 (to use in OpenCV) - Stack Overflow
After I converted an image to cv::Mat (CV_8UC3), I converted it again :
cv::cvtColor(img , img , CV_RGBA2RGB);
And it worked.

(2)load xml files
When I tried to detect a face by OpenCV, I had to load a classifier xml file. I didn’t know how to do it, finally, I brought the xml file to Xcode (only reference) and like this :
{
    NSString* resDir = [[NSBundle mainBundle] resourcePath];
    const char *cascade_name = "haarcascade_frontalface_alt.xml";
    char cascade_path[PATH_MAX];
    sprintf(cascade_path, "%s/%s", [resDir cStringUsingEncoding:NSASCIIStringEncoding], cascade_name );
    cv::CascadeClassifier cascade;
    if(!cascade.load(cascade_path)) {
        NSLog(@"loading failed");
        return;
    }
}
And it also worked.
Develop | Comments(1) | Trackback(0)
Comment
Comment is pending approval.
Comment is pending blog author's approval.

管理者のみに表示