直接使用clion打开opencv2.4.10.4源代码 debug模式编译
打开opencv-2.4.10.4/CMakeLists.txt
修改CUDA相关为OFF
会遇到一个错误:
Problems were encountered while collecting compiler information:
cc1plus: fatal error: /--/--/cmake-build-release/modules/calib3d/perf_precomp.hpp: No such file or directory
cc1plus: fatal error: /--/--/cmake-build-release/modules/calib3d/perf_precomp.hpp: No such file or directory
cc1plus: fatal error: /--/--/cmake-build-release/modules/calib3d/perf_precomp.hpp: No such file or directory
cc1plus: fatal error: /--/--/cmake-build-release/modules/calib3d/perf_precomp.hpp: No such file or directory
cc1plus: fatal error: /--/--/cmake-build-release/modules/calib3d/perf_precomp.hpp: No such file or directory
cc1plus: fatal error: /--/--/cmake-build-release/modules/calib3d/perf_precomp.hpp: No such file or directory
cc1plus: fatal error: /--/--/cmake-build-release/modules/core/precomp.hpp: No such file or directory
cc1plus: fatal error: /--/--/cmake-build-release/modules/core/precomp.hpp: No such file or directory
cc1plus: fatal error: /--/--/cmake-build-release/modules/core/precomp.hpp: No such file or directory
cc1plus: fatal error: /--/--/cmake-build-release/modules/core/precomp.hpp: No such file or directory
cc1plus: fatal error: /--/--/cmake-build-release/modules/core/precomp.hpp: No such file or directory
cc1plus: fatal error: /--/--/cmake-build-release/modules/core/precomp.hpp: No such file or directory
cc1plus: fatal error: /--/--/cmake-build-release/modules/core/precomp.hpp: No such file or directory
cc1plus: fatal error: /--/--/cmake-build-release/modules/core/precomp.hpp: No such file or directory
cc1plus: fatal error: /--/--/cmake-build-release/modules/core/precomp.hpp: No such file or directory
cc1plus: fatal error: /--/--/cmake-build-release/modules/core/precomp.hpp: No such file or directory
cc1plus: fatal error: /--/--/cmake-build-release/modules/core/precomp.hpp: No such file or directory
cc1plus: fatal error: /--/--/cmake-build-release/modules/core/precomp.hpp: No such file or directory
cc1plus: fatal error: /--/--/cmake-build-release/modules/core/precomp.hpp: No such file or directory
cc1plus: fatal error: /--/--/cmake-build-release/modules/core/precomp.hpp: No such file or directory
cc1plus: fatal error: /--/--/cmake-build-release/modules/core/precomp.hpp: No such file or directory
cc1plus: fatal error: /--/--/cmake-build-release/modules/core/precomp.hpp: No such file or directory
cc1plus: fatal error: /--/--/cmake-build-release/modules/core/precomp.hpp: No such file or directory
cc1plus: fatal error: /--/--/cmake-build-release/modules/core/precomp.hpp: No such file or directory
cc1plus: fatal error: /--/--/cmake-build-release/modules/core/precomp.hpp: No such file or directory
cc1plus: fatal error: /--/--/cmake-build-release/modules/core/precomp.hpp: No such file or directory
cc1plus: fatal error: /--/--/cmake-build-release/modules/core/precomp.hpp: No such file or directory
将
ENABLE_PRECOMPILED_HEADERS
设置为OFF
删除之前编译出来的文件集 opencv-2.4.10.4/cmake-build-debug
重新编译
编译test模块还会遇到一个bug
解决方案:
https://blog.csdn.net/qq_32768743/article/details/74935818
以上我们就通过clion编译了oepncv
然后新建一个clion工程opencv_debug:
他的cmakelist.txt
cmake_minimum_required(VERSION 3.12)
project(opencv_debug)
set(CMAKE_CXX_STANDARD 14)
set(OpenCV_DIR ~/deepglint/install/opencv-2.4.10.4/cmake-build-debug)
find_package(OpenCV REQUIRED)
INCLUDE_DIRECTORIES(
${OpenCV_INCLUDE_DIRS}
)
add_executable(opencv_debug main.cpp)
target_link_libraries(opencv_debug ${OpenCV_LIBS})
将OpenCV_DIR设置为刚刚我们clion编译源代码的位置:
然后,新建一个例子:
#include <opencv2/core/core.hpp>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/nonfree/nonfree.hpp"
#include "opencv2/nonfree/features2d.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace cv;
using namespace std;
int main()
{
initModule_nonfree();//初始化模块,使用SIFT或SURF时用到
Ptr<FeatureDetector> detector = FeatureDetector::create( "SIFT" );//创建SIFT特征检测器
Ptr<DescriptorExtractor> descriptor_extractor = DescriptorExtractor::create( "SIFT" );//创建特征向量生成器
Ptr<DescriptorMatcher> descriptor_matcher = DescriptorMatcher::create( "BruteForce" );//创建特征匹配器
if( detector.empty() || descriptor_extractor.empty() )
cout<<"fail to create detector!";
//读入图像
Mat img1 = imread("box.png");
Mat img2 = imread("box_in_scene.png");
//特征点检测
double t = getTickCount();//当前滴答数
vector<KeyPoint> keypoints1,keypoints2;
detector->detect( img1, keypoints1 );//检测img1中的SIFT特征点,存储到keypoints1中
detector->detect( img2, keypoints2 );
cout<<"图像1特征点个数:"<<keypoints1.size()<<endl;
cout<<"图像2特征点个数:"<<keypoints2.size()<<endl;
//根据特征点计算特征描述子矩阵,即特征向量矩阵
Mat descriptors1,descriptors2;
descriptor_extractor->compute( img1, keypoints1, descriptors1 );
descriptor_extractor->compute( img2, keypoints2, descriptors2 );
t = ((double)getTickCount() - t)/getTickFrequency();
cout<<"SIFT算法用时:"<<t<<"秒"<<endl;
cout<<"图像1特征描述矩阵大小:"<<descriptors1.size()
<<",特征向量个数:"<<descriptors1.rows<<",维数:"<<descriptors1.cols<<endl;
cout<<"图像2特征描述矩阵大小:"<<descriptors2.size()
<<",特征向量个数:"<<descriptors2.rows<<",维数:"<<descriptors2.cols<<endl;
//画出特征点
Mat img_keypoints1,img_keypoints2;
drawKeypoints(img1,keypoints1,img_keypoints1,Scalar::all(-1),0);
drawKeypoints(img2,keypoints2,img_keypoints2,Scalar::all(-1),0);
//imshow("Src1",img_keypoints1);
//imshow("Src2",img_keypoints2);
//特征匹配
vector<DMatch> matches;//匹配结果
descriptor_matcher->match( descriptors1, descriptors2, matches );//匹配两个图像的特征矩阵
cout<<"Match个数:"<<matches.size()<<endl;
//计算匹配结果中距离的最大和最小值
//距离是指两个特征向量间的欧式距离,表明两个特征的差异,值越小表明两个特征点越接近
double max_dist = 0;
double min_dist = 100;
for(int i=0; i<matches.size(); i++)
{
double dist = matches[i].distance;
if(dist < min_dist) min_dist = dist;
if(dist > max_dist) max_dist = dist;
}
cout<<"最大距离:"<<max_dist<<endl;
cout<<"最小距离:"<<min_dist<<endl;
//筛选出较好的匹配点
vector<DMatch> goodMatches;
for(int i=0; i<matches.size(); i++)
{
if(matches[i].distance < 0.31 * max_dist)
{
goodMatches.push_back(matches[i]);
}
}
cout<<"goodMatch个数:"<<goodMatches.size()<<endl;
//画出匹配结果
Mat img_matches;
//红色连接的是匹配的特征点对,绿色是未匹配的特征点
drawMatches(img1,keypoints1,img2,keypoints2,goodMatches,img_matches,
Scalar::all(-1)/*CV_RGB(255,0,0)*/,CV_RGB(0,255,0),Mat(),2);
imwrite("MatchSIFT.png", img_matches);
// imshow("MatchSIFT",img_matches);
// waitKey();
return 0;
}
为了验证我们的想法,更改clion中opencv源代码工程的代码:
然后重新点编译,这时候编译会很快,因为只改动了某个模块
运行opencv_debug,我们得到如下结果:
~/CLionProjects/opencv_debug/cmake-build-debug/opencv_debug
yangninghua
yangninghua
图像1特征点个数:604
图像2特征点个数:969
yangninghua
yangninghua
SIFT算法用时:1.06016秒
图像1特征描述矩阵大小:[128 x 604],特征向量个数:604,维数:128
图像2特征描述矩阵大小:[128 x 969],特征向量个数:969,维数:128
Match个数:604
最大距离:432.236
最小距离:66.2571
goodMatch个数:34
验证后我们, 后续我们就可以通过断点 调试进入源代码
updatas:
其实使用任何方式编译都行,不太需要clion,只不过clion修改方便,比如,只要在cmakelist.txt中将 CMAKE_BUILD_TYPE=DEBUG改为debug就行
# ----------------------------------------------------------------------------
# OpenCV compiler and linker options
# ----------------------------------------------------------------------------
# In case of Makefiles if the user does not setup CMAKE_BUILD_TYPE, assume it's Release:
if(CMAKE_GENERATOR MATCHES "Makefiles|Ninja" AND "${CMAKE_BUILD_TYPE}" STREQUAL "")
set(CMAKE_BUILD_TYPE Release)
endif()
set(CMAKE_BUILD_TYPE Debug)
然后设置安装位置:
# Following block can broke build in case of cross-compilng
# but CMAKE_CROSSCOMPILING variable will be set only on project(OpenCV) command
# so we will try to detect crosscompiling by presense of CMAKE_TOOLCHAIN_FILE
if(NOT CMAKE_TOOLCHAIN_FILE)
# it _must_ go before project(OpenCV) in order to work
if(WIN32)
set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/install" CACHE PATH "Installation Directory")
else()
set(CMAKE_INSTALL_PREFIX "/usr/local" CACHE PATH "Installation Directory")
endif()
else(NOT CMAKE_TOOLCHAIN_FILE)
#Android: set output folder to ${CMAKE_BINARY_DIR}
set( LIBRARY_OUTPUT_PATH_ROOT ${CMAKE_BINARY_DIR} CACHE PATH "root for library output, set this to change where android libs are compiled to" )
# any crosscompiling
set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/install" CACHE PATH "Installation Directory")
endif(NOT CMAKE_TOOLCHAIN_FILE)
set(CMAKE_INSTALL_PREFIX "~/deepglint/install/opencv-2.4.13.7/install")
然后就直接编译,安装,
mkdir build
cmake ..
make -j4
make install -j4
我将他安装在了:
~/deepglint/install/opencv-2.4.13.7/install
然后打开clion
新建项目
cmakelist.txt:
cmake_minimum_required(VERSION 3.12)
project(opencv_debug)
set(CMAKE_CXX_STANDARD 14)
set(OpenCV_DIR ~/deepglint/install/opencv-2.4.13.7/install/share/OpenCV)
find_package(OpenCV REQUIRED)
INCLUDE_DIRECTORIES(
${OpenCV_INCLUDE_DIRS}
)
add_executable(opencv_debug main.cpp)
target_link_libraries(opencv_debug ${OpenCV_LIBS})
随便一个例子:
#include <opencv2/core/core.hpp>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/nonfree/nonfree.hpp"
#include "opencv2/nonfree/features2d.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace cv;
using namespace std;
int main()
{
initModule_nonfree();//初始化模块,使用SIFT或SURF时用到
Ptr<FeatureDetector> detector = FeatureDetector::create( "SIFT" );//创建SIFT特征检测器
Ptr<DescriptorExtractor> descriptor_extractor = DescriptorExtractor::create( "SIFT" );//创建特征向量生成器
Ptr<DescriptorMatcher> descriptor_matcher = DescriptorMatcher::create( "BruteForce" );//创建特征匹配器
if( detector.empty() || descriptor_extractor.empty() )
cout<<"fail to create detector!";
//读入图像
Mat img1 = imread("box.png");
Mat img2 = imread("box_in_scene.png");
//特征点检测
double t = getTickCount();//当前滴答数
vector<KeyPoint> keypoints1,keypoints2;
detector->detect( img1, keypoints1 );//检测img1中的SIFT特征点,存储到keypoints1中
detector->detect( img2, keypoints2 );
cout<<"图像1特征点个数:"<<keypoints1.size()<<endl;
cout<<"图像2特征点个数:"<<keypoints2.size()<<endl;
//根据特征点计算特征描述子矩阵,即特征向量矩阵
Mat descriptors1,descriptors2;
descriptor_extractor->compute( img1, keypoints1, descriptors1 );
descriptor_extractor->compute( img2, keypoints2, descriptors2 );
t = ((double)getTickCount() - t)/getTickFrequency();
cout<<"SIFT算法用时:"<<t<<"秒"<<endl;
cout<<"图像1特征描述矩阵大小:"<<descriptors1.size()
<<",特征向量个数:"<<descriptors1.rows<<",维数:"<<descriptors1.cols<<endl;
cout<<"图像2特征描述矩阵大小:"<<descriptors2.size()
<<",特征向量个数:"<<descriptors2.rows<<",维数:"<<descriptors2.cols<<endl;
//画出特征点
Mat img_keypoints1,img_keypoints2;
drawKeypoints(img1,keypoints1,img_keypoints1,Scalar::all(-1),0);
drawKeypoints(img2,keypoints2,img_keypoints2,Scalar::all(-1),0);
//imshow("Src1",img_keypoints1);
//imshow("Src2",img_keypoints2);
//特征匹配
vector<DMatch> matches;//匹配结果
descriptor_matcher->match( descriptors1, descriptors2, matches );//匹配两个图像的特征矩阵
cout<<"Match个数:"<<matches.size()<<endl;
//计算匹配结果中距离的最大和最小值
//距离是指两个特征向量间的欧式距离,表明两个特征的差异,值越小表明两个特征点越接近
double max_dist = 0;
double min_dist = 100;
for(int i=0; i<matches.size(); i++)
{
double dist = matches[i].distance;
if(dist < min_dist) min_dist = dist;
if(dist > max_dist) max_dist = dist;
}
cout<<"最大距离:"<<max_dist<<endl;
cout<<"最小距离:"<<min_dist<<endl;
//筛选出较好的匹配点
vector<DMatch> goodMatches;
for(int i=0; i<matches.size(); i++)
{
if(matches[i].distance < 0.31 * max_dist)
{
goodMatches.push_back(matches[i]);
}
}
cout<<"goodMatch个数:"<<goodMatches.size()<<endl;
//画出匹配结果
Mat img_matches;
//红色连接的是匹配的特征点对,绿色是未匹配的特征点
drawMatches(img1,keypoints1,img2,keypoints2,goodMatches,img_matches,
Scalar::all(-1)/*CV_RGB(255,0,0)*/,CV_RGB(0,255,0),Mat(),2);
imwrite("MatchSIFT.png", img_matches);
// imshow("MatchSIFT",img_matches);
// waitKey();
return 0;
}
通过调试器的步入就可以debug