Monday, April 9, 2012

cvblob and opencv for ios

There are some really good tutorials and sample code out there for getting OpenCV working in ios.

All of these show how it is possible to build a static library/framework for ios.

However, I also wanted the cvblob plugin working alongside OpenCV.
Being relatively new to c++ and OpenCV, it was a struggle to get it working right away, and my solution may not be the most elegant.
Here are the steps that I followed, which may give you insight if you are doing the same thing.

Step 1: Get OpenCV working
I followed the steps here to get OpenCV built and working in my xcode project

If everything goes alright, you should have OpenCV working in xcode.

Fortunately, Khvedchenya Eugene also gives a walkthrough to get the libraries and header files working in your xcode project.

In my case, the MIN symbol was giving me trouble, even if I included OpenCV before the ios frameworks. So I cheated and replaced all instances of "MIN" with "CVMIN" in my OpenCV header files.

In my project, I also added some source from Yoshimasa Niwa for converting between IpImage and UIImage.

By the way, here's what I have for included opencv libraries.

Step 2 (FAILED): Build cvblob library
After making sure the OpenCV is working as it should, it is time to import cvblob. Initially I tried to create a library for cvblob by modifying Khvedchenya Eugene's build script. Essentially, I just copied the "ios" folder from the opencv source to the cvblob source, and added
-DOpenCV_DIR=/path/to/compiled/opencv \
to the arguments in each build command in the build script.
However, there were a couple of problems. First, the current version of cvblob uses dynamically linked libraries, which aren't supported in ios. This can be mitigated by using an earlier (static) version of cvblob, like this one:
Also, everything in cvblob/tests/CMakeLists has to be commented out, since there are some things in the test files that aren't supported in ios.

With these changes, I was able to build a library. However, the linker was giving me errors when I compiled my ios project, forcing me to temporarily abandon the cvblob library idea.

Step 2 (SUCCEEDED): Add cvblob source to project
Instead of building a cvblob library, I decided to import the cvblob source directly into my project. Download the latest version of cvblob:

Put the source somewhere accessible, and add the cvBlob subfolder to the header search path.

Put the following code at the top of the file where you will use cvblob.

And compile. You'll see this error:

There's a problem with the cvSaveImageBlob in cvblob.cpp (I think this was the cause of the library build not working either). Comment out the contents of cvBlob/cvblob.cpp::cvSaveImageBlob and try to compile again.

This time it should succeed. If it did, you can now play with cvblobs inside ios. Yay!

Since I found the problem with the cvSaveImageBlob, I'd like to try and build the library again when I get the time. I'll also be blogging again if I can actually get my project idea working.

You can see the project source code here.

Hope it helps!