Using OpenCV in Objective-C code
After publishing Building OpenCV for iOS article many of readers asked me how to use OpenCV within ObjectiveC code, because they encountered compilation errors. In this post I’ll show you how to use OpenCV and ObjectiveC to make some image processing.
This post is outdated
Current version of OpenCV supports iOS toolchain and can be build in much easier way. You are welcome to read this post for educational purposes. It's 99.9% chance that you will not be able to use OpenCV as described here. Please use my OpenCV Tutorial project if you want to learn how to use OpenCV on iOS platform.
Please use my OpenCV Tutorial project if you want to learn how to use OpenCV on iOS platform.
In this post I’ll use GLImageProcessing sample demo from Apple. Also you will need precompiled OpenCV for iPhone. How to make it read here. I’ve copied all OpenCV stuff to “opencv” folder into the GLImageProcessing. Here is project directory structure:
Now, let’s add OpenCV to our project. **First step - **specify the place, where the OpenCV headers are located. Add the path to OpenCV headers to the “Header Search Paths” property:
Second step - set up path where the OpenCV libraries are. Please note: you will need to repeat this step for each configuration (debug/release, device/simulator) because of directory structure of the OpenCV libs. Add path of the OpenCV libraries to the “Library Search Paths” property (for release device this will be “opencv/lib/iphoneos/Release” if you follow directory structure listed above). Third step: Tell linker to include OpenCV to the output executable:
As you can see i prefer use linker flags instead using XCode UI (Targets -> GLImageProcessing -> Link Binary With Libraries -> Add -> Existing Files…). There are several reasons: i prefer this way because i don’t trust XCode :) (Actually because for all my projects i use cross-platform build system which allows to do such things much easies, but this is a topic for another article). Now, add #include <opencv2/opencv.hpp> to header files . I’ve added this include to Imaging.h header right after #include <OpenGLES/ES1/glext.h>. Crossing fingers and hitting “Build and Run”…..Oooops, compilation errors. Let’s see, what we can do with all those errors. First of all - change extensions of all .m files which include OpenCV headers to .mm. This tells compiler that such file contains mixed ObjectiveC and C++ code. Also change extensions of all .c files to .cpp, because OpenCV uses STL, which is absent for pure C and thus you’ll get compilation errors. Now, only one compilation error left:
|
|
These errors are caused by MIN symbol, which is a macro in OpenCV and a function, defined somewhere in UIKit or other frameworks..Simple solution - include OpenCV before iOS frameworks. So…with all these fixes all builds fine. Now it’s time to add some OpenCV code.. Let’s change the matrix multiplication code! File Imaging.cpp
|
|
A little crappy code, isn’t it? Let’s rewrite it!
|
|
A bit of comments. cv::Mat is a type of OpenCV matrix. We initialize a matrix view, which takes data from the float4 array, but not copy it. Result of multiplication of two views (a and b) assigned to view c which is a result. Hit “Build and Run”, and here we go:
This is a very simple example which demonstrates that you can use OpenCV even with ObjectiveC without any problems. Good luck in your projects!