Installing OpenCV with contrib extra modules from source on Fedora Linux
Why the sudden need? I have mostly used the OpenCV library as available from pip or by installing wheel until I required some more essential features that are not available from those pre built binaries. However the installation looking fairly simple can have a lot of issues if not done correctly and hence the reason for being so mnay questions on forums out there on the internet pertaining to the installation issues:
Let’s begin :
Although it is always a best practice to create a virtual environment so as to keep your project dependencies sorted but skipping the step here. If you want to create one or know more about those you can visit this link for more information .
- Clone
git clone https://github.com/opencv/opencv.git
the official OpenCV repository from Github to have the latest updated version for use.
This will create a folder named opencv in your home directory.
- Some dependencies need to be taken care of before building OpenCV for use. These dependencies are IMPORTANT to facilitate the proper installation of opencv on the system and avoiding errors like
no module found
and etc even after having a successful install.
The must have dependenices :
* dnf install cmake
* dnf install python-devel numpy
* dnf install gcc gcc-c++
* dnf install make
-
GTK support :
* dnf install gtk2-devel * dnf install libdc1394-devel * dnf install libv4l-devel * dnf install ffmpeg-devel * dnf install gstreamer-plugins-base-devel
-
For parallelizing functions in opencv on Intel machines you can install Threaded building blocks
yum install tbb-devel
. If you install this pass -D WITH_TBB=ON while building OpenCV with CMake. -
Similarly for optimized mathematical operations install
dnf install eigen3-devel
and pass -D WITH_EIGEN=ON as earlier. -
Create a folder named build wherein you’ll configure and build the library to later install by doing
mkdir build
cd build
-
Installing OpenCV - OpenCV is typically installed using CMake as(explanation from the documentation) - Installation has to be configured with CMake. It specifies which modules are to be installed, installation path, which additional libraries to be used, whether documentation and examples to be compiled etc. Below command is normally used for configuration (executed from build folder).
-
To configure build parameters using CMake you will pass the following command for minimal installation in release mode
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
-
If you have installed the threading building blocks then you pass
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local .. -D WITH_TBB=ON
-
If you have installed the eigen library then do
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local .. -D WITH_EIGEN=ON
. -
If installed both then
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local .. -D WITH_EIGEN=ON -D WITH_TBB=ON
-
Now to also include non free algorithms(optional) you will need to add the specific parameter to it as :
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local .. -D WITH_EIGEN=ON -D WITH_TBB=ON -D OPENCV_ENABLE_NONFREE=ON
-
We have now set up our opencv installation command, many a times opencv’s extra modules also help you solve many computer vision challentasks and it is better to have the contrib i.e extra modules with opencv as a additional step. Hence to do so first clone the opencv contrib module from Github using
git clone https://github.com/opencv/opencv_contrib.git
to a seperate folder than your opencv directory.
-
Doing so it is then required to pass the path to the downloaded contrib modules to the build configuration of opencv. To do that use the follwoing command
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local .. -D WITH_EIGEN=ON -D WITH_TBB=ON -D OPENCV_ENABLE_NONFREE=ON -D OPENCV_EXTRA_MODULES_PATH=<opencv_contrib_path>/modules <opencv_source_directory_path>
.
This will serve as your final command to installl opencv with all the required modules.
- Finally execute
make install
to successfully build and install the OpenCV library on your Linux Fedora machine.