您的位置:首页 > 运维架构

opencv SURF、SIFT的使用

2015-04-15 08:58 190 查看
#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: