近期為了做三維重建。開(kāi)始了OpenCV學(xué)習(xí)之路。順便記錄一下。
如題所述,本次是在Ubuntu下配置的。所以用到的各種軟件如下所示:
將cmake下載并解壓到任意文件夾下,然后建立軟連接。我是放在~/opt
下
sudo ln -sf /opt/cmake-3.19.2/bin/* /usr/bin/
這里不再贅述。下載安裝即可。
將4.5對(duì)應(yīng)的倆個(gè)壓縮包下載下來(lái)。分別更改為opencv
、opencv_contrib
,然后將opencv_contrib
放入到opencv
目錄下,再將opencv放到你想安裝的地方。我放在D:\opencv
.進(jìn)入該目錄,新建一個(gè)build文件夾用于后續(xù)編譯。
安裝完cmake后在終端里輸入cmake-gui
即可看到CMake(cmake_gui)程序。
Where is the source code 選擇安裝opencv的目錄,我的是/home/pc/opencv
。Where to build the binaries 選擇build文件夾我的是/home/pc/opencv/build
然后點(diǎn)擊 configure 按鈕來(lái)配置,在彈出的頁(yè)面按下圖選擇后點(diǎn) Finish。
耐心等待進(jìn)度條讀完,直到輸出窗口看到 Configuring done
然后中間就會(huì)彈出一系列的標(biāo)紅的配置變量。在這需要重點(diǎn)關(guān)注幾個(gè)變量:
OPENCV_EXTRA_MODULES_PATH: 在 value 中瀏覽選擇opencv_contrib
的目錄即/home/pc/opencv/opencv_contrib/modules
;
ENABLE_CXX11: 勾選;
OPENCV_ENABLE_NONFREE: 勾選;
WITH_MSMF: 取消勾選;
更改完再點(diǎn)擊 configure 按鈕來(lái)配置,等待 Configuring done ,然后點(diǎn)擊 Generate 等待 Generating done 。
打開(kāi)終端,cd ~/opencv/build
輸入make -j8
.j后面數(shù)值取決于電腦的cpu核心數(shù)。該步驟耗時(shí)較長(zhǎng),且有可能會(huì)出現(xiàn)以下幾個(gè)問(wèn)題:
D:/opencv/modules/stitching/include/opencv2/stitching/detail/matchers.hpp:52:12: fatal error: opencv2/xfeatures2d/cuda.hpp: No such file or directory # include "opencv2/xfeatures2d/cuda.hpp"
進(jìn)入對(duì)應(yīng)的文件里將頭文件的相對(duì)路徑改為絕對(duì)路徑,以下同理;D:\opencv\opencv_contrib\modules\xfeatures2d\include\opencv2\xfeatures2d\cuda.hpp
D:/opencv/modules/stitching/src/precomp.hpp:91:12: fatal error: opencv2/xfeatures2d/cuda.hpp: No such file or directory # include "opencv2/xfeatures2d/cuda.hpp"
D:\opencv\opencv_contrib\modules\xfeatures2d\include\opencv2\xfeatures2d\cuda.hpp
D:\opencv\modules\stitching\src\matchers.cpp:52:10: fatal error: opencv2/xfeatures2d.hpp: No such file or directory #include "opencv2/xfeatures2d.hpp"
D:\opencv\opencv_contrib\modules\xfeatures2d\include\opencv2\xfeatures2d.hpp
D:\opencv\opencv_contrib\modules\xfeatures2d\include\opencv2/xfeatures2d.hpp:43:10: fatal error: opencv2/xfeatures2d/nonfree.hpp: No such file or directory #include "opencv2/xfeatures2d/nonfree.hpp"
D:\opencv\opencv_contrib\modules\xfeatures2d\include\opencv2\xfeatures2d\nonfree.hpp
D:\opencv\opencv_contrib\modules\xfeatures2d\src\boostdesc.cpp:653:20: fatal error: boostdesc_bgm.i: No such file or directory #include "boostdesc_bgm.i"
下載該壓縮包,解壓后將里面的文件放入/home/pc/opencv/opencv_contrib/modules/xfeatures2d/src
文件夾中。
到這應(yīng)該就編譯成功了。最后再運(yùn)行sudo make install
來(lái)安裝。
接下來(lái)最后將編譯好的OpenCV加入到環(huán)境變量中sudo gedit /etc/ld.so.conf.d/opencv.conf
執(zhí)行此命令后打開(kāi)的可能是一個(gè)空白的文件,不用管,只需要在文件末尾添加/usr/local/lib
。執(zhí)行sudo ldconfig
使得剛才的配置路徑生效。sudo gedit /etc/bash.bashrc
在最末尾添加
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig export PKG_CONFIG_PATH
最后在source
一下.bashrc
.
至此,Ubuntu下的OpenCV就配置完了。打開(kāi)VScode,在項(xiàng)目的CMakeLists.txt
文件中加入TARGET_LINK_LIBRARIES(3DR ${OpenCV_LIBS})
即可調(diào)用OpenCV庫(kù)啦。
聯(lián)系客服