Loading a Haar Classifier with OpenCV in Mac OS X without hassle

OpenCV is a great library, and there are a lot of examples around the Internet, and while many of them date back to the OpenCV 1.x release (which is not compatible with OpenCV 2.x), you can just install and link against the 1.x release without any problem.

On the other hand, you may face some trouble getting the examples to compile and / or run correctly, depending of how you installed OpenCV and the example source code you are looking at. In my case I installed the library in my Mac OS X using homebrew, which installs both the 1.x and 2.x versions, with the includes at /usr/local/include and libraries at /usr/local/lib. Then, just import prefixing <opencv/….> or <opencv2/….>, like

#include <opencv/cv.h>
#include <opencv/highgui.h>

Another problem I faced was when trying to load a Haar Classifier, which should be a very simple operation, but for some reason it fails miserably with the error

OpenCV Error: Unspecified error (The node does not represent a user object (unknown type?)) in cvRead, file /tmp/homebrew-opencv-2.2-mR8t/OpenCV-2.2.0/modules/core/src/persistence.cpp, line 4811
terminate called after throwing an instance of ‘cv::Exception’
what(): /tmp/homebrew-opencv-2.2-mR8t/OpenCV-2.2.0/modules/core/src/persistence.cpp:4811: error: (-2) The node does not represent a user object (unknown type?) in function cvRead

A lot of posts in the forums relate this same problem, and most of the suggested workarounds tell to perform any dummy operation on a IplImage before loading the classifier, like

IplImage* hack = cvLoadImage("dummy.jpg", CV_LOAD_IMAGE_COLOR);
cvErode(hack, hack, 0, 3);

but that didn’t work for me (it may work for you, though… just give it a try). What I’ve found out is that I should link against opencv_objdetect as well. One may argue that was an obvious thing to do, but as at the moment I write this post I am not that experienced with OpenCV, I did take some time to figure out the solution, as many of the examples available out there use Windows or Linux, with different setups.

My working compile command is this one:

gcc -o detect detect.c -lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_objdetect

It does make sense linking against objdetect¬†if you think for a while, but as the program compiled fine without it and the error message was cryptic, the path to the solution didn’t make so much sanse, though.

One thought on “Loading a Haar Classifier with OpenCV in Mac OS X without hassle

  1. I tried to see the Linker Command. Project -> <>Properties -> Linker -> Command Line. I had linked opencv_objdetect220d – debug file hence included all the debug files in the Linker-> Input-> Additional Dependencies as opencv_core220d.lib;opencv_highgui220d.lib;opencv_imgproc220d.lib;. Placed all these files in the Debug folder of solution and Bingo it works fine.

Leave a Reply

Your email address will not be published. Required fields are marked *


seven + = 9

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>