Thursday, September 8, 2011

Configuring an OpenCV 2 project in Eclipse

I recently picked up a book on OpenCV 2 but all the setup instructions are mostly geared toward developing in either Visual Studio or Qt. Since I mostly do Java development, I would prefer to use Eclipse and I happen to use a Mac at home. And I haven't done much C++ development in Eclipse so I wasn't sure how to get things setup. Setup is actually pretty easy once you know the right values. I've seen some incorrect (maybe just outdated?) info about this on the internet so hopefully this updated info will be useful for people like myself who are new to OpenCV, as well as the C++/Eclipse/Mac combo.

Once you have CDT installed, download the OpenCV source from their site, opencv.willowgarage.com. OpenCV uses a tool called CMake to generate make scripts for the build tool of your choice. Since I'm not very familiar with Xcode, I followed the Linux instructions, where you generate files for make. Using the default build configuration in CMake has worked well for me so far. In a Terminal, navigate to the directory where the Makefile was generated and run "make". Then run "sudo make install" to copy the headers and dylibs where they belong, in /usr/local/include/ and /usr/local/lib/ respectively.

Once those are built and installed, you can creata a C++ project in Eclipse. Then, the trick to getting an OpenCV project to compile and run is to go to the Project Properties and select Settings, under C/C++ Build. On the Tool Settings tab, under MacOS X C++ Linker, select Libraries. Add /usr/local/lib as a library search path. Then add the following as libraries:

  • opencv_calib3d
  • opencv_contrib
  • opencv_core
  • opencv_features2d
  • opencv_flann
  • opencv_gpu
  • opencv_highgui
  • opencv_imgproc
  • opencv_legacy
  • opencv_ml
  • opencv_objdetect
  • opencv_ts
  • opencv_video

Those values correspond to files in the library search path but with the "lib" prefix and ".dylib" extension stripped off. So if your build relies on libfoo.dylib, you would simply add "foo" as a library.

You may also notice that the OpenCV build process creates quite a few files in the library directory. Most of those are aliases/links that allow you to be as specific (or not) as you want about which version to use. A library like opencv_core should point to the most recent version. But the most recent version tomorrow might have a totally different API than it did yesterday, so watch out. The opencv_core.2.3 library points to the most recent 2.3.x release. And for me, as of right now, opencv_core.2.3.1 is that most recent version.

4 comments:

  1. I've been looking for this data for weeks. Thanks.

    ReplyDelete
    Replies
    1. I'm always glad when my experiences struggling through something helps someone else.

      Delete
  2. mine says opencv_gpu library not found.. when i try to build an example project i found on this website http://docs.opencv.org/doc/tutorials/introduction/linux_eclipse/linux_eclipse.html

    since u are using mac, i'm following yours as i am using a mac too.

    i have done what u have said in this page, i have included both forms of the library eg: opencv_gpu, libopencv_gpu. thought it wont matter as i'm putting in both. i had already put the libopencv_gpu, etc libraries before i found this. so i did not bother removing it

    ReplyDelete
    Replies
    1. Neven mind, i fixed it. i removed libopencv_gpu,etc and kept the names without the lib prefix. it just magically worked :D

      Delete