Mujtaba Alboori
Mujtaba Alboori

Reputation: 395

How to use OpenCV/or any ported library classes in Objective-C

First, I apology for this newbie question. I'm new in Objective C and OpenCV

the normal method declaration in Objective-C is like that

Function functionName = [[Function alloc] init];

but when I use OpenCV class it says (for example CvMat) receiver type is not an Objective C class.

or am I suppose to write the code in C++ syntax ?

Upvotes: 0

Views: 1147

Answers (3)

Robin Summerhill
Robin Summerhill

Reputation: 13675

Take a look at this article that shows how to use OpenCV on the iPhone. It provides an OpenCV framework that can be dropped into your own projects and supports video capture too.

Upvotes: 0

Jordan Smith
Jordan Smith

Reputation: 10378

You write the code in C++ syntax. Objective-C is actually a superset of C++ which means that any C++ program is also a valid Objective-C program (well in most cases anyway). Objective-C just adds a whole lot of functionality to what already exists in C++.

When I was developing an openCV app for iOS, here were the main stumbling blocks:

  • Compiling OpenCV as a static library. It's the only way to use OpenCV in iOS, and is not an easy task if you've never done anything similar before. There are a couple of great blog posts about how to do it, such as this one.

  • Armv6 and armv7 - make sure you have static libraries compiled for both (or a universal binary), as iOS runs on both.

  • When you're coding, just code as you would for c++. Here's a chunk of example code you can refer to.

    // NOTE you SHOULD cvReleaseImage() for the return value when end of the code.
    - (IplImage *)CreateIplImageFromUIImage:(UIImage *)image {
      // Getting CGImage from UIImage
      CGImageRef imageRef = image.CGImage;
    
      CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
      // Creating temporal IplImage for drawing
      IplImage *iplimage = cvCreateImage(
        cvSize(image.size.width,image.size.height), IPL_DEPTH_8U, 4
      );
      // Creating CGContext for temporal IplImage
      CGContextRef contextRef = CGBitmapContextCreate(
        iplimage->imageData, iplimage->width, iplimage->height,
        iplimage->depth, iplimage->widthStep,
        colorSpace, kCGImageAlphaPremultipliedLast|kCGBitmapByteOrderDefault
      );
      // Drawing CGImage to CGContext
      CGContextDrawImage(
        contextRef,
        CGRectMake(0, 0, image.size.width, image.size.height),
        imageRef
      );
      CGContextRelease(contextRef);
      CGColorSpaceRelease(colorSpace);
    
      // Creating result IplImage
      IplImage *ret = cvCreateImage(cvGetSize(iplimage), IPL_DEPTH_8U, 3);
      cvCvtColor(iplimage, ret, CV_RGBA2BGR);
      cvReleaseImage(&iplimage);
    
      return ret;
    }
    

Upvotes: 2

Igor Konoplyanko
Igor Konoplyanko

Reputation: 9374

Yes. You should use special factory functions.

For example CvMat can be created by CvMat* cvCreateMat(int rows, int cols, int type);

For more info use documentation

Upvotes: 0

Related Questions