OpenCV is a machine learning library that provides various functions relating to Computer Vision applications. OpenCV can be used with C++, Python, Java, and MATLAB.
Since it is an external library not built into any programming language, it has to be installed separately on the system before use. While it is usually simple to install and run OpenCV, a few problems might cause the OpenCV library function not to work, such as missing packages or wrong syntax when compiling.
Below we will list a few common problems when installing and running OpenCV and how to fix them.
libopencv-dev Is Not Installed
libopencv-dev package is a supporting package for running OpenCV, and it contains some library binaries that are usually required for running OpenCV on Linux distributions, specifically Ubuntu.
When running a file containing OpenCV functions doesn’t execute, and the system shows the error message
pkg-config cannot find OpenCV, it is usually due to the missing
libopencv-dev package. This problem can easily be solved by installing
If you have installed OpenCV on the system with Linux package manager, then to install
libopencv-dev, run the following command on the terminal.
sudo apt-get install libopencv-dev
If you have installed OpenCV to be used with Python using
pip, you need to run the following command on the terminal.
sudo apt-get install libopencv-dev python-opencv sudo pip install opencv-python
Note: For the above command, if you are using
pip3on your system, you must replace
C++ Files Using OpenCV Not Compiling
Suppose you have already installed OpenCV on your system with all the relevant packages, and when compiling with G++, you get the
undefined reference to OpenCV error. In that case, it is usually due to the syntax of the compile command.
For example, compiling with the following command will generate an
undefined reference error.
g++ `pkg-config --cflags --libs opencv` test.cpp -o test
To fix this error, add the
pkg-config --cflags --libs opencv at the end of the command like this:
g++ test.cpp -o test `pkg-config --cflags --libs opencv`
The above command will cause the file to compile and run successfully.
If you are using the correct syntax and the file still does not compile and generates an
undefined reference error, it can be due to the version of OpenCV installed on your system not matching with the compile command; for example, if you are using
OpenCV4, it is essential to add the 4 with the compile command as well.
g++ test.cpp -o test `pkg-config --cflags --libs opencv4`
- No Module Named CV2 in Mac in Python
- Image Masking in OpenCV
- Utilize Bitwise_AND on an Image Using OpenCV
- OpenCV ArUco Markers
- SIFT Using OpenCV in Python