如何在Ubuntu 20.04上安装OpenCV

这是续集。
您可以通过以下链接以PDF格式下载本文,以为我们提供支持。

以PDF格式下载指南


这是续集。

这是续集。
这是续集。

OpenCV是一个开源的,高度优化的计算机视觉库,可用于各种图像处理应用程序。该项目得到了大量开发人员和用户的支持。 OpenCV具有许多内置的图像处理功能,这些功能使随时间推移改进的文档入门变得容易。本指南介绍了在Ubuntu 20.04 Linux系统上安装OpenCV的步骤。

OpenCV是使用允许扩展功能的模块化结构开发的。以下是项目的主要模块,但也有许多其他模块。

在Ubuntu 20.04上安装OpenCV

您可以从Ubuntu 20.04上的预构建二进制文件安装OpenCV,或者,如果您需要该软件的最新发行版,则可以从源代码构建所需的软件包。我们将向您展示两种方法,以便您选择最适合自己的一种。

方法1:从预建的二进制文件在Ubuntu 20.04上安装OpenCV

更新您的Ubuntu系统,以获取最新软件包。

sudo apt update
sudo apt upgrade

如果您有内核更新,请考虑重新引导系统。

sudo systemctl reboot

备份系统后,使用以下命令安装OpenCV Python软件包。

sudo apt update
sudo apt install python3-opencv

将显示一长列要安装的软件包。 ÿ 按一个键继续安装。

.....
The following NEW packages will be installed:
  adwaita-icon-theme fontconfig fontconfig-config fonts-dejavu-core gdal-data gtk-update-icon-cache hicolor-icon-theme humanity-icon-theme libaec0
  libaom0 libarchive13 libarmadillo9 libarpack2 libatk-bridge2.0-0 libatk1.0-0 libatk1.0-data libatspi2.0-0 libavahi-client3 libavahi-common-data
  libavahi-common3 libavcodec58 libavformat58 libavutil56 libblas3 libbluray2 libcairo-gobject2 libcairo2 libcfitsio8 libcharls2 libchromaprint1
  libcodec2-0.9 libcolord2 libcups2 libcurl3-gnutls libdap25 libdapclient6v5 libdatrie1 libdc1394-22 libdrm-amdgpu1 libdrm-intel1 libdrm-nouveau2
  libdrm-radeon1 libepoxy0 libepsilon1 libevent-2.1-7 libevent-core-2.1-7 libevent-pthreads-2.1-7 libfabric1 libfontconfig1 libfreexl1 libfyba0
  libgdal26 libgdcm3.0 libgdk-pixbuf2.0-0 libgdk-pixbuf2.0-common libgeos-3.8.0 libgeos-c1v5 libgeotiff5 libgfortran5 libgif7 libgl1 libgl1-mesa-dri
  libgl2ps1.4 libglapi-mesa libglvnd0 libglx-mesa0 libglx0 libgme0 libgomp1 libgraphite2-3 libgsm1 libgstreamer-plugins-base1.0-0 libgtk-3-0
  libgtk-3-common libharfbuzz0b libhdf4-0-alt libhdf5-103 libhdf5-openmpi-103 libhwloc-plugins libhwloc15 libibverbs1 libice6 libilmbase24 libjbig0
  libjpeg-turbo8 libjpeg8 libjson-glib-1.0-0 libjson-glib-1.0-common libjsoncpp1 libkmlbase1 libkmldom1 libkmlengine1 liblapack3 liblcms2-2 liblept5
  libllvm9 libminizip1 libmp3lame0 libmpg123-0 libmysqlclient21 libnetcdf-c++4 libnetcdf15 libnl-route-3-200 libnspr4 libnss3 libodbc1 libogdi4.1
  libopencv-calib3d4.2 libopencv-contrib4.2 libopencv-core4.2 libopencv-dnn4.2 libopencv-features2d4.2 libopencv-flann4.2 libopencv-highgui4.2
  libopencv-imgcodecs4.2 libopencv-imgproc4.2 libopencv-ml4.2 libopencv-objdetect4.2 libopencv-photo4.2 libopencv-shape4.2 libopencv-stitching4.2
  libopencv-superres4.2 libopencv-video4.2 libopencv-videoio4.2 libopencv-videostab4.2 libopencv-viz4.2 libopenexr24 libopenjp2-7 libopenmpi3
  libopenmpt0 libopus0 liborc-0.4-0 libpango-1.0-0 libpangocairo-1.0-0 libpangoft2-1.0-0 libpciaccess0 libpixman-1-0 libpmix2 libpoppler97 libpq5
  libproj15 libprotobuf17 libpsm-infinipath1 libpsm2-2 libqhull7 libquadmath0 libraw1394-11 librdmacm1 librest-0.7-0 librsvg2-2 librsvg2-common
  libsensors-config libsensors5 libshine3 libsm6 libsnappy1v5 libsocket++1 libsoup-gnome2.4-1 libsoxr0 libspatialite7 libspeex1 libssh-gcrypt-4
  libsuperlu5 libswresample3 libswscale5 libsz2 libtbb2 libtesseract4 libthai-data libthai0 libtheora0 libtiff5 libtwolame0 liburiparser1 libva-drm2
  libva-x11-2 libva2 libvdpau1 libvorbisenc2 libvpx6 libvtk6.3 libvulkan1 libwavpack1 libwayland-client0 libwayland-cursor0 libwayland-egl1 libwebp6
  libwebpmux3 libx11-6 libx11-data libx11-xcb1 libx264-155 libx265-179 libxau6 libxcb-dri2-0 libxcb-dri3-0 libxcb-glx0 libxcb-present0 libxcb-render0
  libxcb-shm0 libxcb-sync1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxdmcp6 libxerces-c3.2 libxext6 libxfixes3 libxi6 libxinerama1
  libxkbcommon0 libxnvctrl0 libxrandr2 libxrender1 libxshmfence1 libxt6 libxvidcore4 libxxf86vm1 libzvbi-common libzvbi0 mysql-common
  ocl-icd-libopencl1 odbcinst odbcinst1debian2 proj-data python3-numpy python3-opencv ubuntu-mono x11-common
