一 安裝依賴
sudo apt install -y g++
sudo apt install -y cmake
sudo apt install -y make
二、安裝opencv依賴的庫
sudo apt-get install build-essential libgtk2.0-dev libgtk-3-dev libavcodec-dev libavformat-dev libjpeg-dev libswscale-dev libtiff5-dev
參考下以下opencv依賴庫
sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
安裝opencv依賴的時(shí)候,有可能會(huì)出現(xiàn)sbin/ldconfig.real:/usr/local/cuda-11.1/targets/x86_64-linux/lib/libcudnn_ops_infer.so.8 is not a symbolic link 這個(gè)問題,請(qǐng)參考Ubuntu20.04中配置opencv依賴時(shí)/usr/local/cuda-11.1/targets/x86_64-linux/lib/libcudnn_ops_infer.so.8 is not
三、下載OpenCV 4.5.5和對(duì)應(yīng)版本的opencv-contrib源文件 ,打開opencv官網(wǎng) opencv.org
解壓放到家目錄,進(jìn)入opencv4.5.5目錄里,右擊鼠標(biāo)打開終端??創(chuàng)建目錄build? ,再執(zhí)行 cd build
mkdir build
cd build
?四、cd build目錄,開始編譯opencv
(1)不添加擴(kuò)展庫
sudo cmake -D CMAKE_BUILD_TYPE=Release -D OPENCV_GENERATE_PKGCONFIG=YES -D WITH_FFMPEG=ON ..
?(2)編譯添加擴(kuò)展庫
sudo cmake -D CMAKE_INSTALL_PREFIX=/usr/local -D CMAKE_BUILD_TYPE=Release -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules ..
?(3)若裝過anaconda,可能存在一些 沖突,如果使用上面這條命令出現(xiàn):?recipe for target 'all' failed的報(bào)錯(cuò)。只需要改成
sudo cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_SHARED_LIBS=OFF -D WITH_OPENMP=ON -D ENABLE_PRECOMPILED_HEADERS=OFF ..
-D OPENCV_GENERATE_PKGCONFIG=YES
OpenCV4以上默認(rèn)不使用pkg-config,該編譯選項(xiàng)開啟生成opencv4.pc文件,支持pkg-config功能
opencv_contrib全部的版本下載地址:https://github.com/opencv/opencv_contrib/tags
五、編譯?
sudo make -j8
-j8? 8指同時(shí)使用8個(gè)進(jìn)程,速度快,你也可以根據(jù)自己的電腦情況,用-j2 或者-j16
六、安裝
sudo make install
七、配置pkg-config環(huán)境
opencv4.pc文件的默認(rèn)路徑:/usr/local/lib/pkgconfig/opencv4.pc若此目錄下沒有,可以使用以下命令搜索??sudo find / -iname opencv4.pc
sudo gedit /etc/profile.d/pkgconfig.sh
?把路徑加入到PKG_CONFIG_PATH,在文件中加入下面一行代碼
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH
保存并退出,然后source,使生效
source /etc/profile
八、驗(yàn)證是否成功
pkg-config --libs opencv4
?類似以上信息,即表示成功
九、配置動(dòng)態(tài)庫環(huán)境
sudo vim /etc/ld.so.conf.d/opencv4.conf
文件末尾加上OpenCV的lib路徑??
/usr/local/lib
?配置生效
sudo ldconfig
十、?測試
cd 到/opencv/samples/cpp/example_cmake目錄下,執(zhí)行如下命令
cmake .
make
./opencv_example
十一、vscode里配置?
1、安裝c/c++擴(kuò)展, 點(diǎn)擊文件,
新建一個(gè)文件夾,打開文件夾
,,新建test.cpp
文件,找張圖片,寫入下列代碼
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main(int argc, char* argv[]) {
const char* imagename = "test.jpg";//此處為你自己的圖片路徑
//從文件中讀入圖像
Mat img = imread(imagename, 1);
//如果讀入圖像失敗
if (img.empty()) {
fprintf(stderr, "Can not load image %s\n", imagename);
return -1;
}
//顯示圖像
imshow("image", img);
//此函數(shù)等待按鍵,按鍵盤任意鍵就返回
waitKey();
return 0;
}
?2、vscode調(diào)試主要就是改那三個(gè)json文件,opencv4和以前版本的文件夾不一樣,這里給出我適用的三個(gè)json文件
tasks.json
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file", /* 與launch.json文件里的preLaunchTask的內(nèi)容保持一致 */
"command": "/usr/bin/g++",
"args": [
"-std=c++11",
"-g",
//"${file}", /* 編譯單個(gè)文件 */
"${fileDirname}/*.cpp", /* 編譯多個(gè)文件 */
"-o",
"${fileDirname}/${fileBasenameNoExtension}", /* 輸出文件路徑 */
/* 項(xiàng)目所需的頭文件路徑 */
"-I","${workspaceFolder}/",
"-I","/usr/local/include/",
"-I","/usr/local/include/opencv4/",
"-I","/usr/local/include/opencv4/opencv2",
/* 項(xiàng)目所需的庫文件路徑 */
"-L", "/usr/local/lib",
/* OpenCV的lib庫 */
"/usr/local/lib/libopencv_*",
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++ - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}", //程序文件路徑
"args": [], //程序運(yùn)行需傳入的參數(shù)
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": true, //運(yùn)行時(shí)是否顯示控制臺(tái)窗口
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++ build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
?c_cpp_properties.json
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/local/include/opencv4"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu11",
"cppStandard": "gnu++14",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}
然后運(yùn)行圖片出來就OK了,完畢
? ? ? ? ? ? ? ? ? ? opencv 4.5編譯遇到的問題
1.boostdesc_bgm.i,vgg_generated_48.i等文件的缺失
鏈接: https://pan.baidu.com/s/13n1G8xcLoR8LEt8yetH9_A 提取碼: cv8w??
下載后解壓了,將文件夾內(nèi)的文件放入opencv_contrib/modules/xfeatures2d/src/ 路徑下即可
2.fatal?error:?features2d/test/test_detectors_regression.impl.hpp:?沒有那個(gè)文件或目錄?
將以下文件(文件不是在opencv\modules\features2d\test\ 里,就是在opencv_contrib-4.5.0\modules\xfeatures2d\test\ 中)放入opencv_contrib-4.5.0\modules\xfeatures2d\test\ 里(在你編譯生成的文件夾中的\modules\features2d\中也最好新建個(gè)test文件夾,將內(nèi)容粘貼過去)文章來源:http://www.zghlxwxcb.cn/news/detail-415237.html
test_descriptors_invariance.impl.hpp
test_descriptors_regression.impl.hpp
test_detectors_invariance.impl.hpp
test_detectors_regression.impl.hpp
test_features2d.cpp
test_invariance_utils.hpp
test_rotation_and_scale_invariance.cpp文章來源地址http://www.zghlxwxcb.cn/news/detail-415237.html
修改test_features2d.cpp文件下的
#include “features2d/test/test_detectors_regression.impl.hpp”
#include “features2d/test/test_descriptors_regression.impl.hpp”
去掉目錄,改成#include “test_detectors_regression.impl.hpp”
? ? ? ? ? ? ? ? ? ? ? ? ?#include “test_descriptors_regression.impl.hpp”
修改test_rotation_and_scale_invariance.cpp文件下的
#include “features2d/test/test_detectors_invariance.impl.hpp”
#include “features2d/test/test_descriptors_invariance.impl.hpp”
去掉目錄,改成#include “test_detectors_invariance.impl.hpp”
? ? ? ? ? ? ? ? ? ? ? ? ?#include “test_descriptors_invariance.impl.hpp”
到了這里,關(guān)于Ubuntu20下 vscode配置OpenCV 4.5.5 ? ? ? ? ? ? ? ? ? ? opencv 4.5編譯遇到的問題的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!