Nov 28, 2011

Using OpenCV on the iPhone

In my project, I'm doing a lot of image processing on the GPU, however, we still need OpenCV to do a lot of stuff. Here I'm writing down my experience of using OpenCV on the iPhone:

Building OpenCV 2.2 with Xcode 4 and iOS SDK 4.3
OpenCV 2.2 started to support Android SDK, but not iPhone. So we have to build it on our own. As iOS SDK doesn't support dynamic ".framework", the solution is to build it as a static ".a" lib file, and link it against the app statically

1. Install CMake. I used Homebrew as a package manager. I highly recommend it. As compared to Macports, it's much cleaner and more elegant.

2. Download OpenCV 2.2 from http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.2/OpenCV-2.2.0.tar.bz2/download

3. xjvf OpenCV-2.2.0.tar.bz2

4.
% cd OpenCV-2.2.0
% patch -p1 < ../OpenCV-2.2.0.patch

5.

% cd .. # Back to the top of demo project directory.
% mkdir build_simulator
% cd build_simulator
% ../opencv_cmake.sh Simulator ../OpenCV-2.2.0
% make -j 4
% make install

6.

% cd .. # Back to the top of demo project directory.
% mkdir build_device
% cd build_device
% ../opencv_cmake.sh Device ../OpenCV-2.2.0
% make -j 4
% make install

Using OpenCV library in the project
Actually it is more complicated than I thought. I googled for a long time and finally got it working.

Add libopencv_core.a etc, from OpenCV lib directory for either simulators or devices. Actually Xcode doesn’t care which one is for devices or simulators at this point because it is selected by the library search path.
Add Accelerate.framework which is used internally from OpenCV library.
Select your active build target, then open the build tab in the info panel by Get Info menu.
Add -lstdc++ and -lz to Other Linker Flags
Add path to OpenCV include directory to Header Search Paths for both simulators and devices.
Add path to OpenCV lib directory to Library Search Paths for both simulators and devices.

Ok. For now we're happy to use OpenCV on the iPhone!