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

Install OpenCV on Linux Mint or Ubuntu

  • 4 min

OpenCV is undoubtedly the premier computer vision library of the moment. This library contains over 2500 algorithms, including a vast number of functions for image processing, computer vision, and machine learning.

For example, we have algorithms for identifying objects, faces, tracking objects, and extracting 3D models. These functions can be used in applications for intruder detection, robot navigation, 3D reconstruction, quality control, label reading, and countless other applications.

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

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 how to install OpenCV on Linux Mint or Ubuntu and configure it correctly. The installation process itself is not complicated, but it’s true that without an ordered list of commands to execute, it can be quite difficult to remember all the steps.

Download OpenCV

First, we ensure that our system is up to date. To do this, we execute 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 decompress the library. We can use an 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 2.4.9 in all commands 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 don’t know exactly 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 will appear with the results of cmake. We must check that no errors appear, and that all the options we selected are marked as ‘YES’. In particular, check that FFMPEG, PYTHON, OPENGL, and QT appear correctly, as sometimes it doesn’t find them.

If any error appears or it doesn’t find a component, you will have to go back and check that you have all the dependencies installed.

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

make sudo make install

If no error messages have appeared, you have successfully installed OpenCV.

Configure OpenCV

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

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

Add the following line to the end of the file (if the file is empty, it’s fine, we just copy the line).

/usr/local/lib

Next, we edit the bashrc file.

sudo nano /etc/bash.bashrc

Add the following lines to the end of the file.

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

Finally, restart the computer and we have OpenCV ready to go.

Testing OpenCV

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

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

If everything has worked correctly, congratulations, you have successfully installed and configured OpenCV. Now you can experiment with the rest of the library’s examples and freely use its algorithms in your projects.