0 upgraded, 230 newly installed, 0 to remove and 0 not upgraded.
Need to get 131 MB of archives.
After this operation, 766 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

验证在Ubuntu 20.04上的OpenCV安装。

$ python3
Python 3.8.2 (default, Apr 27 2020, 15:53:34) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> import cv2 as cv
>>> print(cv.__version__)
4.2.0

如果您得到的结果没有任何错误,则表明您已在Ubuntu 20.04上成功安装了OpenCV。

方法2:从源代码生成并在Ubuntu 20.04上安装OpenCV

第一种方法不能保证您将在Ubuntu 20.04计算机上安装最新版本的OpenCV。另一种方法是根据源代码自己构建它。该方法需要一些依赖关系。

安装所需的依赖项。

sudo apt update
sudo apt install git gcc g++ ffmpeg cmake make python3-dev python3-numpy libavcodec-dev libavformat-dev libswscale-dev libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev libgtk-3-dev libpng-dev libjpeg-dev libopenexr-dev libtiff-dev libwebp-dev

从OpenCV下载最新资源 GitHub资料库..

cd ~/
git clone https://github.com/opencv/opencv.git

下载以包含其他模块 OpenCV的附加模块 资源。

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

创建一个构建目录。

cd ~/
cd opencv
mkdir build
cd build

构建OpenCV时,请包括以下模块。 〜/ Opencv_contrib /模块..

cmake -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules ..

OpenCV配置的输出样本。

