Blobs by OpenCV and CoreVideo
October 22nd, 2008

I was recently in need of a clean and simple mechanism for doing realtime blob detection using OpenCV. One technique that would at least get me halfway there would be to use something like OpenFrameworks, which contains a sample project for this purpose. However, when writing applications that have no reason to run on anything other than a particular OS (in my case, OS X), it makes sense to leverage as much of the native OS APIs as possible for performance. In addition, I was also drawn to the possibility of being able to take the video frames and pre-process them with some CoreImage filters before sending them to OpenCV.
The right answer is to use CoreVideo in conjunction with OpenCV to accomplish this task. Unfortunately, I found a number of discussions on the web asking how to accomplish precisely this task — apparently with little success. To be honest, CoreVideo still scares me a bit, but, according to Apple, it’s the best way to deal with video on OS X. I suspect the main reason that I couldn’t find an existing solution to a relatively simple problem is mainly due to the complexity of the CoreVideo APIs compounded with the issue that OpenCV can be challenging to compile for OS X.
Surprisingly, this task is incredibly simple. In less than 200 lines of Objective-C, I’ve got a CoreVideo-based OS X application that does blob detection using OpenCV. My performance concerns were also justified, as it uses ~10% CPU to accomplish the same task the OpenFrameworks does with nearly 80%. The only downside is that even though I’ve been able to inject OpenCV into the CoreVideo pipeline and obtain some great results, there doesn’t appear to be any way to insert CoreImage into this pipeline as well. The benefit of CoreImage filters, of course, is that they are running on the GPU. This turns out to be a double-edged sword however, as the only way to get the pixels back from the GPU (or wherever Apple keeps them), is to copy them back out. And copying is sloooow.
October 23rd, 2008 at 5:42 am
just a quick defense of OF – I think you have the cpu performance comparison wrong — openframeworks uses GLUT, which tries to run as fast as possible, so your cpu usage will always be high, no matter if you are drawing nothing (and thus having a mega high frame rate) or alot. A better metric of how hard the app is working is frame rate, which you can query.
October 23rd, 2008 at 5:43 am
I think you have the cpu usage wrong – OF uses glut which tries to run as fast as possible — therefore, it will always have high cpu usage amounts no matter if you are drawing a little or a lot. – z
October 25th, 2008 at 2:08 pm
[...] is to use OpenCV, which doesn’t yet have a particularly stable home on OS X. As mentioned in my blog, my task was to find out how to use the most appropriate OS X technologies (in this case, [...]
December 17th, 2008 at 1:27 pm
Howdy there,
I’m learning OpenCV, and would love to work in Objective-C, but haven’t seen any examples of OpenCV in Objective-C. Can you throw a brother those 200 lines you worked out, or refer me to a greater source of knowledge? Any help would be much appreciated. These first few steps in a new direction are always slow and timid.
December 17th, 2008 at 4:48 pm
My response can be found here:
http://blog.buzamoto.com/2008/10/25/the-cross-platform-tax/#comments
December 17th, 2008 at 6:37 pm
Hi Ben,
The example project can be found here:
http://wiki.buzamoto.com/cv_opencv
I hope it helps!
March 10th, 2010 at 1:12 am
Most helpful. Thank you!