Thursday, March 17, 2011

OpenCV installation with Ubuntu

I am sure that many of you have tried installing OpenCV with python support on Linux but found no success as executing a program gives you no module found error. I recently figured out on how to do it and fix all the errrors.

Btw, My other programs in OpenCV will be posted here

In this post I explain on how to install OpenCV on Ubuntu with python and ffmpeg support on

Follow the steps and your problem will be solved.

  1. First install python-dev package (with $sudo apt-get install python-dev )
  2. Then install ffmpeg required files with $sudo apt-get install libavformat-dev libavcodec-dev libavfilter-dev libswscale-dev
  3. Also in some systems, there might be a pre-requsite that you have to install gtk, etc. For those, execute the following:  $ sudo apt-get install libavformat-dev libgtk2.0-dev pkg-config cmake libswscale-dev bzip2
  4. Then, download the tar ball from the here. Also instead of this, you can run a svn checkout against latest version of opencv trunk.
  5. save the above file in a folder.
  6. Open Terminal and browse to above folder.
  7. Give the command $tar xvjf OpenCV-2.3.0.tar.bz2
  8. The files in tar ball will be extracted in a folder named OpenCV.... in the present folder.
  9. browse to the extracted folder, create a folder named release  by giving the following command and change the present directory to release folder. $ cd OpenCV-2.3.0; mkdir release; cd release
  10. Now we need to create configuration files using CMAKE (if cmake isn't installed install it by giving the command $sudo apt-get install cmake) using the following command: $cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_PYTHON_SUPPORT=ON ..
  11. Give  the following command to compile the source: $make
  12. Give the following command to install the libraries: $sudo make install
  13. Then we need to make the OS know that OpenCV is installed in a particular folder: so edit the LD_LIBRARY_PATH variable by giving the following command: $export LD_LIBRARY_PATH=/usr/local/lib/:$LD_LIBRARY_PATH
  14. Then give the following command to update the ld paths: $sudo ldconfig.
  15. Now we need to move file in site-packages folder to dist-packages folder $sudo mv /usr/local/lib/python2.6/site-packages/ /usr/local/lib/python2.6/dist-packages/
  16. and we're done!! :-)
  17. And yeah! In order to compile use the following: $g++ `pkg-config opencv --cflags --libs` -g input.cpp -o output
  18. I usually use this alias in Ubuntu which makes my life easier: alias opencvcc="g++ `pkg-config opencv --cflags --libs` -g". Now I can just compile using the following command: $opencvcc input.cpp -o output

Or you can just execute the following script (which does same as the above):
Now you are ready to run the first program in python, click here a simple 14 line code in python.


No comments:

Post a Comment