How to build OpenCV C++ for Pythonistas

Sayantan Das
Data Science Network (DSNet)
2 min readJul 14, 2019

I am making this article because I felt this was needed as when I was rummaging through StackOverflow I couldn’t understand anything.

Now that I have figured out part of this, let me take you through some basics behing gcc toolchain and CMake , to make your life easier.

I’ll directly quote from the official documentation so this doesn’t stand as an alternative tutorial of build , just making things easier for you.

[compiler] sudo apt-get install build-essential[required] sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev[optional] sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

Do the above line. This stays the same. You might come across one or more of those lib**-dev library not being you able to download them. I believe you won’t face an issue with that. Just download as much as possible and move on to the next snippet.

cd ~/<my_working_directory>git clone https://github.com/opencv/opencv.gitgit clone https://github.com/opencv/opencv_contrib.git

This is easy. Yes, contrib is important because there are important modules there

cd ~/opencvmkdir buildcd build

Let me show you my working directory

/home/sayantan/

Now you have to run CMake. CMake is used to build C++ applications.

cmake -D CMAKE_BUILD_TYPE=Release -D OPENCV_EXTRA_MODULES_PATH=/home/sayantan/opencv_contrib/modules/ -D BUILD_EXAMPLES=ON -D CMAKE_INSTALL_PREFIX=/usr/local ..

for my case my CMAKE_INSTALL_PREFIX is /home/sayantan/opencv/build

I suggest you not to use cmake-gui.

I have switched on build examples. I am using opencv_contrib. Make changes to your path as required.

make -j7

Use j7 so that make can get speeded up with parallelisation.

sudo make install

That’s it .

In the next we will go through how to run an OpenCV application.

--

--