目录
一、概述
二、安装
2.1 从预编译文件安装
2.2从源码安装
OpenCV是开源、高度优化的计算机视觉库,目前,在众多图像相关领域都会使用Opencv进行处理。Opencv有很多现成的图像处理函数可以帮助我们快速实现相关工程算法落地,并且效率和稳定性均有较好的保障。尤其近来,深度学习得到了大力的推广,在高版本的OpenCV中也集成了深度学习的相关功能,因此选择OpenCV作为研究或者部署的图像处理工具再适合不过。下面是一些opencv的一些核心功能组件:
本教程面向Ubuntu20.04操作系统,讲解如何在Ubuntu中安装最新的opencv库,并最终在Python中实现调用。
具体有两种安装方式。第一种就是采用预先编译好的二值包进行安装,第二种就是利用源码重新编译安装。总体来说,第一种方式安装比较简单,但是不能保证安装的opencv是最新版。第二种方式安装比较繁琐,但是可以自由选择版本,尤其是最新的opencv。下面分两种方式进行讲解。
首先更新系统:
sudo apt update
sudo apt upgrade
更新完以后最好重启以下:
sudo systemctl reboot
接下来开始安装opencv:
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
这时候直接输入”y“键按回车即可。安装完成后可以使用python进行测试:
$ 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
此时,已经安装完成。
首先安装环境依赖:
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
然后从github上下载最新的opencv:
cd ~/
git clone https://github.com/opencv/opencv.git
建立build目录:
cd ~/
cd opencv
mkdir build
cd build
包含相关模块:
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
最后进行安装:
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