這篇文章將說明在 Ubuntu Linux 系統(tǒng)上搭建 OpenCV 開發(fā)環(huán)境的過程驯击,以支持 Python 和 C++ 編程語言開發(fā)基于 OpenCV 的應(yīng)用,或?qū)W習(xí) OpenCV。本文說明的過程在 Ubuntu 20.04 版本的系統(tǒng)上經(jīng)過測試驗(yàn)證碍讯。
在 Ubuntu 系統(tǒng)上安裝 OpenCV-Python
在 Ubuntu 系統(tǒng)中安裝 OpenCV-Python 有兩種方式:
- 從 Ubuntu 倉庫安裝可用的預(yù)編譯二進(jìn)制文件
- 由源碼編譯啤它。
這里這兩種方式都會(huì)介紹笑撞。
另一個(gè)非常重要的問題是必須的附加庫黄伊。OpenCV-Python 只需要 Numpy。但在這個(gè)教程中派殷,我們還使用 Matplotlib 進(jìn)行一些簡單而漂亮的繪圖还最。Matplotlib 是可選的,但強(qiáng)烈建議安裝它毡惜。類似地拓轻,我們也將看到 IPython,一個(gè)交互式 Python 終端经伙,同樣強(qiáng)烈建議安裝它扶叉。
從預(yù)編譯二進(jìn)制文件安裝 OpenCV-Python
在僅僅想要編程和開發(fā) OpenCV 應(yīng)用程序時(shí),這個(gè)方法最好帕膜。
在終端中通過如下命令安裝 python3-opencv 包(以 root 用戶):
$ sudo apt-get install python3-opencv
正在讀取軟件包列表... 完成
正在分析軟件包的依賴關(guān)系樹
正在讀取狀態(tài)信息... 完成
下列軟件包是自動(dòng)安裝的并且現(xiàn)在不需要了:
ibus-data python3-ibus-1.0
使用'sudo apt autoremove'來卸載它(它們)枣氧。
將會(huì)同時(shí)安裝下列軟件:
cpp-8 gcc-8 gcc-8-base gdal-data gfortran gfortran-8 gfortran-9 ibverbs-providers libaec0 libarmadillo9 libarpack2 libcaf-openmpi-3
libcfitsio8 libcharls2 libcoarrays-dev libcoarrays-openmpi-dev libdap25 libdapclient6v5 libepsilon1 libevent-core-2.1-7 libevent-dev
libevent-extra-2.1-7 libevent-openssl-2.1-7 libevent-pthreads-2.1-7 libfabric1 libfreexl1 libfyba0 libgcc-8-dev libgdal26 libgdcm3.0
libgeos-3.8.0 libgeos-c1v5 libgeotiff5 libgfortran-8-dev libgfortran-9-dev libgl2ps1.4 libhdf4-0-alt libhdf5-103 libhdf5-openmpi-103
libhwloc-dev libhwloc-plugins libhwloc15 libibverbs-dev libibverbs1 libilmbase24 libkmlbase1 libkmldom1 libkmlengine1 liblept5 libminizip1
libmpx2 libnetcdf-c++4 libnetcdf15 libnl-3-dev libnl-route-3-dev libnuma-dev 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 libopenmpi-dev libopenmpi3
libpmix2 libpq5 libproj15 libpsm-infinipath1 libpsm2-2 libqhull7 librdmacm1 libsocket++1 libspatialite7 libsuperlu5 libsz2 libtbb2
libtesseract4 liburiparser1 libvtk6.3 libxerces-c3.2 libxnvctrl0 odbcinst odbcinst1debian2 openmpi-bin openmpi-common proj-bin proj-data
python3-numpy
建議安裝:
gcc-8-locales gcc-8-multilib gcc-8-doc gfortran-multilib gfortran-doc gfortran-8-multilib gfortran-8-doc gfortran-9-multilib gfortran-9-doc
geotiff-bin gdal-bin libgeotiff-epsg libhdf4-doc libhdf4-alt-dev hdf4-tools libhwloc-contrib-plugins libmyodbc odbc-postgresql tdsodbc
unixodbc-bin ogdi-bin openmpi-doc mpi-default-bin python-numpy-doc python3-pytest python3-numpy-dbg
下列【新】軟件包將被安裝:
cpp-8 gcc-8 gcc-8-base gdal-data gfortran gfortran-8 gfortran-9 ibverbs-providers libaec0 libarmadillo9 libarpack2 libcaf-openmpi-3
libcfitsio8 libcharls2 libcoarrays-dev libcoarrays-openmpi-dev libdap25 libdapclient6v5 libepsilon1 libevent-core-2.1-7 libevent-dev
libevent-extra-2.1-7 libevent-openssl-2.1-7 libevent-pthreads-2.1-7 libfabric1 libfreexl1 libfyba0 libgcc-8-dev libgdal26 libgdcm3.0
libgeos-3.8.0 libgeos-c1v5 libgeotiff5 libgfortran-8-dev libgfortran-9-dev libgl2ps1.4 libhdf4-0-alt libhdf5-103 libhdf5-openmpi-103
libhwloc-dev libhwloc-plugins libhwloc15 libibverbs-dev libibverbs1 libilmbase24 libkmlbase1 libkmldom1 libkmlengine1 liblept5 libminizip1
libmpx2 libnetcdf-c++4 libnetcdf15 libnl-3-dev libnl-route-3-dev libnuma-dev 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 libopenmpi-dev libopenmpi3
libpmix2 libpq5 libproj15 libpsm-infinipath1 libpsm2-2 libqhull7 librdmacm1 libsocket++1 libspatialite7 libsuperlu5 libsz2 libtbb2
libtesseract4 liburiparser1 libvtk6.3 libxerces-c3.2 libxnvctrl0 odbcinst odbcinst1debian2 openmpi-bin openmpi-common proj-bin proj-data
python3-numpy python3-opencv
升級了 0 個(gè)軟件包,新安裝了 105 個(gè)軟件包垮刹,要卸載 0 個(gè)軟件包达吞,有 70 個(gè)軟件包未被升級。
需要下載 104 MB 的歸檔荒典。
解壓縮后會(huì)消耗 397 MB 的額外空間酪劫。
您希望繼續(xù)執(zhí)行嗎吞鸭? [Y/n] y
獲取:1 http://cn.archive.ubuntu.com/ubuntu focal/universe amd64 gcc-8-base amd64 8.4.0-3ubuntu2 [18.7 kB]
. . . . . .
它會(huì)安裝非常多在 Python 中調(diào)用 OpenCV 接口所需的依賴,如 libopencv-core4.2
覆糟、libopencv-superres4.2
刻剥、libopencv-objdetect4.2
和 libopencv-features2d4.2
等。注意滩字,安裝的這些 OpenCV 依賴庫的包只包含了動(dòng)態(tài)鏈接庫二進(jìn)制文件造虏,而沒有 C++ OpenCV 開發(fā)所需要的頭文件等內(nèi)容。
打開一個(gè) Python IDLE (或者 IPython)并在 Python 終端中輸入如下代碼:
import cv2 as cv
print(cv.__version__)
執(zhí)行這段代碼踢械,輸出了適當(dāng)?shù)?OpenCV 版本號而沒有任何報(bào)錯(cuò)酗电,則恭喜你!D诹小撵术!你已經(jīng)成功地安裝了 OpenCV-Python。
這很簡單话瞧。但這有一個(gè)問題嫩与。Apt 倉庫可能總是不包含最新版本的 OpenCV。比如筆者在 Ubuntu 20.04 上通過上面的操作安裝 OpenCV交排,得到的版本為 4.2.0划滋,最新的版本則為 4.5.5:
$ python3
Python 3.8.10 (default, Nov 26 2021, 20:14:08)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> print(cv2.__version__)
4.2.0
對于 Python API,最新版本將始終包含更好的支持和最新的錯(cuò)誤修復(fù)埃篓。
因此处坪,獲取最新源代碼的下一個(gè)方法,即從源代碼編譯架专。同時(shí)同窘,如果在某個(gè)時(shí)間點(diǎn),你想為 OpenCV 貢獻(xiàn)代碼部脚,你也將需要這個(gè)想邦。
從源碼構(gòu)建
乍一看去,從源碼編譯可能有點(diǎn)復(fù)雜委刘,但一旦你成功了丧没,你會(huì)發(fā)現(xiàn)其實(shí)也沒什么復(fù)雜的。
首先我們將安裝一些依賴锡移。有些是必須的呕童,有些是可選的。如果你不需要罩抗,可以跳過可選的依賴拉庵。
必須的構(gòu)建依賴
我們需要 CMake 來配置安裝,需要 GCC 來編譯。需要 Python-devel 和 Numpy 來構(gòu)建 Python 綁定等钞支。
sudo apt-get install cmake
sudo apt-get install gcc g++
要支持 python2:
sudo apt-get install python-dev python-numpy
要支持 python3:
sudo apt-get install python3-dev python3-numpy
接著茫蛹,需要 GTK 來支持 GUI 功能,Camera 支持 (v4l)烁挟,媒體支持 (ffmpeg婴洼,gstreamer) 等等。
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev
要支持 gtk2:
sudo apt-get install libgtk2.0-dev
要支持 gtk3:
sudo apt-get install libgtk-3-dev
可選的依賴
對于在 Ubuntu 機(jī)器上安裝 OpenCV撼嗓,上面的依賴已經(jīng)足夠了柬采。但根據(jù)你的需求,你可能需要一些額外的依賴且警。這樣的額外依賴列表如下粉捻。你可以根據(jù)你的需求把它們留在那里或安裝它們。
OpenCV 附帶了對圖像格式的支持斑芜,如 PNG肩刃、JPEG、JPEG2000杏头、TIFF盈包、WebP 等。但它可能有點(diǎn)老醇王。如果你想獲得最新的庫呢燥,你可以為這些格式的系統(tǒng)庫安裝開發(fā)文件。
sudo apt-get install libpng-dev
sudo apt-get install libjpeg-dev
sudo apt-get install libopenexr-dev
sudo apt-get install libtiff-dev
sudo apt-get install libwebp-dev
注意: 如果你正在使用 Ubuntu 16.04寓娩,也可以安裝
libjasper-dev
來給你的系統(tǒng)添加對 JPEG2000 格式的系統(tǒng)級支持叛氨。
下載 OpenCV
獲得 OpenCV 的源碼有兩種方式:
- 從 OpenCV 的 Github 倉庫 下載最新的源碼。(如果你想要給 OpenCV 貢獻(xiàn)代碼也可以通過它棘伴。要下載源碼力试,首先需要安裝 Git。)
$ sudo apt-get install git
$ git clone https://github.com/opencv/opencv.git
這將在當(dāng)前目錄創(chuàng)建一個(gè)名為 "opencv" 的目錄排嫌。代碼克隆過程可能需要一些時(shí)間,這依賴于你的網(wǎng)絡(luò)連接情況缰犁。
- 除了直接克隆 OpenCV 的 Github 倉庫 之外淳地,還有另外一種獲取 OpenCV 源碼的方法。OpenCV 官方提供了已經(jīng)打包好的發(fā)行版源碼的壓縮包帅容,可以下載這些壓縮包獲取源碼颇象,如:
# Install minimal prerequisites
$ sudo apt update && sudo apt install -y wget unzip
# Download and unpack sources
$ wget -O opencv.zip https://github.com/opencv/opencv/archive/4.x.zip
$ unzip opencv.zip
$ mv opencv-4.x opencv
解壓之后并重命名源碼目錄,OpenCV 源碼將位于 opencv 目錄下并徘。
現(xiàn)在打開一個(gè)終端窗口遣钳,切換到下載的 "opencv" 目錄。創(chuàng)建一個(gè)名為 "build" 的目錄并切換到它下面麦乞。
$ mkdir build
$ cd build
當(dāng)然也可以在 OpenCV 源碼目錄的同級目錄中創(chuàng)建 "build" 目錄蕴茴,這同樣需要切換到新建的目錄下劝评。注意,后面執(zhí)行 CMake 配置構(gòu)建時(shí)倦淀,需要正確引用 OpenCV 源碼目錄蒋畜。
配置和安裝
現(xiàn)在我們已經(jīng)擁有了所有必須的依賴,讓我們開始安裝 OpenCV 吧撞叽。安裝必須通過 CMake 來配置姻成。它指定了將要安裝哪些模塊、安裝路徑愿棋、將使用哪些附加庫科展、是否要編譯文檔和示例等。這些工作中的大部分將通過默認(rèn)參數(shù)的良好配置自動(dòng)完成糠雨。
以下命令通常用于配置 OpenCV 庫構(gòu)建(在 "build" 目錄中執(zhí)行):
$ cmake ../
OpenCV 默認(rèn)假設(shè)構(gòu)建類型為 "Release"才睹,安裝路徑為 "/usr/local"。關(guān)于 CMake 選項(xiàng)的其它信息請參考 OpenCV C++ 編譯指南见秤。
你應(yīng)該能夠在你的 CMake 輸出中看到如下這些行(它們意味著已經(jīng)發(fā)現(xiàn)了適當(dāng)?shù)?Python):
-- Other third-party libraries:
-- VA: YES
-- Lapack: NO
-- Eigen: NO
-- Custom HAL: NO
-- Protobuf: build (3.19.1)
--
-- OpenCL: YES (INTELVA)
-- Include path: /media/data/my_multimedia/opencv-4.x/3rdparty/include/opencl/1.2
-- Link libraries: Dynamic load
--
-- Python 2:
-- Interpreter: /usr/bin/python2.7 (ver 2.7.18)
-- Libraries: /usr/lib/x86_64-linux-gnu/libpython2.7.so (ver 2.7.18)
-- numpy: /usr/lib/python2.7/dist-packages/numpy/core/include (ver 1.16.5)
-- install path: lib/python2.7/dist-packages/cv2/python-2.7
--
-- Python 3:
-- Interpreter: /usr/bin/python3 (ver 3.8.10)
-- Libraries: /usr/lib/x86_64-linux-gnu/libpython3.8.so (ver 3.8.10)
-- numpy: /usr/lib/python3/dist-packages/numpy/core/include (ver 1.17.4)
-- install path: lib/python3.8/site-packages/cv2/python-3.8
--
-- Python (for build): /usr/bin/python2.7
--
-- Java:
-- ant: NO
-- JNI: /usr/lib/jvm/default-java/include /usr/lib/jvm/default-java/include/linux /usr/lib/jvm/default-java/include
-- Java wrappers: NO
-- Java tests: NO
--
-- Install to: /usr/local
cmake
命令的參數(shù)是包含 CMakelists.txt 文件的目錄的路徑砂竖,即源碼目錄。如果 "build" 目錄在 "opencv" 源碼目錄的同級目錄中創(chuàng)建鹃答,則為(在 "build" 目錄中執(zhí)行):
# Configure
$ cmake ../opencv
現(xiàn)在可以使用 "make" 命令來構(gòu)建文件:
$ make
. . . . . .
Scanning dependencies of target opencv_version
[100%] Building CXX object apps/version/CMakeFiles/opencv_version.dir/opencv_version.cpp.o
[100%] Linking CXX executable ../../bin/opencv_version
[100%] Built target opencv_version
Scanning dependencies of target opencv_model_diagnostics
[100%] Building CXX object apps/model-diagnostics/CMakeFiles/opencv_model_diagnostics.dir/model_diagnostics.cpp.o
[100%] Linking CXX executable ../../bin/opencv_model_diagnostics
[100%] Built target opencv_model_diagnostics
構(gòu)建也可以通過 cmake
命令完成:
$ cmake --build .
成功構(gòu)建之后乎澄,可以在 build/lib
目錄下找到庫文件,并在 build/bin
目錄下找到可執(zhí)行文件(測試测摔,示例置济,和 apps):
build$ ls bin
opencv_annotation opencv_perf_optflow opencv_test_bgsegm opencv_test_intensity_transform opencv_test_stitching
opencv_interactive-calibration opencv_perf_photo opencv_test_bioinspired opencv_test_line_descriptor opencv_test_structured_light
opencv_model_diagnostics opencv_perf_reg opencv_test_calib3d opencv_test_mcc opencv_test_superres
opencv_perf_aruco opencv_perf_rgbd opencv_test_core opencv_test_ml opencv_test_text
opencv_perf_bioinspired opencv_perf_stereo opencv_test_dnn opencv_test_objdetect opencv_test_tracking
opencv_perf_calib3d opencv_perf_stitching opencv_test_dnn_superres opencv_test_optflow opencv_test_video
opencv_perf_core opencv_perf_superres opencv_test_face opencv_test_phase_unwrapping opencv_test_videoio
opencv_perf_dnn opencv_perf_tracking opencv_test_features2d opencv_test_photo opencv_test_videostab
opencv_perf_dnn_superres opencv_perf_video opencv_test_flann opencv_test_quality opencv_test_wechat_qrcode
opencv_perf_features2d opencv_perf_videoio opencv_test_fuzzy opencv_test_rapid opencv_test_xfeatures2d
opencv_perf_gapi opencv_perf_xfeatures2d opencv_test_gapi opencv_test_reg opencv_test_ximgproc
opencv_perf_imgcodecs opencv_perf_ximgproc opencv_test_highgui opencv_test_rgbd opencv_test_xphoto
opencv_perf_imgproc opencv_perf_xphoto opencv_test_imgcodecs opencv_test_saliency opencv_version
opencv_perf_line_descriptor opencv_test_aruco opencv_test_img_hash opencv_test_shape opencv_visualisation
opencv_perf_objdetect opencv_test_barcode opencv_test_imgproc opencv_test_stereo opencv_waldboost_detector
build$ ls lib
cv2.so libopencv_features2d.so.405 libopencv_objdetect.so libopencv_structured_light.so.4.5.5
libopencv_aruco.so libopencv_features2d.so.4.5.5 libopencv_objdetect.so.405 libopencv_superres.so
libopencv_aruco.so.405 libopencv_flann.so libopencv_objdetect.so.4.5.5 libopencv_superres.so.405
libopencv_aruco.so.4.5.5 libopencv_flann.so.405 libopencv_optflow.so libopencv_superres.so.4.5.5
libopencv_barcode.so libopencv_flann.so.4.5.5 libopencv_optflow.so.405 libopencv_surface_matching.so
libopencv_barcode.so.405 libopencv_freetype.so libopencv_optflow.so.4.5.5 libopencv_surface_matching.so.405
libopencv_barcode.so.4.5.5 libopencv_freetype.so.405 libopencv_phase_unwrapping.so libopencv_surface_matching.so.4.5.5
libopencv_bgsegm.so libopencv_freetype.so.4.5.5 libopencv_phase_unwrapping.so.405 libopencv_text.so
libopencv_bgsegm.so.405 libopencv_fuzzy.so libopencv_phase_unwrapping.so.4.5.5 libopencv_text.so.405
libopencv_bgsegm.so.4.5.5 libopencv_fuzzy.so.405 libopencv_photo.so libopencv_text.so.4.5.5
libopencv_bioinspired.so libopencv_fuzzy.so.4.5.5 libopencv_photo.so.405 libopencv_tracking.so
libopencv_bioinspired.so.405 libopencv_gapi.so libopencv_photo.so.4.5.5 libopencv_tracking.so.405
libopencv_bioinspired.so.4.5.5 libopencv_gapi.so.405 libopencv_plot.so libopencv_tracking.so.4.5.5
libopencv_calib3d.so libopencv_gapi.so.4.5.5 libopencv_plot.so.405 libopencv_ts.a
libopencv_calib3d.so.405 libopencv_hfs.so libopencv_plot.so.4.5.5 libopencv_videoio.so
libopencv_calib3d.so.4.5.5 libopencv_hfs.so.405 libopencv_quality.so libopencv_videoio.so.405
libopencv_ccalib.so libopencv_hfs.so.4.5.5 libopencv_quality.so.405 libopencv_videoio.so.4.5.5
libopencv_ccalib.so.405 libopencv_highgui.so libopencv_quality.so.4.5.5 libopencv_video.so
libopencv_ccalib.so.4.5.5 libopencv_highgui.so.405 libopencv_rapid.so libopencv_video.so.405
libopencv_core.so libopencv_highgui.so.4.5.5 libopencv_rapid.so.405 libopencv_video.so.4.5.5
libopencv_core.so.405 libopencv_imgcodecs.so libopencv_rapid.so.4.5.5 libopencv_videostab.so
libopencv_core.so.4.5.5 libopencv_imgcodecs.so.405 libopencv_reg.so libopencv_videostab.so.405
libopencv_datasets.so libopencv_imgcodecs.so.4.5.5 libopencv_reg.so.405 libopencv_videostab.so.4.5.5
libopencv_datasets.so.405 libopencv_img_hash.so libopencv_reg.so.4.5.5 libopencv_wechat_qrcode.so
libopencv_datasets.so.4.5.5 libopencv_img_hash.so.405 libopencv_rgbd.so libopencv_wechat_qrcode.so.405
libopencv_dnn_objdetect.so libopencv_img_hash.so.4.5.5 libopencv_rgbd.so.405 libopencv_wechat_qrcode.so.4.5.5
libopencv_dnn_objdetect.so.405 libopencv_imgproc.so libopencv_rgbd.so.4.5.5 libopencv_xfeatures2d.so
libopencv_dnn_objdetect.so.4.5.5 libopencv_imgproc.so.405 libopencv_saliency.so libopencv_xfeatures2d.so.405
libopencv_dnn.so libopencv_imgproc.so.4.5.5 libopencv_saliency.so.405 libopencv_xfeatures2d.so.4.5.5
libopencv_dnn.so.405 libopencv_intensity_transform.so libopencv_saliency.so.4.5.5 libopencv_ximgproc.so
libopencv_dnn.so.4.5.5 libopencv_intensity_transform.so.405 libopencv_shape.so libopencv_ximgproc.so.405
libopencv_dnn_superres.so libopencv_intensity_transform.so.4.5.5 libopencv_shape.so.405 libopencv_ximgproc.so.4.5.5
libopencv_dnn_superres.so.405 libopencv_line_descriptor.so libopencv_shape.so.4.5.5 libopencv_xobjdetect.so
libopencv_dnn_superres.so.4.5.5 libopencv_line_descriptor.so.405 libopencv_stereo.so libopencv_xobjdetect.so.405
libopencv_dpm.so libopencv_line_descriptor.so.4.5.5 libopencv_stereo.so.405 libopencv_xobjdetect.so.4.5.5
libopencv_dpm.so.405 libopencv_mcc.so libopencv_stereo.so.4.5.5 libopencv_xphoto.so
libopencv_dpm.so.4.5.5 libopencv_mcc.so.405 libopencv_stitching.so libopencv_xphoto.so.405
libopencv_face.so libopencv_mcc.so.4.5.5 libopencv_stitching.so.405 libopencv_xphoto.so.4.5.5
libopencv_face.so.405 libopencv_ml.so libopencv_stitching.so.4.5.5 python3
libopencv_face.so.4.5.5 libopencv_ml.so.405 libopencv_structured_light.so
libopencv_features2d.so libopencv_ml.so.4.5.5 libopencv_structured_light.so.405
編譯出來的可執(zhí)行文件和庫文件非常多。
CMake 包文件位于構(gòu)建根目錄:
build$ ls OpenCVConfig*.cmake
OpenCVConfig.cmake OpenCVConfig-version.cmake
build$ ls OpenCVModules.cmake
OpenCVModules.cmake
它們用于支持 CMake 的 find_library锋八。
默認(rèn)情況浙于,OpenCV 將安裝在 /usr/local
目錄下,所有文件將被拷貝如下位置:
-
/usr/local/bin
- 可執(zhí)行文件 -
/usr/local/lib
- 庫文件 (.so) -
/usr/local/cmake/opencv4
- cmake 包 -
/usr/local/include/opencv4
- 頭文件 -
/usr/local/share/opencv4
- 其它文件(比如 XML 格式的訓(xùn)練的級聯(lián))
由于 /usr/local
為 root 用戶所有挟纱,因而安裝應(yīng)該以超級用戶特權(quán)執(zhí)行 (sudo
)羞酗,使用 "make install" 命令來安裝它:
$ sudo make install
安裝結(jié)束。所有的文件都被安裝在了 "/usr/local/" 目錄下紊服。打開一個(gè)終端并嘗試導(dǎo)入 "cv2":
import cv2 as cv
print(cv.__version__)
構(gòu)建配置和安裝過程的進(jìn)一步說明
這一部分將提供更多構(gòu)建過程的細(xì)節(jié)說明檀轨,并描述了其它可選的方法和工具。請參考 OpenCV 安裝概述 了解一般的安裝細(xì)節(jié)說明欺嗤,并參考 OpenCV 配置選項(xiàng)參考 了解配置選項(xiàng)文檔参萄。
編譯器和構(gòu)建工具
要編譯 OpenCV,我們就需要 C++ 編譯器煎饼。通常是 G++/GCC 或 Clang/LLVM讹挎。上面我們看到了 G++/GCC 的安裝方法,這里給出安裝 Clang/LLVM 的命令:
$ sudo apt install -y clang
無論何種編譯工具,構(gòu)建 OpenCV 都需要用 CMake 來執(zhí)行構(gòu)建配置筒溃。
CMake 可以為不同的構(gòu)建系統(tǒng)生成構(gòu)建腳本马篮,除了上面看到的 make,在 Ubuntu 上還可以用 ninja 來構(gòu)建铡羡。Ninja 的安裝命令如下:
$ sudo apt install -y ninja-build
配置和構(gòu)建
CMake 默認(rèn)為 make 構(gòu)建系統(tǒng)生成構(gòu)建腳本积蔚,如上面那樣。但我們可以通過 CMake 命令行參數(shù)強(qiáng)制為 ninja 構(gòu)建系統(tǒng)生成構(gòu)建腳本烦周,如:
$ cmake -GNinja ../opencv
再提一點(diǎn)關(guān)于安裝目標(biāo)目錄配置的問題尽爆。
安裝過程僅將文件復(fù)制到預(yù)定義的位置并進(jìn)行少量修補(bǔ)。使用這種方法读慎,無論是通過 make漱贱,還是通過 ninja,安裝都不會(huì)將 opencv 集成到系統(tǒng)包注冊表中夭委,如無法通過 apt 來管理這些安裝幅狮。因此,也無法自動(dòng)卸載 opencv株灸。一般來說崇摄,最好不要進(jìn)行系統(tǒng)范圍的安裝,因?yàn)榭赡芘c系統(tǒng)包沖突慌烧。
安裝根目錄可以通過 CMAKE_INSTALL_PREFIX
配置參數(shù)改變逐抑,比如 -DCMAKE_INSTALL_PREFIX=$HOME/.local
指明安裝到當(dāng)前用戶的本地目錄。安裝布局可以通過 OPENCV_*_INSTALL_PATH
參數(shù)修改屹蚊。詳情請參考 OpenCV 配置選項(xiàng)參考厕氨。
另外,OpenCV 有一些所謂的 "extra" 模塊汹粤,它們也提供了一些功能命斧,源碼位于單獨(dú)的 git 倉庫內(nèi)。新模塊常常不具有穩(wěn)定的 API嘱兼,且還沒經(jīng)過良好地測試国葬。由于 OpenCV 庫想要提供良好的二進(jìn)制兼容性,并嘗試提供良好的性能和穩(wěn)定性芹壕,這樣胃惜,新模塊不應(yīng)該作為正式的 OpenCV 分發(fā)版的一部分發(fā)型。
所有的新模塊單獨(dú)開發(fā)哪雕,并首先發(fā)布到 opencv_contrib
倉庫中。隨后模塊變得成熟并流行時(shí)鲫趁,再被移入 OpenCV 主倉庫中斯嚎,開發(fā)團(tuán)隊(duì)再為這個(gè)模塊提供產(chǎn)品級質(zhì)量的支持。在構(gòu)建時(shí),想要一起編譯 opencv_contrib
的話堡僻,首先需要下載 opencv_contrib
的源碼糠惫。方法同樣有兩個(gè),一是下載官方發(fā)布的源碼包:
$ wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.x.zip
$ unzip opencv_contrib.zip
二是克隆 Github 倉庫:
$ git clone https://github.com/opencv/opencv_contrib.git
假設(shè) opencv_contrib
的源碼被下載到了 opencv
源碼的同級目錄中钉疫。
在通過 CMake 執(zhí)行構(gòu)建配置時(shí)硼讽,通過 -DOPENCV_EXTRA_MODULES_PATH
參數(shù)選項(xiàng)指定 opencv_contrib
的模塊的路徑。
這樣牲阁,配置通過 ninja 構(gòu)建固阁,連同 opencv_contrib
一起構(gòu)建,并將 OpenCV 安裝在用戶本地目錄的命令如下:
$ cmake -GNinja -DCMAKE_INSTALL_PREFIX=$HOME/.local -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib-4.x/modules ../opencv
對于構(gòu)建城菊,在使用 make 構(gòu)建系統(tǒng)時(shí)备燃,可以給 make 傳參數(shù),以啟用多線程構(gòu)建凌唬,如:
$ make -j4
運(yùn)行 ninja 執(zhí)行構(gòu)建的命令如下:
$ ninja
運(yùn)行 ninja 執(zhí)行安裝的命令如下:
$ ninja install
[0/1] Install the project...
-- Install configuration: "Release"
-- Installing: /home/zhangsan/.local/share/licenses/opencv4/opencl-headers-LICENSE.txt
-- Installing: /home/zhangsan/.local/share/licenses/opencv4/ade-LICENSE
-- Installing: /home/zhangsan/.local/include/opencv4/opencv2/cvconfig.h
-- Installing: /home/zhangsan/.local/include/opencv4/opencv2/opencv_modules.hpp
-- Installing: /home/zhangsan/.local/lib/cmake/opencv4/OpenCVModules.cmake
-- Installing: /home/zhangsan/.local/lib/cmake/opencv4/OpenCVModules-release.cmake
-- Installing: /home/zhangsan/.local/lib/cmake/opencv4/OpenCVConfig-version.cmake
-- Installing: /home/zhangsan/.local/lib/cmake/opencv4/OpenCVConfig.cmake
-- Installing: /home/zhangsan/.local/bin/setup_vars_opencv4.sh
-- Installing: /home/zhangsan/.local/share/opencv4/valgrind.supp
-- Installing: /home/zhangsan/.local/share/opencv4/valgrind_3rdparty.supp
-- Installing: /home/zhangsan/.local/share/licenses/opencv4/libopenjp2-README.md
-- Installing: /home/zhangsan/.local/share/licenses/opencv4/libopenjp2-LICENSE
-- Installing: /home/zhangsan/.local/share/licenses/opencv4/protobuf-LICENSE
-- Installing: /home/zhangsan/.local/share/licenses/opencv4/protobuf-README.md
-- Installing: /home/zhangsan/.local/share/licenses/opencv4/quirc-LICENSE
-- Installing: /home/zhangsan/.local/share/licenses/opencv4/ittnotify-LICENSE.BSD
-- Installing: /home/zhangsan/.local/share/licenses/opencv4/ittnotify-LICENSE.GPL
-- Installing: /home/zhangsan/.local/include/opencv4/opencv2/opencv.hpp
. . . . . .
-- Installing: /home/zhangsan/.local/lib/libopencv_optflow.so.4.5.5
-- Installing: /home/zhangsan/.local/lib/libopencv_optflow.so.405
-- Set runtime path of "/home/zhangsan/.local/lib/libopencv_optflow.so.4.5.5" to "/home/zhangsan/.local/lib"
-- Installing: /home/zhangsan/.local/lib/libopencv_optflow.so
-- Installing: /home/zhangsan/.local/include/opencv4/opencv2/optflow.hpp
-- Installing: /home/zhangsan/.local/include/opencv4/opencv2/optflow/motempl.hpp
-- Installing: /home/zhangsan/.local/include/opencv4/opencv2/optflow/pcaflow.hpp
-- Installing: /home/zhangsan/.local/include/opencv4/opencv2/optflow/rlofflow.hpp
-- Installing: /home/zhangsan/.local/include/opencv4/opencv2/optflow/sparse_matching_gpc.hpp
-- Installing: /home/zhangsan/.local/lib/libopencv_stitching.so.4.5.5
-- Installing: /home/zhangsan/.local/lib/libopencv_stitching.so.405
. . . . . .
-- Installing: /home/zhangsan/.local/include/opencv4/opencv2/stereo/stereo.hpp
-- Installing: /home/zhangsan/.local/lib/python2.7/dist-packages/cv2/__init__.py
-- Installing: /home/zhangsan/.local/lib/python2.7/dist-packages/cv2/load_config_py2.py
-- Installing: /home/zhangsan/.local/lib/python2.7/dist-packages/cv2/load_config_py3.py
-- Installing: /home/zhangsan/.local/lib/python2.7/dist-packages/cv2/config.py
-- Installing: /home/zhangsan/.local/lib/python2.7/dist-packages/cv2/misc/__init__.py
-- Installing: /home/zhangsan/.local/lib/python2.7/dist-packages/cv2/misc/version.py
-- Installing: /home/zhangsan/.local/lib/python2.7/dist-packages/cv2/mat_wrapper/__init__.py
-- Installing: /home/zhangsan/.local/lib/python2.7/dist-packages/cv2/utils/__init__.py
-- Installing: /home/zhangsan/.local/lib/python2.7/dist-packages/cv2/gapi/__init__.py
-- Installing: /home/zhangsan/.local/lib/python2.7/dist-packages/cv2/python-2.7/cv2.so
-- Set runtime path of "/home/zhangsan/.local/lib/python2.7/dist-packages/cv2/python-2.7/cv2.so" to "/home/zhangsan/.local/lib"
-- Installing: /home/zhangsan/.local/lib/python2.7/dist-packages/cv2/config-2.7.py
-- Installing: /home/zhangsan/.local/lib/python3.8/site-packages/cv2/__init__.py
-- Installing: /home/zhangsan/.local/lib/python3.8/site-packages/cv2/load_config_py2.py
-- Installing: /home/zhangsan/.local/lib/python3.8/site-packages/cv2/load_config_py3.py
-- Installing: /home/zhangsan/.local/lib/python3.8/site-packages/cv2/config.py
-- Installing: /home/zhangsan/.local/lib/python3.8/site-packages/cv2/misc/__init__.py
-- Installing: /home/zhangsan/.local/lib/python3.8/site-packages/cv2/misc/version.py
-- Installing: /home/zhangsan/.local/lib/python3.8/site-packages/cv2/mat_wrapper/__init__.py
-- Installing: /home/zhangsan/.local/lib/python3.8/site-packages/cv2/utils/__init__.py
-- Installing: /home/zhangsan/.local/lib/python3.8/site-packages/cv2/gapi/__init__.py
-- Installing: /home/zhangsan/.local/lib/python3.8/site-packages/cv2/python-3.8/cv2.cpython-38-x86_64-linux-gnu.so
-- Set runtime path of "/home/zhangsan/.local/lib/python3.8/site-packages/cv2/python-3.8/cv2.cpython-38-x86_64-linux-gnu.so" to "/home/zhangsan/.local/lib"
-- Installing: /home/zhangsan/.local/lib/python3.8/site-packages/cv2/config-3.8.py
. . . . . .
-- Installing: /home/zhangsan/.local/share/opencv4/haarcascades/haarcascade_smile.xml
-- Installing: /home/zhangsan/.local/share/opencv4/haarcascades/haarcascade_upperbody.xml
-- Installing: /home/zhangsan/.local/share/opencv4/lbpcascades/lbpcascade_frontalcatface.xml
-- Installing: /home/zhangsan/.local/share/opencv4/lbpcascades/lbpcascade_frontalface.xml
-- Installing: /home/zhangsan/.local/share/opencv4/lbpcascades/lbpcascade_frontalface_improved.xml
-- Installing: /home/zhangsan/.local/share/opencv4/lbpcascades/lbpcascade_profileface.xml
-- Installing: /home/zhangsan/.local/share/opencv4/lbpcascades/lbpcascade_silverware.xml
-- Installing: /home/zhangsan/.local/bin/opencv_annotation
-- Set runtime path of "/home/zhangsan/.local/bin/opencv_annotation" to "/home/zhangsan/.local/lib"
-- Installing: /home/zhangsan/.local/bin/opencv_visualisation
-- Set runtime path of "/home/zhangsan/.local/bin/opencv_visualisation" to "/home/zhangsan/.local/lib"
-- Installing: /home/zhangsan/.local/bin/opencv_interactive-calibration
-- Set runtime path of "/home/zhangsan/.local/bin/opencv_interactive-calibration" to "/home/zhangsan/.local/lib"
-- Installing: /home/zhangsan/.local/bin/opencv_version
-- Set runtime path of "/home/zhangsan/.local/bin/opencv_version" to "/home/zhangsan/.local/lib"
-- Installing: /home/zhangsan/.local/bin/opencv_model_diagnostics
-- Set runtime path of "/home/zhangsan/.local/bin/opencv_model_diagnostics" to "/home/zhangsan/.local/lib"
由于指定的安裝路徑為用戶本地路徑并齐,因而不需要 sudo
。在安裝過程的輸出中客税,還是能發(fā)現(xiàn)不少信息况褪,如 Python 綁定的安裝路徑,頭文件的具體安裝路徑等更耻。
在 Ubuntu 系統(tǒng)上搭建 OpenCV 的 C++ 開發(fā)環(huán)境
這里說明配置 CMake 和 Eclipse 構(gòu)建基于 OpenCV 的 C++ 項(xiàng)目的方法测垛。
準(zhǔn)備依賴
構(gòu)建依賴可以分為兩部分:
(1). 開發(fā)和構(gòu)建工具,包括 Eclipse酥夭,CMake赐纱,GCC/G++,Clang/LLVM熬北,Git 等疙描。如果還沒有安裝 Eclipse(含 CDT),可以到 Eclipse 的網(wǎng)站上讶隐,下載 Eclipse IDE for C/C++ Developers起胰。根據(jù)自己的系統(tǒng)平臺(tái)類型選擇下載的版本。其它工具可以參考上面的說明進(jìn)行安裝巫延,這里不再贅述效五。
(2). 用于 C/C++ 開發(fā)的 OpenCV 庫開發(fā)包,主要包括動(dòng)態(tài)鏈接庫炉峰,靜態(tài)鏈接庫和頭文件畏妖。
對于 OpenCV 庫開發(fā)包安裝,有兩種方法:
從源碼構(gòu)建安裝疼阔。如上面從源碼構(gòu)建安裝 OpenCV-Python 的過程戒劫,該過程除了可以構(gòu)建安裝 OpenCV 的 Python 綁定之外半夷,也同樣會(huì)構(gòu)建安裝C/C++ 開發(fā)所需要的所有庫文件、頭文件等內(nèi)容迅细。對于這種方法巫橄,這里不再贅述。
從 Apt 倉庫安裝預(yù)編譯的二進(jìn)制安裝包茵典。同樣湘换,用這種方式安裝的 OpenCV 庫開發(fā)包版本可能比較老。但這種方法更簡單统阿,對于只想利用 OpenCV 的 API 開發(fā)應(yīng)用彩倚,而無心研究 OpenCV 的 源碼或向 OpenCV 項(xiàng)目貢獻(xiàn)源碼的同學(xué),這種方法比較方便砂吞。
對于從從 Apt 倉庫安裝預(yù)編譯的二進(jìn)制安裝包署恍,搜索 Apt 倉庫中的 OpenCV 開發(fā)包,可以看到如下內(nèi)容:
build$ sudo apt-cache search libopencv
libopencv-apps-dev - Opencv_apps Robot OS package - development files
libopencv-apps1d - opencv_apps Robot OS package - runtime files
libopencv-calib3d-dev - development files for libopencv-calib3d4.2
libopencv-calib3d4.2 - computer vision Camera Calibration library
libopencv-contrib-dev - development files for libopencv-contrib4.2
libopencv-contrib4.2 - computer vision contrlib library
libopencv-core-dev - development files for libopencv-core4.2
libopencv-core4.2 - computer vision core library
libopencv-dev - development files for opencv
libopencv-dnn-dev - development files for libopencv-dnn4.2
libopencv-dnn4.2 - computer vision Deep neural network module
libopencv-features2d-dev - development files for libopencv-features2d4.2
libopencv-features2d4.2 - computer vision Feature Detection and Descriptor Extraction library
libopencv-flann-dev - development files for libopencv-flann4.2
libopencv-flann4.2 - computer vision Clustering and Search in Multi-Dimensional spaces library
libopencv-highgui-dev - development files for libopencv-highgui4.2
libopencv-highgui4.2 - computer vision High-level GUI and Media I/O library
libopencv-imgcodecs-dev - development files for libopencv-imgcodecs4.2
libopencv-imgcodecs4.2 - computer vision Image Codecs library
libopencv-imgproc-dev - development files for libopencv-imgproc4.2
libopencv-imgproc4.2 - computer vision Image Processing library
libopencv-ml-dev - development files for libopencv-ml4.2
libopencv-ml4.2 - computer vision Machine Learning library
libopencv-objdetect-dev - development files for libopencv-objdetect4.2
libopencv-objdetect4.2 - computer vision Object Detection library
libopencv-photo-dev - development files for libopencv-photo4.2
libopencv-photo4.2 - computer vision computational photography library
libopencv-shape-dev - development files for libopencv-shape4.2
libopencv-shape4.2 - computer vision shape descriptors and matchers library
libopencv-stitching-dev - development files for libopencv-stitching4.2
libopencv-stitching4.2 - computer vision image stitching library
libopencv-superres-dev - development files for libopencv-superres4.2
libopencv-superres4.2 - computer vision Super Resolution library
libopencv-ts-dev - development files for TS library of OpenCV (Open Computer Vision)
libopencv-video-dev - development files for libopencv-video4.2
libopencv-video4.2 - computer vision Video analysis library
libopencv-videoio-dev - development files for libopencv-videoio4.2
libopencv-videoio4.2 - computer vision Video I/O library
libopencv-videostab-dev - development files for libopencv-videostab4.2
libopencv-videostab4.2 - computer vision video stabilization library
libopencv-viz-dev - development files for libopencv-viz4.2
libopencv-viz4.2 - computer vision 3D data visualization library
libopencv4.2-java - Java bindings for the computer vision library
libopencv4.2-jni - Java jni library for the computer vision library
上面找到的這些包中蜻直,包名含有 "libopencv" 字樣盯质,后綴為 "-dev" 的包即為我們需要安裝的 OpenCV 庫開發(fā)包「哦可以按需安裝這些包呼巷,也可以一次性安裝所有這些包。在終端中通過如下命令安裝所有 OpenCV 庫開發(fā)包(以 root 用戶):
$ sudo apt install libopencv-apps-dev libopencv-calib3d-dev libopencv-contrib-dev libopencv-core-dev libopencv-dev libopencv-dnn-dev libopencv-features2d-dev libopencv-flann-dev libopencv-highgui-dev libopencv-imgcodecs-dev libopencv-imgproc-dev libopencv-ml-dev libopencv-objdetect-dev libopencv-photo-dev libopencv-shape-dev libopencv-stitching-dev libopencv-superres-dev libopencv-ts-dev libopencv-video-dev libopencv-videoio-dev libopencv-videostab-dev libopencv-viz-dev
這里隨便找一個(gè)包赎瑰,看下它安裝的文件及各個(gè)文件的安裝路徑:
$ dpkg -L libopencv-core-dev
/.
/usr
/usr/include
/usr/include/opencv4
/usr/include/opencv4/opencv2
/usr/include/opencv4/opencv2/core
/usr/include/opencv4/opencv2/core/affine.hpp
/usr/include/opencv4/opencv2/core/async.hpp
/usr/include/opencv4/opencv2/core/base.hpp
/usr/include/opencv4/opencv2/core/bindings_utils.hpp
/usr/include/opencv4/opencv2/core/bufferpool.hpp
/usr/include/opencv4/opencv2/core/check.hpp
/usr/include/opencv4/opencv2/core/core.hpp
/usr/include/opencv4/opencv2/core/core_c.h
/usr/include/opencv4/opencv2/core/cuda
/usr/include/opencv4/opencv2/core/cuda/block.hpp
/usr/include/opencv4/opencv2/core/cuda/border_interpolate.hpp
/usr/include/opencv4/opencv2/core/cuda/color.hpp
/usr/include/opencv4/opencv2/core/cuda/common.hpp
/usr/include/opencv4/opencv2/core/cuda/datamov_utils.hpp
/usr/include/opencv4/opencv2/core/cuda/detail
/usr/include/opencv4/opencv2/core/cuda/detail/color_detail.hpp
/usr/include/opencv4/opencv2/core/cuda/detail/reduce.hpp
/usr/include/opencv4/opencv2/core/cuda/detail/reduce_key_val.hpp
/usr/include/opencv4/opencv2/core/cuda/detail/transform_detail.hpp
/usr/include/opencv4/opencv2/core/cuda/detail/type_traits_detail.hpp
/usr/include/opencv4/opencv2/core/cuda/detail/vec_distance_detail.hpp
/usr/include/opencv4/opencv2/core/cuda/dynamic_smem.hpp
/usr/include/opencv4/opencv2/core/cuda/emulation.hpp
/usr/include/opencv4/opencv2/core/cuda/filters.hpp
/usr/include/opencv4/opencv2/core/cuda/funcattrib.hpp
/usr/include/opencv4/opencv2/core/cuda/functional.hpp
/usr/include/opencv4/opencv2/core/cuda/limits.hpp
/usr/include/opencv4/opencv2/core/cuda/reduce.hpp
/usr/include/opencv4/opencv2/core/cuda/saturate_cast.hpp
/usr/include/opencv4/opencv2/core/cuda/scan.hpp
/usr/include/opencv4/opencv2/core/cuda/simd_functions.hpp
/usr/include/opencv4/opencv2/core/cuda/transform.hpp
/usr/include/opencv4/opencv2/core/cuda/type_traits.hpp
/usr/include/opencv4/opencv2/core/cuda/utility.hpp
/usr/include/opencv4/opencv2/core/cuda/vec_distance.hpp
/usr/include/opencv4/opencv2/core/cuda/vec_math.hpp
/usr/include/opencv4/opencv2/core/cuda/vec_traits.hpp
/usr/include/opencv4/opencv2/core/cuda/warp.hpp
/usr/include/opencv4/opencv2/core/cuda/warp_reduce.hpp
/usr/include/opencv4/opencv2/core/cuda/warp_shuffle.hpp
/usr/include/opencv4/opencv2/core/cuda.hpp
/usr/include/opencv4/opencv2/core/cuda.inl.hpp
/usr/include/opencv4/opencv2/core/cuda_stream_accessor.hpp
/usr/include/opencv4/opencv2/core/cuda_types.hpp
/usr/include/opencv4/opencv2/core/cv_cpu_dispatch.h
/usr/include/opencv4/opencv2/core/cv_cpu_helper.h
/usr/include/opencv4/opencv2/core/cvdef.h
/usr/include/opencv4/opencv2/core/cvstd.hpp
/usr/include/opencv4/opencv2/core/cvstd.inl.hpp
/usr/include/opencv4/opencv2/core/cvstd_wrapper.hpp
/usr/include/opencv4/opencv2/core/detail
/usr/include/opencv4/opencv2/core/detail/async_promise.hpp
/usr/include/opencv4/opencv2/core/detail/exception_ptr.hpp
/usr/include/opencv4/opencv2/core/directx.hpp
/usr/include/opencv4/opencv2/core/eigen.hpp
/usr/include/opencv4/opencv2/core/fast_math.hpp
/usr/include/opencv4/opencv2/core/hal
/usr/include/opencv4/opencv2/core/hal/hal.hpp
/usr/include/opencv4/opencv2/core/hal/interface.h
/usr/include/opencv4/opencv2/core/hal/intrin.hpp
/usr/include/opencv4/opencv2/core/hal/intrin_avx.hpp
/usr/include/opencv4/opencv2/core/hal/intrin_avx512.hpp
/usr/include/opencv4/opencv2/core/hal/intrin_cpp.hpp
/usr/include/opencv4/opencv2/core/hal/intrin_forward.hpp
/usr/include/opencv4/opencv2/core/hal/intrin_msa.hpp
/usr/include/opencv4/opencv2/core/hal/intrin_neon.hpp
/usr/include/opencv4/opencv2/core/hal/intrin_sse.hpp
/usr/include/opencv4/opencv2/core/hal/intrin_sse_em.hpp
/usr/include/opencv4/opencv2/core/hal/intrin_vsx.hpp
/usr/include/opencv4/opencv2/core/hal/intrin_wasm.hpp
/usr/include/opencv4/opencv2/core/hal/msa_macros.h
/usr/include/opencv4/opencv2/core/hal/simd_utils.impl.hpp
/usr/include/opencv4/opencv2/core/mat.hpp
/usr/include/opencv4/opencv2/core/mat.inl.hpp
/usr/include/opencv4/opencv2/core/matx.hpp
/usr/include/opencv4/opencv2/core/neon_utils.hpp
/usr/include/opencv4/opencv2/core/ocl.hpp
/usr/include/opencv4/opencv2/core/ocl_genbase.hpp
/usr/include/opencv4/opencv2/core/opencl
/usr/include/opencv4/opencv2/core/opencl/ocl_defs.hpp
/usr/include/opencv4/opencv2/core/opencl/opencl_info.hpp
/usr/include/opencv4/opencv2/core/opencl/opencl_svm.hpp
/usr/include/opencv4/opencv2/core/opencl/runtime
/usr/include/opencv4/opencv2/core/opencl/runtime/autogenerated
/usr/include/opencv4/opencv2/core/opencl/runtime/autogenerated/opencl_clamdblas.hpp
/usr/include/opencv4/opencv2/core/opencl/runtime/autogenerated/opencl_clamdfft.hpp
/usr/include/opencv4/opencv2/core/opencl/runtime/autogenerated/opencl_core.hpp
/usr/include/opencv4/opencv2/core/opencl/runtime/autogenerated/opencl_core_wrappers.hpp
/usr/include/opencv4/opencv2/core/opencl/runtime/autogenerated/opencl_gl.hpp
/usr/include/opencv4/opencv2/core/opencl/runtime/autogenerated/opencl_gl_wrappers.hpp
/usr/include/opencv4/opencv2/core/opencl/runtime/opencl_clamdblas.hpp
/usr/include/opencv4/opencv2/core/opencl/runtime/opencl_clamdfft.hpp
/usr/include/opencv4/opencv2/core/opencl/runtime/opencl_core.hpp
/usr/include/opencv4/opencv2/core/opencl/runtime/opencl_core_wrappers.hpp
/usr/include/opencv4/opencv2/core/opencl/runtime/opencl_gl.hpp
/usr/include/opencv4/opencv2/core/opencl/runtime/opencl_gl_wrappers.hpp
/usr/include/opencv4/opencv2/core/opencl/runtime/opencl_svm_20.hpp
/usr/include/opencv4/opencv2/core/opencl/runtime/opencl_svm_definitions.hpp
/usr/include/opencv4/opencv2/core/opencl/runtime/opencl_svm_hsa_extension.hpp
/usr/include/opencv4/opencv2/core/opengl.hpp
/usr/include/opencv4/opencv2/core/operations.hpp
/usr/include/opencv4/opencv2/core/optim.hpp
/usr/include/opencv4/opencv2/core/ovx.hpp
/usr/include/opencv4/opencv2/core/persistence.hpp
/usr/include/opencv4/opencv2/core/saturate.hpp
/usr/include/opencv4/opencv2/core/simd_intrinsics.hpp
/usr/include/opencv4/opencv2/core/softfloat.hpp
/usr/include/opencv4/opencv2/core/sse_utils.hpp
/usr/include/opencv4/opencv2/core/traits.hpp
/usr/include/opencv4/opencv2/core/types.hpp
/usr/include/opencv4/opencv2/core/types_c.h
/usr/include/opencv4/opencv2/core/utility.hpp
/usr/include/opencv4/opencv2/core/utils
/usr/include/opencv4/opencv2/core/utils/allocator_stats.hpp
/usr/include/opencv4/opencv2/core/utils/allocator_stats.impl.hpp
/usr/include/opencv4/opencv2/core/utils/filesystem.hpp
/usr/include/opencv4/opencv2/core/utils/instrumentation.hpp
/usr/include/opencv4/opencv2/core/utils/logger.defines.hpp
/usr/include/opencv4/opencv2/core/utils/logger.hpp
/usr/include/opencv4/opencv2/core/utils/logtag.hpp
/usr/include/opencv4/opencv2/core/utils/tls.hpp
/usr/include/opencv4/opencv2/core/utils/trace.hpp
/usr/include/opencv4/opencv2/core/va_intel.hpp
/usr/include/opencv4/opencv2/core/version.hpp
/usr/include/opencv4/opencv2/core/vsx_utils.hpp
/usr/include/opencv4/opencv2/core.hpp
/usr/include/opencv4/opencv2/cvconfig.h
/usr/include/opencv4/opencv2/opencv.hpp
/usr/include/opencv4/opencv2/opencv_modules.hpp
/usr/lib
/usr/lib/x86_64-linux-gnu
/usr/lib/x86_64-linux-gnu/libopencv_core.a
/usr/share
/usr/share/doc
/usr/share/doc/libopencv-core-dev
/usr/share/doc/libopencv-core-dev/README.Debian
/usr/share/doc/libopencv-core-dev/copyright
/usr/lib/x86_64-linux-gnu/libopencv_core.so
/usr/share/doc/libopencv-core-dev/changelog.Debian.gz
注意王悍,頭文件是安裝在 /usr/include/opencv4/
目錄下的,但這個(gè)目錄通常不是編譯工具默認(rèn)的頭文件搜索目錄餐曼。包含頭文件時(shí)压储,要么寫成 <opencv4/opencv2/opencv.hpp>
這樣,要么寫成 <opencv2/opencv.hpp>
并定制頭文件的搜索路徑源譬。
在 Eclipse 中構(gòu)建使用 OpenCV(CDT 插件)的應(yīng)用
首先要?jiǎng)?chuàng)建一個(gè)工程集惋。具體過程如下:
啟動(dòng) Eclipse。在 Eclipse 的安裝目錄中運(yùn)行可執(zhí)行文件踩娘。
轉(zhuǎn)到 File -> New -> C/C++ Project
- 為這個(gè)工程選擇一個(gè)名字刮刑,比如 DisplayImage。對于這個(gè)例子來說一個(gè) Empty Project 應(yīng)該就可以了养渴。
其它配置保持默認(rèn)值雷绢。點(diǎn)擊 Finish。
新建的工程(在這種情況下是 DisplayImage)應(yīng)該出現(xiàn)在 Project Navigator(通常出現(xiàn)在窗口的左側(cè))理卑。
- 現(xiàn)在來給工程添加一個(gè)使用 OpenCV 的源文件:
- 鼠標(biāo)右鍵點(diǎn)擊 DisplayImage (在 Navigator 中)翘紊。New -> Folder。
將目錄命名為 src藐唠,然后點(diǎn)擊 Finish霞溪。
右鍵點(diǎn)擊新創(chuàng)建的 目錄孵滞。選擇 New source file。
將其命名為 DisplayImage.cpp鸯匹。點(diǎn)擊 Finish。
- 現(xiàn)在我們有了一個(gè)包含一個(gè)空 .cpp 文件的工程泄伪。讓我們在這個(gè)源文件中添加我們的示例代碼(換句話說殴蓬,把下面的代碼片段拷貝過去):
#include <iostream>
using namespace std;
#include <opencv2/opencv.hpp>
int main(int argc, char **argv) {
cv::Mat image;
image = cv::imread(argv[1], 1);
if (argc != 2 || !image.data) {
printf("No image data \n");
return -1;
}
cv::namedWindow("Display Image", cv::WINDOW_AUTOSIZE);
imshow("Display Image", image);
cv::waitKey(0);
return 0;
}
- 我們只差最后一步了:告訴 Eclipse OpenCV 的頭文件和庫文件在哪兒。要做到這一點(diǎn)蟋滴,執(zhí)行如下操作:
轉(zhuǎn)到 Project–>Properties
在 C/C++ Build 中染厅,點(diǎn)中 Settings。在右邊津函,選擇 Tool Settings 標(biāo)簽肖粮。這里我們進(jìn)入頭文件和庫信息設(shè)置:
a. 在 GCC C++ Compiler 中,切換到 Includes尔苦。在 Include paths(-l) 中涩馆,應(yīng)該包含安裝 opencv 的頭文件的目錄的路徑。對于默認(rèn)的安裝允坚,一般來說是 /usr/include/opencv4魂那,對于上面將 OpenCV 庫安裝到用戶本地目錄的情況荠诬,則是 /home/zhangsan/.local/include/opencv4鲜侥。
b. 現(xiàn)在切換到 GCC C++ Linker,在這里需要填兩個(gè)地方:
首先是必須在 Library search path (-L) 寫入 opencv 庫所在的目錄的路徑瘸爽,默認(rèn)情況下這個(gè)路徑為:
/usr/local/lib
這個(gè)路徑一般都是鏈接器默認(rèn)的庫文件搜索路徑展运,因而一般不用填寫活逆。
在 Libraries(-l) 中添加需要的 OpenCV 庫。通常只需要添加下面列表中的前 4 個(gè)就夠了(對于簡單的應(yīng)用程序來說)拗胜。這里我們把所有計(jì)劃要使用的庫都加上蔗候。
opencv_core opencv_imgproc opencv_imgcodecs opencv_highgui opencv_ml opencv_videoio opencv_video opencv_features2d opencv_calib3d opencv_objdetect opencv_flann
現(xiàn)在點(diǎn)擊 OK。
- 這個(gè)工程應(yīng)該已經(jīng)準(zhǔn)備好構(gòu)建了挤土。轉(zhuǎn)到 Project->Build Project
在終端中應(yīng)該能看到類似這樣的內(nèi)容:
檢查你的目錄的話琴庵,應(yīng)該能夠看到可執(zhí)行文件。
運(yùn)行應(yīng)用
現(xiàn)在仰美,我們已經(jīng)有可執(zhí)行文件可以運(yùn)行了迷殿。如果是把 OpenCV 安裝在默認(rèn)的 /usr/local
目錄的話,則一般情況下 OpenCV 庫文件的安裝目錄已經(jīng)在動(dòng)態(tài)鏈接器的庫文件搜索路徑了咖杂。使用終端庆寺,我們可以像這樣做運(yùn)行它:
$ cd ~/workspace/DisplayImage
$ ./Debug/DisplayImage ~/HappyLittleFish.jpg
如果我們將 OpenCV 安裝在了用戶本地目錄的話,像上面那樣執(zhí)行可執(zhí)行文件會(huì)報(bào)錯(cuò)诉字,找不到動(dòng)態(tài)鏈接庫懦尝,如:
$ ./Debug/DisplayImage ~/HappyLittleFish.jpg
Debug/DisplayImage: error while loading shared libraries: libopencv_core.so.405: cannot open shared object file: No such file or directory
此時(shí)我們可以將 OpenCV 庫文件的安裝路徑添加進(jìn) /etc/ld.so.conf
并執(zhí)行 sudo ldconfig
以更新動(dòng)態(tài)鏈接器的庫文件搜索路徑知纷;也可以設(shè)置 LD_LIBRARY_PATH
指向 OpenCV 庫文件的安裝路徑,然后再執(zhí)行可執(zhí)行文件陵霉,如:
$ export LD_LIBRARY_PATH=~/.local/lib
$ ./Debug/DisplayImage ~/HappyLittleFish.jpg
假設(shè)要使用作為路徑參數(shù)的圖片位于 ~/HappyLittleFish.jpg
琅轧。我們也可以在 Eclipse 中執(zhí)行可執(zhí)行文件:
轉(zhuǎn)到 Run->Run Configurations
在 C/C++ Application 下,將看到可執(zhí)行文件的名字(如果沒有踊挠,則鼠標(biāo)左鍵雙擊 C/C++ Application)乍桂。選擇該名字(在這個(gè)例子中是 DisplayImage)。
現(xiàn)在在窗口的右側(cè)效床,選擇 Arguments 標(biāo)簽睹酌。把我們想打開的圖片的路徑(可以寫絕對路徑,也可以寫相對于 workspace/DisplayImage 目錄的相對路徑)寫進(jìn)去剩檀。讓我們使用 ~/HappyLittleFish.jpg:
- 如果我們沒有把 OpenCV 安裝在默認(rèn)位置憋沿,同樣需要設(shè)置環(huán)境變量
LD_LIBRARY_PATH
指向 OpenCV 庫文件的安裝路徑。同樣是在上面的窗口中沪猴,選中 Environment 標(biāo)簽辐啄。添加環(huán)境變量LD_LIBRARY_PATH
,如:
- 點(diǎn)擊 Apply 按鈕字币,然后運(yùn)行则披。這將彈出一個(gè) OpenCV 窗口并顯示一張魚圖(或其它你想顯示的):
- 現(xiàn)在已經(jīng)完全做好了使用 Eclipse 開發(fā) OpenCV 應(yīng)用的準(zhǔn)備了。
V2:在 Eclipse (包含 CDT 插件)中使用 CMake+OpenCV
比如已經(jīng)在一個(gè)稱為 helloopencv 的目錄下創(chuàng)建了一個(gè)名為 helloworld.cpp 的新文件洗出,其內(nèi)容如下:
#include <opencv2/opencv.hpp>
using namespace cv;
int main ( int argc, char **argv )
{
Mat img(480, 640, CV_8U);
putText(img, "Hello World!", Point( 200, 400 ), FONT_HERSHEY_SIMPLEX | FONT_ITALIC, 1.0, Scalar( 255, 255, 0 ));
imshow("My Window", img);
waitKey();
return 0;
}
- 在 helloopencv 目錄下放一個(gè)
CMakeLists.txt
文件士复,其內(nèi)容如下:
cmake_minimum_required(VERSION 3.6)
PROJECT( helloworld_proj )
FIND_PACKAGE( OpenCV REQUIRED )
ADD_EXECUTABLE( helloworld helloworld.cpp )
TARGET_LINK_LIBRARIES(helloworld
${OpenCV_LIBS})
- 在 helloopencv 目錄下創(chuàng)建 build 目錄,并切換進(jìn)去翩活,執(zhí)行
cmake
命令生成構(gòu)建腳本:
$ cd helloopencv
$ mkdir build
$ cd build
$ cmake -DOpenCV_DIR=/home/zhangsan/.local/lib/cmake/opencv4 ..
-- Configuring done
-- Generating done
-- Build files have been written to: /home/zhangsan/workspace/helloopencv/build
這里定義 OpenCV_DIR 指向 opencv 庫的 cmake 文件安裝路徑阱洪。
運(yùn)行
make -j4
(-j4 是可選的,它只是告訴編譯器用 4 個(gè)線程來編譯)菠镇,或者cmake --build .
冗荸。確保它構(gòu)建完成。啟動(dòng) Eclipse利耍。把 workspace 放在某些目錄下蚌本,但 不能 在
helloopencv
或helloopencv\build
下。轉(zhuǎn)到 File -> Import...隘梨。然后打開 C/C++ filter程癌。選中 Existing Code as a Makefile Project。
命名這個(gè)工程轴猎,比如 helloworld嵌莉。瀏覽到代碼目錄位置 helloopencv 目錄。在 "Toolchain for Indexer Settings" 中選擇 Linux GCC 并按下 Finish捻脖。
在 Project Explorer 部分選中工程锐峭,右鍵單擊中鼠。選擇 Properties。在 C/C++ Build 下沿癞,在 Builder Settings 標(biāo)簽中援雇,設(shè)置 Build directory::從類似于
${workspace_loc:/helloopencv}
改為${workspace_loc:/helloopencv}/build
,即執(zhí)行 cmake 并生成 Makefile 文件的目錄椎扬。
也可以選擇修改構(gòu)建命令 Build command:熊杨,反選 Use default build command,然后在 Build command: 中填入自己需要的構(gòu)建命令盗舰。在 Behavior 標(biāo)簽中,選擇 Use custom build arguments桂躏,還可以輸入想要的構(gòu)建參數(shù)钻趋,比如加上VERBOSE=1 -j4
參數(shù),告訴編譯器生成詳細(xì)的符號文件用于調(diào)試剂习,并且以 4 個(gè)并發(fā)線程來編譯蛮位。轉(zhuǎn)到 Project->Build Project 構(gòu)建工程。
在命令行及 Eclipse 中運(yùn)行編譯出來的二進(jìn)制可執(zhí)行的方法如上文所述鳞绕。
通過 GCC 和 CMake 使用 OpenCV
在自己的代碼中使用 OpenCV 最簡單的方式就是使用 CMake 了失仁。這有這樣一些優(yōu)勢(摘自 Wiki):
- 當(dāng)在 Linux 和 Windows 之間移植時(shí),不需要修改任何東西们何。
- 可以通過 CMake 簡單地與其它工具結(jié)合使用(比如 Qt萄焦,ITK 和 VTK)
如果你還不熟悉 CMake,可以參考它的網(wǎng)站上的 教程冤竹。
步驟
創(chuàng)建一個(gè)使用 OpenCV 的程序
隨便使用任何一個(gè)你所熟悉的喜歡的編輯器拂封,或者集成開發(fā)環(huán)境,創(chuàng)建一個(gè)類似于名為 DisplayImage.cpp 的源文件鹦蠕,并輸入如下這樣簡單程序的源碼:
#include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char **argv) {
if (argc != 2) {
printf("usage: DisplayImage.out <Image_Path>\n");
return -1;
}
Mat image;
image = imread(argv[1], 1);
if (!image.data) {
printf("No image data \n");
return -1;
}
namedWindow("Display Image", WINDOW_AUTOSIZE);
imshow("Display Image", image);
waitKey(0);
return 0;
}
把這個(gè)源文件放在某個(gè)目錄下冒签,如 DisplayImage/src
。
創(chuàng)建一個(gè) CMake 文件
然后必須創(chuàng)建你的 CMakeLists.txt钟病,比如在 DisplayImage
目錄下萧恕。它看起來像這樣:
cmake_minimum_required(VERSION 3.8)
project(DisplayImage)
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(DisplayImage
src/DisplayImage.cpp)
target_link_libraries(DisplayImage ${OpenCV_LIBS})
生成可執(zhí)行文件
這個(gè)部分很簡單,就像在任何其它使用 CMake 的項(xiàng)目里那樣處理:
$ cd DisplayImage
$ cmake -Bbuild -DOpenCV_DIR=/home/zhangsan/.local/lib/cmake/opencv4 .
$ cmake --build build
CMake 的 -B
選項(xiàng)用于指定生成構(gòu)建腳本的目錄肠阱。
結(jié)果
現(xiàn)在應(yīng)該有了可執(zhí)行文件(在這個(gè)例子中稱為 DisplayImage)票唆。只需要給它傳入一個(gè)圖片文件的路徑作為參數(shù)來運(yùn)行它,比如:
$ build/DisplayImage ..opencv/samples/data/lena.jpg
這應(yīng)該能得到一個(gè)漂亮的窗口辖所,就像下面展示的這個(gè):
參考文檔
OpenCV Releases
OpenCV 4.5.5 文檔
OpenCV-Python Tutorials
OpenCV-Python Tutorials - Introduction to OpenCV
OpenCV-Python Tutorials - Introduction to OpenCV-Python Tutorials
OpenCV-Python Tutorials - Install OpenCV-Python in Ubuntu
OpenCV Tutorials
OpenCV Tutorials - Introduction to OpenCV
OpenCV Tutorials - Installation in Linux
OpenCV Tutorials - Using OpenCV with Eclipse (plugin CDT)
OpenCV Tutorials - Using OpenCV with gcc and CMake
Done.