ubuntu24.04lts cmake编译 opencv4.5.4 contrib的一些问题
编译之前一定要安装好必须的库,否则即使提示编译成功,调用opencv后也可能会有问题
sudo apt-get update
sudo apt-get upgradesudo apt-get install -y g++
sudo apt-get install -y cmake
sudo apt-get install -y make
sudo apt-get install -y wget
sudo apt-get install -y unzip
sudo apt-get install -y gitsudo apt-get install build-essential pkg-config sudo apt-get install libgtk2.0-dev libgtk-3-dev libglib2.0-dev libavcodec-dev libavformat-dev libswscale-dev libavutil-dev libv4l-dev liblapacke-dev libxvidcore-dev libx264-devsudo apt-get install python-dev python-numpysudo apt-get install libgstreamer-plugins-base1.0-dev libgstreamer1.0-devsudo apt-get install libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper1 libjasper-dev libdc1394-22-dev libopenexr-dev libwebp-devsudo apt-get install libatlas-base-dev gfortran sudo apt-get install ffmpeg
下载opencv4.5.4源码和opencv_contrib4.5.4后如下存放
sudo cmake -D CMAKE_BUILD_TYPE=RELEASE -D OPENCV_ENABLE_NONFREE=ON -D OPENCV_EXTRA_MODULES_PATH=/home/mwj/open4.5.4/opencv-4.5.4/opencv_contrib-4.5.4/modules ..
CMAKE_INSTALL_PREFIX 参数没有设置,从编译生成结果来看默认是放在了/usr/local下面
下次考虑设置成
-D CMAKE_INSTALL_PREFIX=/usr/local/opencv4.5.4
那么命令应该是这个样子,如果编译多个版本的opencv,生成后就不会乱了
sudo cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local/opencv4.5.4 -D OPENCV_ENABLE_NONFREE=ON -D OPENCV_EXTRA_MODULES_PATH=/home/mwj/open4.5.4/opencv-4.5.4/opencv_contrib-4.5.4/modules ..
如果cmake没有错误,那么接下来(-j6的6表示cpu核心数)
sudo make -j6
如果这一步也没有出错,那继续以下命令
sudo make install
到这一步还不能直接在vscode中调用opencv
打开命令窗口
sudo gedit /etc/ld.so.conf
打开文件后在最后一行增加
include /usr/local/lib
遇到
问题1:typed_graph.hpp:106:53: error: ‘m_srcGraph’ was not declared in this scope
问题2:OpenCV Error:If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config,then re-run cmake
The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function 'cvShowImage'
sudo apt-get install libgtk2.0-dev
安装好这2个依赖之后,重新编译,生成
问题3:图片能出来但有个提示【Failed to load module "canberra-gtk-module" 】,这个安装后,重新运行就可以了
sudo apt-get install libcanberra-gtk-module
问题4:各种文件不能下载(常用的解决方法是科学上网,有时还不能下载,只能手动下载放在对应文件夹中了,有时迅雷 idm都很有用)
参考网上的说法hosts文件中可以设置一下:
cmakez中需要下载的文件都来源于https://raw.githubusercontent.com这个网站,但是这个网站被墙了,所以会下载失败,我试过开代理,但是没有用。找个查ip的网站,查到https://raw.githubusercontent.com的ip,有四个,全部添加到/etc/hosts文件后面,格式如下:
185.199.108.133 中要去查询的
这个可以通过下面网站查询:
What Is My IP Address? Free IP Lookup
http://whoissoft.com/
Dns检测|Dns查询 - 站长工具
185.199.108.133 raw.githubusercontent.com185.199.109.133 raw.githubusercontent.com185.199.110.133 raw.githubusercontent.com185.199.111.133 raw.githubusercontent.com
----------问题都解决之后可以测试下效果----------------
vscode中.vscode配置
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
}
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": [], //程序运行需传入的参数"stopAtEntry": false,"cwd": "${fileDirname}","environment": [],"externalConsole": true, //运行时是否显示控制台窗口"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"}]
}
tasks.json
{"tasks": [{"type": "cppbuild","label": "C/C++: g++ build active file", /* 与launch.json文件里的preLaunchTask的内容保持一致 */"command": "/usr/bin/g++","args": ["-std=c++11","-g",//"${file}", /* 编译单个文件 */"${fileDirname}/*.cpp", /* 编译多个文件 */"-o","${fileDirname}/${fileBasenameNoExtension}", /* 输出文件路径 *//* 项目所需的头文件路径 */"-I","${workspaceFolder}/","-I","/usr/local/include/","-I","/usr/local/include/opencv4/","-I","/usr/local/include/opencv4/opencv2",/* 项目所需的库文件路径 */"-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"
}
找了一段代码测试
#include <opencv2/opencv.hpp>#include <opencv2/xfeatures2d.hpp>//SIFT SURF#include<iostream>
#include<vector>constexpr auto path0 = "1.jpg";
constexpr auto path1 = "2.jpg";int main() {cv::Mat image0 = cv::imread(path0, 1);cv::Mat image1 = cv::imread(path1, 1);cv::imshow("image0", image0);cv::imshow("image1", image1);/*step1:特征检测器*/cv::Ptr<cv::xfeatures2d::SURF> detector;detector = cv::xfeatures2d::SURF::create(800); //800为海塞矩阵阈值,越大越精准/*-----SURF----cv::Ptr<cv::xfeatures2d::SURF> detector;detector = cv::xfeatures2d::SURF::create(800); //800为海塞矩阵阈值,越大越精准-----SIFT-----cv::Ptr<cv::xfeatures2d::SIFT> detector;detector = cv::xfeatures2d::SIFT::create(800);//800为保留的点数-----ORB------cv::Ptr<cv::ORB> detector;detector = cv::ORB::create(800);//保留点数-----STAR-----cv::Ptr<cv::xfeatures2d::StarDetector> detector;detector = cv::xfeatures2d::StarDetector::create();-----MSD-----cv::Ptr<cv::xfeatures2d::MSDDetector> detector;detector = cv::xfeatures2d::MSDDetector::create();*/std::vector <cv::KeyPoint > key0;std::vector <cv::KeyPoint > key1;detector->detect(image0,key0,cv::noArray());detector->detect(image1, key1, cv::noArray());/*step2:描述子提取器*/cv::Ptr<cv::xfeatures2d::SURF> Extractor;Extractor = cv::xfeatures2d::SURF::create(800);/*以下都是xfeature2d中的提取器-----SURF----------SIFT----------LUCID---------BriefDescriptorExtractor---------VGG----------BoostDesc-----*/cv::Mat descriptor0, descriptor1;Extractor->compute(image0, key0, descriptor0);Extractor->compute(image1, key1, descriptor1);/*step3:匹配器*/cv::BFMatcher matcher;//暴力匹配器std::vector<cv::DMatch> matches; // 存放匹配结果std::vector<cv::DMatch> good_matches; //存放好的匹配结果matcher.match(descriptor0, descriptor1, matches); std::sort(matches.begin(), matches.end()); //筛选匹配点,根据match里面特征对的距离从小到大排序int ptsPairs = std::min(50, (int)(matches.size() * 0.15));std::cout << "匹配点数为" << ptsPairs << std::endl;for (int i = 0; i < ptsPairs; i++){good_matches.push_back(matches[i]); //距离最小的50个压入新的DMatch}cv::Mat result;cv::drawMatches(image0, key0,image1, key1,good_matches, result,cv::Scalar::all(-1), cv::Scalar::all(-1),std::vector<char>(),cv::DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS); //绘制匹配点 cv::imshow("result", result);cv::waitKey(0);
}
效果图: