Language: EN

instalar-opencv-en-linux-mint-o-ubuntu

Install OpenCV on Linux Mint or Ubuntu

OpenCV is undoubtedly the library par excellence in computer vision at the moment. This library contains more than 2500 algorithms that include a large number of functions on image processing, computer vision, and machine learning.

For example, we have algorithms to identify objects, faces, track objects, extract 3D models. These functions can be used in applications for intruder detection, robot navigation, 3D reconstruction, quality control, tag reading, and endless applications.

OpenCV is free, in the sense that it is distributed under the BSD license, that is, we can use it freely in our projects, whether commercial or research, as long as we comply with the conditions of the license.

OpenCV is written in C++, has interfaces in C++, C, Python, Java, and MATLAB and works on Windows, Linux, Android, and Mac OS.

In this post, we will learn to install OpenCV on Linux Mint or Ubuntu and configure it correctly. The installation process is not complicated in itself, but it is true that if you do not have an ordered list of the commands to execute, it can be quite difficult to remember all the steps.

Download OpenCV

First, we make sure that our system is updated. To do this, we run the following commands in a terminal window.

sudo apt-get update
sudo apt-get upgrade 

Next, we install the necessary dependencies to run OpenCV. To do this, we execute the following two commands.

sudo apt-get -y install build-essential cmake cmake-qt-gui pkg-config libpng12-0 libpng12-dev libpng++-dev libpng3 libpnglite-dev zlib1g-dbg zlib1g zlib1g-dev pngtools libtiff4-dev libtiff4 libtiffxx0c2 libtiff-tools
sudo apt-get -y install libjpeg8 libjpeg8-dev libjpeg8-dbg libjpeg-progs ffmpeg libavcodec-dev libavcodec53 libavformat53 libavformat-dev libgstreamer0.10-0-dbg libgstreamer0.10-0 libgstreamer0.10-dev libxine1-ffmpeg libxine-dev libxine1-bin libunicap2 libunicap2-dev libdc1394-22-dev libdc1394-22 libdc1394-utils swig libv4l-0 libv4l-dev python-numpy libpython2.6 python-dev python2.6-dev libgtk2.0-dev pkg-config

Now we proceed to download and unzip the library. To do this, we can use the Internet browser, or directly use the following commands.

wget http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.4.9/opencv-2.4.9.zip/download -O opencv.zip
unzip opencv.zip
rm opencv.zip
cd OpenCV-2.4.9

At the time of writing this post, the latest version is 2.4.9. If you want to install a different version, simply modify all the commands from 2.4.9 to the corresponding version.

Compile OpenCV

We create a folder to temporarily store the compiled files

mkdir build
cd build

Next, we use cmake to create the makelist. We must specify the modules we want to install, which will depend on your particular needs. If you do not exactly know which modules you need, you can get a fairly standard and versatile configuration with the following command.

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_OPENGL=ON ..

A screen with the results of cmake will appear. We must check that there are no errors, and that all the options we have selected appear marked as ‘YES’. In particular, make sure that FFMPEG, PYTHON, OPENGL, and QT appear correctly, as sometimes it does not find them.

If there is any error or it does not find any component, you will have to go back and check that you have all the dependencies installed.

On the other hand, if everything is correct, we can proceed to compile the library. Run the following commands (and you can go for a coffee because it will take a while)

make
sudo make install

If there is no error message, you have installed OpenCV correctly.

Configure OpenCV

Before we can use OpenCV, we must make a few configurations for it to work correctly. First, we open opencv.conf by typing

sudo nano /etc/ld.so.conf.d/opencv.conf

We add the following line at the end of the file (if the file is empty, it’s okay, just copy the line)

/usr/local/lib

Next, we edit the bashrc file.

sudo nano /etc/bash.bashrc

We add the following lines at the end of the file.

PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig export PKG_CONFIG_PATH

Finally, restart your computer and OpenCV is ready to run.

Testing OpenCV

Now we check that everything is correct and working. To do this, we will run one of the demos available in Python (you can try a C example if you prefer). To do this, run

cd ~/opencv/OpenCV-2.4.9/build/bin
python ./drawing.py