#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/nonfree/features2d.hpp>
#include <opencv2/legacy/legacy.hpp>
int main()
{
cv::Mat ima1=cv::imread("D:\\DEMO\\penpal.jpg");
cv::Mat ima2=cv::imread("D:\\DEMO\\res.jpg");
// 检测surf特征点
cv::vector<cv::KeyPoint> kp1 , kp2 ;
/*SurfFeatureDetector detector( 400 ) ;
detector.detect( image1 , kp1 );
detector.detect( image2 , kp2 );*/
cv::SiftFeatureDetector sift( 2500 );
sift.detect(ima1 , kp1);
sift.detect(ima2 , kp2);
//显示surf检测后的结果
cv::Mat dstimg2;
drawKeypoints( ima2 , kp2 , dstimg2 );
imshow("image2 keypoints" , dstimg2 );
cv::Mat dstimg1;
drawKeypoints( ima1 , kp1 , dstimg1 );
imshow("image1 keypoints" , dstimg1 );
//输出surf检测的结果
cv::vector<cv::KeyPoint> :: iterator kpCounts ;
for( kpCounts = kp2.begin() ; kpCounts != kp2.end() ; kpCounts++ )
{
std::cout<<"angle:"<<kpCounts->angle<<"\t" <<
kpCounts->class_id<<"\t"<<kpCounts->octave<<"\t"<<
kpCounts->pt<<"\t"<<kpCounts->response<<"\n";
}
// 描述surf特征点
cv::SurfDescriptorExtractor surfDesc ;
cv::Mat descriptros1 , descriptros2 ;
surfDesc.compute( ima1 , kp1 , descriptros1 );
surfDesc.compute( ima2 , kp2 , descriptros2 );
// 计算匹配点数
cv::BruteForceMatcher<cv::L2<float>>matcher ;
cv::vector<cv::DMatch> matches ;
matcher.match( descriptros1 , descriptros2 , matches );
cv::Mat imageMatches;
drawMatches( ima1 ,//输入图片1
kp1 ,//输入图片1的提取的特征点
ima2 , //输入图片2
kp2 , //输入图片2的提取出的特征点
matches ,//使用match函数获得的匹配特征点
imageMatches ,//输出图片
cv::Scalar(255,0,0)//画线颜色
);
imshow("image2" , imageMatches);
cv::waitKey( 0 );
return 0;
}
opencv SURF、SIFT的使用
最新推荐文章于 2023-04-14 17:29:25 发布