Skip to content

How To Install OpenCV4

Victor Linfield edited this page Dec 20, 2018 · 1 revision

How To Build OpenCV4 From Source On Linux

This uses some of the methods as presented by the openCV team. However, I skip some unnecessary steps for our use of OpenCV (aka installing it for C++ and Java). If for some reason the method doesn't setup properly come to me (Troake).

The original tutorial is available here


First step is to ensure that we can build the source. Therefore some dependencies are required.

sudo apt-get install cmake
sudo apt-get install python-devel numpy
sudo apt-get install gcc gcc-c++

Cmake is used for building C applications. OpenCV is a C/C++ based library. It just also has Python support. In order to use extra features such as gstreamer, these dependencies must also be downloaded and installed:

sudo apt-get install gtk2-devel
sudo apt-get install libv4l-devel
sudo apt-get install ffmpeg-devel
sudo apt-get install gstreamer-plugins-base-devel

The source for OpenCV is on github so you have to clone the repository. Run this command in a location to hold the source.

git clone https://github.com/opencv/opencv.git

When you run the git clone, it will put it all in a folder called opencv in that directory. The next step is to make a directory for the built version of OpenCV. Go into the opencv folder and create a build folder.

cd opencv
mkdir build

Go into the build directory and run cmake on the parent directory.

cd build
cmake ../

(or you can just run cmake from the opencv folder)

cmake

You should get something like this close to the end of the cmake log. This confirms that OpenCV recognises Python 3 on the machine (There may also be some info about Python2. That's all good as long as you see the Python3 info).

--   Python 3:
--     Interpreter:                 /usr/bin/python3.4 (ver 3.4.3)
--     Libraries:                   /usr/lib/x86_64-linux-gnu/libpython3.4m.so (ver 3.4.3)
--     numpy:                       /usr/lib/python3/dist-packages/numpy/core/include (ver 1.8.2)
--     packages path:               lib/python3.4/dist-packages

After cmake is finished go into the /build/python_loader folder and run setup.py.

python3 setup.py install

How to install OpenCV4 on Windows

Windows is a little complicated for things like this but if you want to give it a try, check out this tutorial or follow the instructions below.

Get dependencies

Download and install Visual Studio Build Tools from here. When installing select Desktop development with C++.

Download and install CMake from here. When installing add it to your system path.

Download and install Python 3 form here.

Download and install git for Windows from here.

Install OpenCV4

Download the latest Win pack from https://opencv.org/releases.html

Run the downloaded executable and extract the contents to where you want the program to be installed.

As administrator then run:

$ setx -m OPENCV_DIR C:\OPENCVDIR\Build\x64\vc14

Where OPENCVDIR is the directory you extracted it to.

Then open the program Edit the system environmental variables, Environmental Variables... and add a new entry to the PATH being %OPENCV_DIR%\bin

To add python support go to the python directory opencv\build\python and run the following:

$ python setup.py install

Everything should now be installed and to check that things are working run:

$ python
>>> import cv2
>>> cv2.__version__

If it prints out '4.0.0' everything is good to go.