Skip navigation

OpenCV is a programming library for computer vision routines. As I’m doing my undergrad research in UPD-DCS’ Computer Vision and Machine Intelligence Group (CVMIG), I recently had the need to install OpenCV in Ubuntu.

Perhaps the most obvious way—yet most troublesome as well—to install OpenCV in Ubuntu would be to compile it from source. I started using Ubuntu March 2010 but, to date, I still haven’t installed anything successfully via build from source. So, I’m not dwelling on that here. Building from source isn’t a quick and easy way by any means.

The instructions at Ubuntu’s Community Documentation is, by far, the easiest way I’ve found. However, the last sudo apt-get install line mentioned—the one that’s supposed to install all the OpenCV goodness into your system—fails with the error message “Couldn’t find package libcv2.1”.

Maybe it used to work, back when OpenCV was just version 2.1. Unfortunately, as of this writing, OpenCV is now in version 2.3. To remedy the apparently outdated Community Docs, head over to Ubuntu’s package information page for OpenCV and sudo apt-get install all the packages mentioned there. So the last line with which I’m having issues with becomes (as of this writing!):

sudo apt-get install libcv-dev libcv4 libcvaux-dev libcvaux4 libhighgui-dev
libhighgui4 opencv-doc python-opencv

Note that the package info page link I gave is for Lucid since I’m in Lucid. There are links for the corresponding packages for other supported and LTS versions in the page. Click the one that suits you.

Running the examples

The Community Docs have instructions on how to run the examples. However, I stumbled on as, apparently, on the first iteration of the script’s compile loop, it misconcatenates strings and tries to compile something called .c.c. I tried to compile each file individually to which my terminal gave me the following:

$ gcc -o squares squares.c
squares.c:12:16: error: cv.h: No such file or directory
squares.c:13:21: error: highgui.h: No such file or directory

Followed by more lines of error which, as far as I can tell, should go away had these two libraries linked properly. While it may initially seem (as it did to me) that OpenCV failed to install properly, this is actually just some quirk with C’s compilation process. I remember having this problem with math.h and that is, as far as I understand, a standard C library at that.

When I realized this, I opened in GEdit and found how to invoke gcc/g++ so that the examples compile. Invoke gcc as:

gcc -ggdb `pkg-config opencv --cflags --libs` inputfilename.c -o outputfilename

Same thing works for the .cpp files (C++ source codes) but don’t forget to invoke g++ instead of gcc!

More musings:

  • The first command the Community Docs asks you to do is sudo su which makes you a root/superuser in the command line. From there you should be able to execute protected commands even without the sudo invocation. Hence, the sudo calls in the subsequent lines shouldn’t be necessary, though it won’t hurt to have them.
  • On why the most troublesome method is the most obvious one. Google “install opencv ubuntu” and the first link that will discuss installation without compiling the code is at position 6, and it is outdated at that (yep Community Docs, I’m still looking at you). Even the official OpenCV installation manual tells you to build from source. I know that it is bothersome to write an installation how-to for every Linux distro out there and that not all of them probably support sudo apt-get install, and for those non-Ubuntu distros that do, they probably do not connect to the Ubuntu package repos and so there may be a discrepancy or two that needs to be addressed. However, I specifically searched for Ubuntu instructions. I love Ubuntu and all the geekery that goes with it but I am of the opinion that if Ubuntu is really to be user-friendly, the easy methods should be the most obvious ones.

One Comment

  1. Thanks, this saved me a lot of time.

Leave a Reply

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