如何在 Ubuntu 20.04 上安装 OpenCV |
OpenCV 是一个开源、高度优化的计算机视觉库,广泛用于图像处理应用。该项目得到了大型开发人员和用户社区的支持。 OpenCV 具有许多内置的图像处理功能,可帮助您轻松开始使用不断改进的文档。在本指南中,我们将引导您完成在 Ubuntu 20.04 Linux 系统上安装 OpenCV 的步骤。
OpenCV 采用模块化结构开发,允许功能扩展。以下是项目中的主要模块,但还有许多额外的模块可用。
- 核。核心功能
- imgproc。图像处理
- img 编解码器。图像文件的读写
- 视频。视频输入/输出
- 高gui。高级图形用户界面
- 视频。视频分析
- calib3d。相机标定和 3D 重建
- 特征2d. 2D 特征框架
- 对象检测。物体检测
- dnn。深度神经网络模块
- 毫升。机器学习
- 弗兰。多维空间中的聚类和搜索
- 照片。计算摄影
- 缝合。图像拼接
- 加皮。图形API
在 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
您将看到一长串要安装的软件包,只需按y键即可继续安装。
.....
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/modules 中的模块。
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
现在,您已使用本指南中所示的任一方法在 Ubuntu 20.04 Linux 上安装了 OpenCV。有关更多详细信息,请参阅 OpenCV 文档。