.....
-- 
--   GUI: 
--     GTK+:                        YES (ver 3.24.18)
--       GThread :                  YES (ver 2.64.2)
--       GtkGlExt:                  NO
--     VTK support:                 NO
-- 
--   Media I/O: 
--     ZLib:                        /usr/lib/x86_64-linux-gnu/libz.so (ver 1.2.11)
--     JPEG:                        /usr/lib/x86_64-linux-gnu/libjpeg.so (ver 80)
--     WEBP:                        /usr/lib/x86_64-linux-gnu/libwebp.so (ver encoder: 0x020e)
--     PNG:                         /usr/lib/x86_64-linux-gnu/libpng.so (ver 1.6.37)
--     TIFF:                        /usr/lib/x86_64-linux-gnu/libtiff.so (ver 42 / 4.1.0)
--     JPEG 2000:                   build Jasper (ver 1.900.1)
--     OpenEXR:                     /usr/lib/x86_64-linux-gnu/libImath.so /usr/lib/x86_64-linux-gnu/libIlmImf.so /usr/lib/x86_64-linux-gnu/libIex.so /usr/lib/x86_64-linux-gnu/libHalf.so /usr/lib/x86_64-linux-gnu/libIlmThread.so (ver 2_3)
--     HDR:                         YES
--     SUNRASTER:                   YES
--     PXM:                         YES
--     PFM:                         YES
-- 
--   Video I/O:
--     DC1394:                      NO
--     FFMPEG:                      YES
--       avcodec:                   YES (58.54.100)
--       avformat:                  YES (58.29.100)
--       avutil:                    YES (56.31.100)
--       swscale:                   YES (5.5.100)
--       avresample:                NO
--     GStreamer:                   YES (1.16.2)
--     v4l/v4l2:                    YES (linux/videodev2.h)
-- 
--   Parallel framework:            pthreads
-- 
--   Trace:                         YES (with Intel ITT)
-- 
--   Other third-party libraries:
--     Intel IPP:                   2020.0.0 Gold [2020.0.0]
--            at:                   /root/opencv/build/3rdparty/ippicv/ippicv_lnx/icv
--     Intel IPP IW:                sources (2020.0.0)
--               at:                /root/opencv/build/3rdparty/ippicv/ippicv_lnx/iw
--     Lapack:                      NO
--     Eigen:                       NO
--     Custom HAL:                  NO
--     Protobuf:                    build (3.5.1)
-- 
--   OpenCL:                        YES (no extra features)
--     Include path:                /root/opencv/3rdparty/include/opencl/1.2
--     Link libraries:              Dynamic load
-- 
--   Python 3:
--     Interpreter:                 /usr/bin/python3 (ver 3.8.2)
--     Libraries:                   /usr/lib/x86_64-linux-gnu/libpython3.8.so (ver 3.8.2)
--     numpy:                       /usr/lib/python3/dist-packages/numpy/core/include (ver 1.17.4)
--     install path:                lib/python3.8/dist-packages/cv2/python-3.8
-- 
--   Python (for build):            /usr/bin/python3
-- 
--   Java:                          
--     ant:                         NO
--     JNI:                         NO
--     Java wrappers:               NO
--     Java tests:                  NO
-- 
--   Install to:                    /usr/local
-- -----------------------------------------------------------------
-- 
-- Configuring done
-- Generating done
-- Build files have been written to: /root/opencv/build

接下来,在Ubuntu 20.04上构建并安装OpenCV。

make -j $(nproc)
sudo make install

安装将需要几分钟,因此请耐心等待。

