0

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 ?

3 Answers 3

2

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;
    }
    
Sign up to request clarification or add additional context in comments.

2 Comments

I believe I have managed to link my project with static library.the code example has clear things up. many thanks for the explanation
Objective C is not a superset of C++, it's a superset of C. To use C++ code, you have to rename your file with extension .mm
0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.