Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to install OpenCV on Ubuntu 20.04

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how to install OpenCV on Ubuntu 20.04. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Install OpenCV from the Ubuntu warehouse

OpenCV can be installed from the default Ubuntu 20.04 warehouse. To install it, run:

Linuxmi@linuxmi:~/www.linuxmi.com$ sudo apt install libopencv-dev python3-opencv

The above command installs all the packages needed to run OpenCV.

Verify the installation by importing the cv2 module and printing the OpenCV version:

Linuxmi@linuxmi:~/www.linuxmi.com$ python3-c "import cv2; print (cv2.__version__)"

At the time of this writing, the version in the repository is 4.2:

Output:

4.2.0

Install OpenCV from source code

Building OpenCV libraries from source code gives you the latest available version. It will be optimized for your specific system, and you will have complete control over the build options. This is the recommended way to install OpenCV.

Perform the following steps to install the latest OpenCV version from the source code:

Install the build tools and dependencies:

Linuxmi@linuxmi:~/www.linuxmi.com$ sudo 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

Clone OpenCV and OpenCV contrib repositories:

Mkdir ~ / opencv_build & & cd ~ / opencv_build git clone https://github.com/opencv/opencv.git git clone https://github.com/opencv/opencv_contrib.git

At the time of this writing, the default version in the github repository is 4.3.0. If you are installing an older version of OpenCV, open both the opencv and opencv_contrib directories on cd, and then run git checkout

When the download is complete, create a temporary build directory and navigate to it:

Cd ~ / opencv_build/opencv mkdir-p build & & cd build

Use CMake to set up 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 output will look like this:

-- Configuring done-- Generating done-- Build files have been written to: / home/vagrant/opencv_build/opencv/build

Start the compilation process:

Make-J8

Modify the-j flag based on your processor. If you don't know the number of cores of the processor, you can find it by typing nproc.

Depending on your system configuration, it may take several minutes or more to compile.

Install OpenCV using the following methods:

Sudo make install

To verify the installation, type the following command, and you will see the OpenCV version.

C++ binding:

Pkg-config-modversion opencv4

Output

4.2.0

Python binding:

Python3-c "import cv2; print (cv2.__version__)"

Output

4.3.0-dev, thank you for your reading! This is the end of this article on "how to install OpenCV on Ubuntu 20.04". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report