赞
踩
CLion 现在也以 snap 软件包的形式提供。如果您使用 Ubuntu 16.04 或更高版本,可以使用命令行安装 CLion。
sudo snap install clion --classic
安装好后可以在应用里面找到Clion图标,打开之后选择试用30天或激活
需要中文界面 可在插件市场安装中文语言包
运行默认代码
也可用自己的代码测试hello c++
测试opencv(之前编译过4.5.4版本ubuntu24.04lts cmake编译 opencv4.5.4 contrib的一些问题-CSDN博客)
CMakeLists.txt 内容设置如下,有
- cmake_minimum_required(VERSION 3.28)
- project(testopen)
-
- set(CMAKE_CXX_STANDARD 11)
-
- add_executable(testopen main.cpp)
-
- find_package(OpenCV REQUIRED)
-
- include_directories(${OpenCV_INCLUDE_DIRS})
-
- target_link_libraries(testopen ${OpenCV_LIBS})
运行后有错误
terminate called after throwing an instance of 'cv::Exception'
what(): OpenCV(4.5.4) /home/mwj/open4.5.4/opencv-4.5.4/modules/core/src/array.cpp:2494: error: (-206:Bad flag (parameter or structure field)) Unrecognized or unsupported array type in function 'cvGetMat'
经网上查询后,是图片路径写法的问题,本来在同一个目录的图片,直接写图片名就可以了,之前在vscode中运行正常,Clion中可能需要设置,当然如果写绝对路径应该是没有问题的
constexpr auto path0 = "/home/mwj/CLionProjects/testopen/1.jpg";
constexpr auto path1 = "/home/mwj/CLionProjects/testopen/2.jpg";
或者如下图设置
- #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);
- }

我记得编译opencv的时候,设置的的是RELEASE,但现在不管是debug还是release都能运行,不清楚为什么,有了解情况的朋友可留言
-D CMAKE_BUILD_TYPE=RELEASE
下面是2个问题对话AI
1. 问:ubuntu为什么clion中CMakeLists.txt 内容 这样设置就能找到opencv目录了
答:在 Ubuntu 中使用 CLion 配置 CMake 项目时,通过在 CMakeLists.txt
文件中添加如下内容可以成功找到 OpenCV 的目录,原因如下:
find_package(OpenCV REQUIRED)
:
OpenCVConfig.cmake
或 opencv-config.cmake
),这些路径通常是在安装 OpenCV 时自动设置的。/usr/local
或 /usr
)下,CMake 会自动找到它。include_directories(${OpenCV_INCLUDE_DIRS})
:
${OpenCV_INCLUDE_DIRS}
是 find_package
查找到的头文件目录的变量,CMake 自动填充它。target_link_libraries(testopen ${OpenCV_LIBS})
:
${OpenCV_LIBS}
是 find_package
发现的库文件路径及库名的变量。由于 find_package(OpenCV REQUIRED)
会根据 OpenCV 的安装位置自动配置所需的变量,通常情况下,你不需要手动指定 OpenCV 的目录。CMake 会根据系统配置和安装位置自动找到这些路径,因此在 CLion 中这样设置 CMakeLists.txt
就能正确找到 OpenCV。
2.问:ubuntu编译c++ opencv的时候,设置的是RELEASE,但现在不管是debug还是release都能运行
答:
在 Ubuntu 上编译 C++ 项目时,通常你会使用 Release
或 Debug
构建类型。这两种构建类型的主要区别在于:
如果你的项目在 Debug
和 Release
模式下都能运行,有以下几个可能的原因:
OpenCV 被配置为多配置(Multi-Configuration)构建:
apt-get
或其他包管理器安装的,通常会包含多个配置版本。也就是说,它可能同时包含 Release
和 Debug
版本的库文件,并且可以在两种构建模式下使用。Release
还是 Debug
,CMake 都会找到相应的 OpenCV 库版本。OpenCV 是动态链接的:
.so
文件),系统库路径中可能已经包含了 OpenCV 的动态库,并且这些库文件可以在任何构建模式下使用。调试版本和发布版本的兼容性:
Debug
和 Release
构建之间的差异并不会对代码的运行产生直接影响,特别是在没有使用特定的优化或调试功能时。Debug
和 Release
。CMake 的默认配置:
cmake -DCMAKE_BUILD_TYPE=Release
),CMake 可能会使用系统的默认配置,这可能是 Release
,但如果你的项目是以一种通用的方式设置的,那么它可以兼容不同的构建类型。Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。