Installing OpenCV from source on Debian / Raspbian / Ubuntu

By CryptoSaga | revtoki | 11 Mar 2020


There're lots of guides out there but I'm gonna go ahead and point out this one. It worked for me and it's got a script, which you can modify and (re)use for a personalized install.

If you're on a Raspberry Pi, you might want to expand your swap space first, otherwise your make -j4 command might cause the system to run out if memory and stall:

sudo dphys-swapfile swapoff

As root, edit the file /etc/dphys-swapfile and modify the variable CONF_SWAPSIZE

sudo dphys-swapfile swapon

I prefer using cmake-gui, replacing the cmake command in the script. When installing OpenCV on my Pi, I had to disable all python and java related flags (because python and java modules were causing errors and I didn't need them anyway) and using cmake with GUI can help find and unset flags.

If you plan on using g++ or make to compile then make sure to set OPENCV_GENERATE_PKGCONFIG flag when running cmake during setup, so the opencv.pc file is generated (inside /usr/local/lib/pkgconfig).

Make sure that the line

export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig

is in your .bashrc file(and reboot).

You can use a Makefile like this (keep in mind there are better ones out there):

OPENCV = $(shell pkg-config --cflags --libs opencv4)

opencv_test:
 g++ -std=c++11 -o opencv_test opencv_test.cpp $(OPENCV)

.PHONY: opencv_test

pkg-config will include what's inside /usr/local/include/opencv4 so you can use them in your code, for example:

#include "opencv2/aruco.hpp"
#include "opencv2/highgui/highgui.hpp"

since the opencv2 dir is inside the above path.

Below there are some specific notes for stuff I had to use or errors I ran into:

/** Notes

* dpkg -L <package_name> helps find packages

*/

-- Cmake can't find Qt5 --

CMake Error at cmake/OpenCVFindLibsGUI.cmake:18 (find_package): Could
not find a package configuration file provided by "Qt5" with any of
the following names:

Qt5Config.cmake qt5-config.cmake

Add the installation prefix of "Qt5" to CMAKE_PREFIX_PATH or set
"Qt5_DIR" to a directory containing one of the above files. If "Qt5"
provides a separate development package or SDK, be sure it has been
installed.

Solution from
https://askubuntu.com/questions/374755/what-package-do-i-need-to-build-a-qt-5-cmake-application

// install dev package
sudo apt install qtbase5-dev

How do you rate this article?

2


CryptoSaga
CryptoSaga

Metaphysics and Crypto. Photo by Athena from Pexels https://www.pexels.com/photo/black-and-gray-motherboard-2582932/


revtoki
revtoki

Engineering blog

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.