This is a step-by-step guide to installing OpenCV 3 on a Debian-based Linux system from source. The steps should stay the same for other distros, just replace the relevant package manager commands when installing packages for the build.
Note: If you don't feel like wasting time building stuff or dislike the terminal, you can most likely install OpenCV from the Synaptic package manager GUI. However, these libraries are often out of date.
Issue the following commands in your terminal to install the required packages:
sudo apt-get update
sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config \
libavcodec-dev libavformat-dev libswscale-dev
The following packages are optional:
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev \
libpng-devlibtiff-dev libjasper-dev libdc1394-22-dev
Issue the following command to get the OpenCV source code and prepare the build:
mkdir ~/src
cd ~/src
git clone https://github.com/opencv/opencv.git
cd opencv
mkdir build && cd build
We include the examples in the build, but feel free to leave them out. Also feel free to set other flags and customise your build as you see fit.
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D INSTALL_C_EXAMPLES=ON ..
If CMake didn't report any errors or missing libraries, continue with the build.
make -j$(nproc)
If no errors were produced, we can carry on with installing OpenCV to the system:
sudo make install
Now OpenCV should be available to your system. You can use the following lines to know where OpenCV was installed and which libraries were installed:
pkg-config --cflags opencv # get the include path (-I)
pkg-config --libs opencv # get the libraries path (-L) and the libraries (-l)
We first build the C++ examples:
cd ~/src/opencv/samples
cmake .
make
If no errors were produced, run a any sample, e.g.
./cpp/cpp-example-edge
If the sample runs, then the C++ libraries are properly installed.
Next, test the Python bindings:
python
>> import cv2
>> print cv2.__version__
If these commands import OpenCV and print the correct version without complaining, then the Python bindings are properly installed.
Congrats, you just built and installed OpenCV. Happy programming!
For Mac refer here OpenCV installation on Mac OS X