- shivani singh
- October 26, 2021
Steps to Setup OpenCV on Ubuntu 20.04 LTS
OpenCV is a free & open source computer vision library with bindings for python, C++, and Java. It supports different Operating Systems like Windows and Linux. It helps to use multi-core processing and GPU acceleration for real-time operating tasks & it is used for used for applications, including face recognition and detection, surveillance video, & 3D model extraction.
There are few steps to setup OpenCV on ubuntu:
Step 1: Update the System.
apt-get update
Step 2: Install OpenCV on system by using ubuntu repository.
apt install libopencv-dev python3-opencv
- To check the OpenCV version.
python3 -c "import cv2; print(cv2.__version__)"
- Here is the command output.
root@ip-172-31-27-203:/home/ubuntu# python3 -c "import cv2; print(cv2.__version__)"
4.2.0
Step 3: Install OpenCV by using the source.
- Install the required dependencies.
apt install build-essential cmake git pkg-config libgtk-3-dev libavcodec-dev libavformat-dev libswscale-dev libv4l-dev \
libxvidcore-dev libx264-dev libjpeg-dev libpng-dev libtiff-dev \
gfortran openexr libatlas-base-dev python3-dev python3-numpy \
libtbb2 libtbb-dev libdc1394-22-dev libopenexr-dev \
libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev
Step 4: Create a folder & git clone the opencv repository.
mkdir ~/opencv_build && cd ~/opencv_build
git clone https://github.com/opencv/opencv.git
- Here is the command output.
root@ip-172-31-27-203:/home/ubuntu# mkdir ~/opencv_build && cd ~/opencv_build
root@ip-172-31-27-203:~/opencv_build# git clone https://github.com/opencv/opencv.git
Cloning into 'opencv'...
remote: Enumerating objects: 297356, done.
remote: Total 297356 (delta 0), reused 0 (delta 0), pack-reused 297356
Receiving objects: 100% (297356/297356), 487.15 MiB | 30.98 MiB/s, done.
Resolving deltas: 100% (206735/206735), done.
Updating files: 100% (6946/6946), done.
- To clone the OpenCV contrib repositories.
git clone https://github.com/opencv/opencv_contrib.git
- Here is the command output.
root@ip-172-31-27-203:~/opencv_build# git clone https://github.com/opencv/opencv_contrib.git
Cloning into 'opencv_contrib'...
remote: Enumerating objects: 35940, done.
remote: Counting objects: 100% (1236/1236), done.
remote: Compressing objects: 100% (765/765), done.
remote: Total 35940 (delta 584), reused 891 (delta 405), pack-reused 34704
Receiving objects: 100% (35940/35940), 131.52 MiB | 33.38 MiB/s, done.
Resolving deltas: 100% (22058/22058), done.
Step 5: Create a folder.
cd ~/opencv_build/opencv
mkdir -p build && cd build
- To setup Opencv build.
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_C_EXAMPLES=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_build/opencv_contrib/modules \
-D BUILD_EXAMPLES=ON ..
- Here is the command output.
root@ip-172-31-27-203:~/opencv_build# cd ~/opencv_build/opencv
root@ip-172-31-27-203:~/opencv_build/opencv# mkdir -p build && cd build
root@ip-172-31-27-203:~/opencv_build/opencv/build# cmake -D CMAKE_BUILD_TYPE=RELEASE \
> -D CMAKE_INSTALL_PREFIX=/usr/local \
> -D INSTALL_C_EXAMPLES=ON \
> -D INSTALL_PYTHON_EXAMPLES=ON \
> -D OPENCV_GENERATE_PKGCONFIG=ON \
> -D OPENCV_EXTRA_MODULES_PATH=~/opencv_build/opencv_contrib/modules \
> -D BUILD_EXAMPLES=ON ..
-- The CXX compiler identification is GNU 9.3.0
-- The C compiler identification is GNU 9.3.0
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Detected processor: x86_64
- To Start the compilation process.
make -j8
- Here is the command output.
root@ip-172-31-27-203:~/opencv_build/opencv/build# make -j8
Scanning dependencies of target opencv_highgui_plugins
Scanning dependencies of target gen-pkgconfig
Scanning dependencies of target libopenjp2
Scanning dependencies of target opencv_videoio_plugins
Scanning dependencies of target ittnotify
[ 0%] Building C object 3rdparty/openjpeg/openjp2/CMakeFiles/libopenjp2.dir/thread.c.o
Scanning dependencies of target libwebp
[ 0%] Built target opencv_highgui_plugins
[ 0%] Generate opencv4.pc
Scanning dependencies of target ippiw
[ 0%] Built target opencv_videoio_plugins
Scanning dependencies of target libprotobuf
Scanning dependencies of target ade
Scanning dependencies of target quirc
[ 0%] Building C object 3rdparty/ittnotify/CMakeFiles/ittnotify.dir/src/ittnotify/ittnotify_static.c.o
[ 0%] Building C object 3rdparty/ippiw/CMakeFiles/ippiw.dir/src/iw_core.c.o
[ 0%] Building C object 3rdparty/quirc/CMakeFiles/quirc.dir/src/decode.c.o
[ 0%] Built target gen-pkgconfig
[ 0%] Building C object 3rdparty/libwebp/CMakeFiles/libwebp.dir/src/dec/alpha_dec.c.o
[ 0%] Building CXX object CMakeFiles/ade.dir/3rdparty/ade/ade-0.1.1f/sources/ade/source/alloc.cpp.o
Scanning dependencies of target gen_opencv_python_source
[ 0%] Generate files for Python bindings and documentation
Step 6: To install opencv.
make install
- Here is the command output.
root@ip-172-31-27-203:~/opencv_build/opencv/build# make install
[ 0%] Built target opencv_highgui_plugins
[ 0%] Built target gen-pkgconfig
[ 1%] Built target libopenjp2
[ 1%] Built target opencv_videoio_plugins
[ 1%] Built target ittnotify
[ 2%] Built target ippiw
[ 5%] Built target opencv_core
[ 8%] Built target opencv_imgproc
[ 12%] Built target libwebp
[ 12%] Built target opencv_imgcodecs
[ 12%] Built target opencv_videoio
[ 12%] Built target opencv_highgui
[ 13%] Built target opencv_ts
[ 14%] Built target opencv_perf_core
[ 16%] Built target opencv_test_core
[ 16%] Built target opencv_flann
[ 16%] Built target opencv_test_flann
[ 17%] Built target opencv_perf_imgproc
[ 19%] Built target opencv_test_imgproc
[ 20%] Built target opencv_intensity_transform
[ 20%] Built target example_intensity_transform_intensity_transform
[ 20%] Built target opencv_test_intensity_transform
[ 20%] Built target opencv_ml
[ 21%] Built target opencv_test_ml
[ 21%] Built target opencv_phase_unwrapping
[ 21%] Built target example_phase_unwrapping_unwrap
[ 21%] Built target opencv_test_phase_unwrapping
[ 22%] Built target opencv_photo
[ 22%] Built target opencv_perf_photo
[ 23%] Built target opencv_test_photo
[ 23%] Built target opencv_plot
[ 23%] Built target example_plot_plot_demo
[ 23%] Built target opencv_quality
[ 23%] Built target opencv_test_quality
[ 23%] Built target example_quality_brisque_eval_tid2008
- To check the OpenCV installation for C++ binding.
pkg-config --modversion opencv4
- Here is the command output.
root@ip-172-31-27-203:~/opencv_build/opencv/build# pkg-config --modversion opencv4
4.4.0
- To check the OpenCV installation for Python.
python3 -c "import cv2; print(cv2.__version__)"
- Here is the command output.
root@ip-172-31-27-203:~/opencv_build/opencv/build# python3 -c "import cv2; print(cv2.__version__)"
4.4.0-dev
Step 7: OpenCV syntax & eamples:
- Syntax of Imread function in OpenCV.
cv2.imread(path, flag)
For example:
import cv2
# read image
img = cv2.imread(r'image_path', 1)
# save image
status = cv2.imwrite(r'image_path',img)
print("Image written sucess? : ", status)
- Syntax of Resize Image in opencv.
cv2.resize(s, size,fx,fy,interpolation)
For example:
import cv2
import numpy as np
#importing the opencv module
import cv2
# using imread('path') and 1 denotes read as color image
img = cv2.imread('dog.jpg',1)
print(img.shape)
img_resized=cv2.resize(img, (780, 540),
interpolation = cv2.INTER_NEAREST)
cv2.imshow("Resized",img_resized)
cv2.waitKey(0)
cv2.destroyAllWindows()