A developer is working on computer vision and he needs some 3D modeling algorithms but he does not have much time so what can he do? He can go to OpenCV, and can have the required algorithm from there. So what is OpenCV? OpenCV is a platform having a huge number of algorithms. It was initially introduced as a research project by Intel in 2006 but today it is the largest platform having 2500+ algorithms and a huge number of interfaces which can be utilized in different languages such as Python, Java etc. moreover, it is an open-source which means anyone can access it free of cost. It contains a variety of algorithms related to 3D modeling, real time functions, support vector machines (SVM) and many more.
Steps involved in installing OpenCV in Ubuntu are discussed in this article
How to install OpenCV on Ubuntu 20.04
OpenCV is among the best platforms which provides ease to developers in their work. The latest version which is being used nowadays is 4.5.3 released on 17th of July, 2021. It is in C++ but it can be used in other languages with the help of bindings.
Here we will discuss about two methods to install OpenCV in Ubuntu either from its own repository or by downloading it from source.
Method 1: Install OpenCV from Default Repository
We can install OpenCV from its own repository and the steps involved in it are discussed.
Step 1: First we will update the repository:
sudo apt update
Step 2: We will install the OpenCV library and python library from the repository by using a single command:
sudo apt install python3-opencv libopencv-dev
Step 3: Installation is verified by checking the version of OpenCV by using the “cv2 module”:
python3 -c "import cv2; print(cv2.__version__)"
Output4.5.3-dev
In the output it will display the version of the OpenCV.
Method 2: Install OpenCV using Source Code
We can install OpenCV from its original source as well. For this purpose, we have to follow the following steps.
Step 1: First we install the files which are necessary for the installation of the OpenCV and for this purpose we install the build tool:
sudo apt install build-essential cmake git pkg-config libpng-dev libtiff-dev gfortran openexr libgtk-3-dev libavcodec-dev libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev libjpeg-dev libatlas-base-dev python3-dev python3-numpy libtbb2 libtbb-dev libdc1394-22-dev libopenexr-dev
Step 2: Now first make the directory using “mkdir” and then clone the OpenCV and OpenCV contribution repository.Clone both of them from the website of github.com:
mkdir ~/opencv_build && cd ~/opencv_build
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git
Step 3: We will navigate the file and make a directory there using “mkdir”:
cd ~/opencv_build/opencv
mkdir -p build && cd build
Now we will configure and setup the OpenCV build by using “make”:
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D BUILD_EXAMPLES=ON \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_build/opencv_contrib/modules \
-D INSTALL_C_EXAMPLES=ON ..
Step 4: Now we should know the number of cores of processor, we can find them out by:
nproc
Output1
In my case it has one core. So now, we start the compilation process by using make command but we will enter the number of cores of the processor with “j” for example in my case I have one core so I’ll write “j1”, likewise if you have 8 cores of processor you can write j8.
make -j1
It will take some time to compile.
Step 5: Start the installation setup:
sudo make install
To verify the installation of OpenCV we will check the versions of C++ and Python bindings.
pkg-config --modversion opencv4
Output4.5.3
Similarly, check the version of OpenVC python library.
python3 -c "import cv2; print(cv2.__version__)"
Output4.5.3-dev
To Conclude
Instead of spending a lot of time developing code for algorithms, developers use the algorithm from the OpenCV library. It not only provides ease in their job but also saves a lot of time. In this article, we have discussed different methods to install OpenCV either from its repository or from the original source. To download it from its own repository is a little bit fast but from the original source is more reliable as it can access all the latest versions of libraries.
2 Comments
…at least this is the case for the ARM version. Maybe it is different for X86_64
IMHO the default OpenCV version installed with 20.04 is 4.2.0, not 4.5.3-dev