......
Scanning dependencies of target example_tutorial_pointPolygonTest_demo
[100%] Built target example_tutorial_generalContours_demo2
Scanning dependencies of target example_tutorial_cornerDetector_Demo
[100%] Building CXX object samples/cpp/CMakeFiles/example_tutorial_pointPolygonTest_demo.dir/tutorial_code/ShapeDescriptors/pointPolygonTest_demo.cpp.o
[100%] Building CXX object samples/cpp/CMakeFiles/example_tutorial_cornerDetector_Demo.dir/tutorial_code/TrackingMotion/cornerDetector_Demo.cpp.o
[100%] Linking CXX executable ../../bin/example_tutorial_porting_anisotropic_image_segmentation_gapi
[100%] Linking CXX executable ../../bin/example_cpp_dft
[100%] Built target example_tutorial_porting_anisotropic_image_segmentation_gapi
Scanning dependencies of target example_tutorial_periodic_noise_removing_filter
[100%] Building CXX object samples/cpp/CMakeFiles/example_tutorial_periodic_noise_removing_filter.dir/tutorial_code/ImgProc/periodic_noise_removing_filter/periodic_noise_removing_filter.cpp.o
[100%] Linking CXX executable ../../bin/example_tutorial_cornerDetector_Demo
[100%] Built target example_cpp_dft
[100%] Linking CXX executable ../../bin/example_tutorial_pointPolygonTest_demo
Scanning dependencies of target example_tutorial_cornerSubPix_Demo
[100%] Building CXX object samples/cpp/CMakeFiles/example_tutorial_cornerSubPix_Demo.dir/tutorial_code/TrackingMotion/cornerSubPix_Demo.cpp.o
[100%] Built target example_tutorial_cornerDetector_Demo
[100%] Built target example_tutorial_pointPolygonTest_demo
[100%] Linking CXX executable ../../bin/example_tutorial_cornerSubPix_Demo
[100%] Linking CXX executable ../../bin/example_tutorial_periodic_noise_removing_filter
[100%] Built target example_tutorial_cornerSubPix_Demo
[100%] Built target example_tutorial_periodic_noise_removing_filter

.........
-- Installing: /usr/local/share/opencv4/samples/python/lk_homography.py
-- Installing: /usr/local/share/opencv4/samples/python/lk_track.py
-- Installing: /usr/local/share/opencv4/samples/python/logpolar.py
-- Installing: /usr/local/share/opencv4/samples/python/morphology.py
-- Installing: /usr/local/share/opencv4/samples/python/mosse.py
-- Installing: /usr/local/share/opencv4/samples/python/mouse_and_match.py
-- Installing: /usr/local/share/opencv4/samples/python/mser.py
-- Installing: /usr/local/share/opencv4/samples/python/opencv_version.py
-- Installing: /usr/local/share/opencv4/samples/python/opt_flow.py
-- Installing: /usr/local/share/opencv4/samples/python/peopledetect.py
-- Installing: /usr/local/share/opencv4/samples/python/plane_ar.py
-- Installing: /usr/local/share/opencv4/samples/python/plane_tracker.py
-- Installing: /usr/local/share/opencv4/samples/python/qrcode.py
-- Installing: /usr/local/share/opencv4/samples/python/squares.py
-- Installing: /usr/local/share/opencv4/samples/python/stereo_match.py
-- Installing: /usr/local/share/opencv4/samples/python/stitching.py
-- Installing: /usr/local/share/opencv4/samples/python/stitching_detailed.py
-- Installing: /usr/local/share/opencv4/samples/python/text_skewness_correction.py
-- Installing: /usr/local/share/opencv4/samples/python/texture_flow.py
-- Installing: /usr/local/share/opencv4/samples/python/tst_scene_render.py
-- Installing: /usr/local/share/opencv4/samples/python/turing.py
-- Installing: /usr/local/share/opencv4/samples/python/video.py
-- Installing: /usr/local/share/opencv4/samples/python/video_threaded.py
-- Installing: /usr/local/share/opencv4/samples/python/video_v4l2.py
-- Installing: /usr/local/share/opencv4/samples/python/watershed.py

所有文件都将安装在“ / usr / local /”目录中。

$ python3
Python 3.8.2 (default, Apr 27 2020, 15:53:34) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> import cv2 as cv
>>> print(cv.__version__)
4.3.0-dev

OpenCV使用本指南中显示的方法之一安装在Ubuntu 20.04 Linux上。参考 OpenCV文档 欲获得更多信息。

这是续集。
您可以通过以下链接以PDF格式下载本文,以为我们提供支持。

以PDF格式下载指南


这是续集。

这是续集。
这是续集。

Sidebar