Skip to content


Installing OpenCV 4.5.0 in Ubuntu 20.04 LTS

OpenCV was initially released about 20 years ago. It’s one of the most well established computer vision libraries in the world, with thousands of algorithm implementations ready to be used in commercial and research applications.

In this guide I’ll show you how to install OpenCV 4.5.0 in your Ubuntu 20.04 LTS and how to create computer vision applications with C++ and Python.

face detection

Note: I have noticed some copies of my posts elsewhere, so make sure that you are reading this from the original source, at samontab dot com, accessible from here so that you don’t miss the comments.

First, make sure you have the latest software installed:

sudo apt-get update
sudo apt-get upgrade

Now, you need to install some dependencies, such as support for reading and writing video files, drawing on the screen, some needed tools, etc… This step is very easy, you only need to write the following command in the Terminal:

sudo apt-get install build-essential cmake python3-numpy python3-dev python3-tk libavcodec-dev libavformat-dev libavutil-dev libswscale-dev libavresample-dev libdc1394-dev libeigen3-dev libgtk-3-dev libvtk7-qt-dev

Time to download and compile OpenCV 4.5.0:

wget https://github.com/opencv/opencv/archive/4.5.0.tar.gz
tar -xvzf 4.5.0.tar.gz
rm 4.5.0.tar.gz
cd opencv-4.5.0
mkdir build;cd build
cmake -DBUILD_EXAMPLES=ON ..

configuration for opencv

Check that the above command produces no error and that in particular it reports FFMPEG as YES. If this is not the case you will not be able to read or write videos. Double check that every feature you want present in OpenCV is reported correctly. If you’re missing something, make sure to install the missing package and run the cmake line again. In some cases you might need to delete the whole build directory first to get a correct detection. Once you’re happy with what’s shown then we’ll build it. After running the following lines you can go grab a coffee as it will take a long time.

make -j4
sudo make install
echo '/usr/local/lib' | sudo tee --append /etc/ld.so.conf.d/opencv.conf
sudo ldconfig
echo 'PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig' | sudo tee --append ~/.bashrc
echo 'export PKG_CONFIG_PATH' | sudo tee --append ~/.bashrc
source ~/.bashrc

After that’s finished you should be able to use OpenCV with C++ and Python. Let’s do a quick check:

python3
import cv2
cv2.__version__

python_opencv

You should see no errors and 4.5.0 as the version.

Now let’s run some Python demos. You can start all of them from a central place:

cd ~/opencv-4.5.0/samples/python
python3 demo.py

python examples

You can also execute any of the already compiled C++ demos. For example let’s run the intelligent scissors demo:

cd ~/opencv-4.5.0/build/bin
./example_cpp_intelligent_scissors

If you click on the image and move your mouse you’ll see that the selection moves to match the object edges. Once you are ready selecting the contour of the object press right click and the object will be selected.

intelligent scissors

Now you’ll learn how to compile an application that uses OpenCV with C++ using cmake. Let’s copy another example source code and prepare it for compilation.

cd ~
mkdir sample
cd sample
cp ~/opencv-4.5.0/samples/cpp/text_skewness_correction.cpp .
cp ~/opencv-4.5.0/samples/data/imageTextR.png .

Now we’re going to create a cmake file that will allow us to compile the sample. Create a text file called CMakeLists.txt and put this content in it:

cmake_minimum_required (VERSION 3.0.2)
project (text_skewness_correction)
find_package(OpenCV REQUIRED)
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable (${PROJECT_NAME} text_skewness_correction.cpp)
target_link_libraries (${PROJECT_NAME} ${OpenCV_LIBS})

OK, now we’re ready to build it and run it:

mkdir build
cd build
cmake ..
make
./text_skewness_correction ../imageTextR.png

rotated text alignment

Now you have OpenCV 4.5.0 correctly installed in your Ubuntu 20.04 LTS system, and you know how to create applications using C++ and Python.

Posted in Computer Vision, Image Processing, Open Source, OpenCV.

Tagged with , , , , , , , , , .


One Response

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

Continuing the Discussion

  1. Installing OpenCV 3.2.0 with contrib modules in Ubuntu 16.04 LTS – Sebastian Montabone linked to this post on November 8, 2020

    […] UPDATE: You can also install OpenCV 4.5.0 in Ubuntu 20.04LTS. […]



Some HTML is OK

or, reply to this post via trackback.