opencv学习笔记(九)2维特征feature2d

上传人:碎****木 文档编号:220863224 上传时间:2021-12-09 格式:DOCX 页数:6 大小:13.49KB
返回 下载 相关 举报
opencv学习笔记(九)2维特征feature2d_第1页
第1页 / 共6页
opencv学习笔记(九)2维特征feature2d_第2页
第2页 / 共6页
opencv学习笔记(九)2维特征feature2d_第3页
第3页 / 共6页
亲,该文档总共6页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《opencv学习笔记(九)2维特征feature2d》由会员分享,可在线阅读,更多相关《opencv学习笔记(九)2维特征feature2d(6页珍藏版)》请在金锄头文库上搜索。

1、OpenCV 学习笔记九2 维特征 Feature2D基于特征点的图像匹配是图像处理中经常会遇到的问 题,手动选取特征点太麻烦了。比较经典常用的特征点自动 提取的方法有 Harris 特征、SIFT 特征、SURF 特征。先介绍利用 SURF 特征的特征描述方法,其操作封装在类SurfFeatureDetector 中,利用类内的 detect 函数可以检测出 SURF 特征的关键点,保存在 vector 容器中。其次部利用SurfDescriptorExtractor 类进展特征向量的相关计算。将之前的vector 变量变成向量矩阵形式保存在 Mat 中。最终强行匹配两幅图像的特征向量,利用

2、了类 BruteForceMatcher 中的函数 match。代码如下:view plain/* file SURF_descriptor* brief SURF detector + descritpor + BruteForce Matcher + drawing matches with OpenCV functions* author A. Huaman*/#include <stdio.h>#include <iostream>#include “opencv2/core/core.hpp“#include “opencv2/features2d/featu

3、res2d.hpp“#include “opencv2/highgui/highgui.hpp“using namespace cv; void readme();/* function main* brief Main function*/int main( int argc, char* argv )if( argc != 3 ) return -1; Mat img_1 =imread( argv1, CV_LOAD_IMAGE_GRAYSCALE ); Mat img_2 = imread( argv2, CV_LOAD_IMAGE_GRAYSCALE );if( !img_1.dat

4、a | !img_2.data ) return -1; /- Step 1: Detect the keypoints using SURF Detectorint minHessian = 400;SurfFeatureDetectordetector( minHessian );std:vector<KeyPoint> keypoints_1, keypoints_2;detector.detect( img_1, keypoints_1 );detector.detect( img_2, keypoints_2 );/- Step 2: Calculate descript

5、ors (feature vectors) SurfDescriptorExtractor extractor;Mat descriptors_1, descriptors_2;extractor pute( img_1, keypoints_1, descriptors_1 );extractor pute( img_2, keypoints_2, descriptors_2 );/- Step 3: Matching descriptor vectors with a brute force matcher BruteForceMatcher< L2<float> >

6、; matcher; std:vector< DMatch > matches;matcher.match( descriptors_1, descriptors_2, matches );/- Draw matchesMat img_matches; drawMatches( img_1, keypoints_1, img_2, keypoints_2, matches, img_matches );/- Show detected matchesimshow(“Matches“, img_matches );waitKey(0);return 0;/* function rea

7、dme*/void readme() std:cout << “Usage: ./SURF_descriptor <img1> <img2>“ << std:endl; 固然,进展强匹配的效果不够抱负,这里再介绍一种 FLANN 特征匹配算法。前两步与上述代码一样,第三步利用FlannBasedMatcher 类进展特征匹配,并只保存好的特征匹配点,代码如下:view plain/- Step 3: Matching descriptor vectors using FLANN matcherFlannBasedMatcher matcher;s

8、td:vector< DMatch > matches; matcher.match( descriptors_1, descriptors_2, matches ); double max_dist = 0; double min_dist = 100;/- Quick calculation of max and min distances between keypoints for( int i = 0; i < descriptors_1.rows; i+ ) double dist = matchesi.distance;if( dist < min_dist

9、 ) min_dist = dist;if( dist > max_dist ) max_dist = dist;printf(“- Max dist : %f n“, max_dist );printf(“- Mindist : %f n“, min_dist );/- Draw only “good“ matches (i.e. whose distance is less than 2*min_dist )/- PS.- radiusMatch can also be used here.std:vector< DMatch > good_matches;for( in

10、t i = 0; i < descriptors_1.rows; i+ ) if( matchesi.distance <2*min_dist ) good_matches.push_back( matchesi); /- Draw only “good“ matchesMat img_matches; drawMatches( img_1, keypoints_1, img_2, keypoints_2, good_matches, img_matches, Scalar:all(-1), Scalar:all(-1), vector<char>(), DrawMat

11、chesFlags:NOT_DRAW_SINGLE_POINTS );/- Show detected matchesimshow( “Good Matches“, img_matches );在 FLANN 特征匹配的根底上,还可以进一步利用Homography 映射找出物体。具体来说就是利用findHomography 函数利用匹配的关键点找出相应的变换, 再利用perspectiveTransform 函数映射点群。具体代码如下: view plain/- Localize the object from img_1 in img_2 std:vector<Point2f>

12、obj;std:vector<Point2f> scene;for( int i = 0; i < good_matches.size(); i+ )/- Get the keypoints from the good matches obj.push_back( keypoints_1 good_matchesi.queryIdx .pt);scene.push_back( keypoints_2 good_matchesi.trainIdx . pt );Mat H = findHomography( obj, scene, CV_RANSAC );/- Get the

13、corners from the image_1( the object to be “detected“ )Point2f obj_corners4 = cvPoint(0,0), cvPoint( img_1.cols, 0 ), cvPoint( img_1.cols, img_1.rows ), cvPoint( 0, img_1.rows ) ;Point scene_corners4;/- Map these corners in the scene( image_2)for( int i = 0; i < 4; i+ )double x = obj_cornersi.x;d

14、ouble y = obj_cornersi.y; double Z = 1./( H.at<double>(2,0)*x + H.at<double>(2,1)*y + H.at<double>(2,2) ); double X = ( H.at<double>(0,0)*x + H.at<double>(0,1)*y + H.at<double>(0,2) )*Z; double Y = ( H.at<double>(1,0)*x + H.at<double>(1,1)*y + H.at<

15、double>(1,2) )*Z; scene_cornersi = cvPoint( cvRound(X) + img_1.cols, cvRound(Y) );/- Draw lines between the corners (the mapped object in the scene - image_2 ) line( img_matches, scene_corners0, scene_corners1, Scalar(0, 255, 0), 2 );line( img_matches,scene_corners1, scene_corners2, Scalar( 0, 255, 0), 2 ); line( img_matches, scene_corners2, scene_corners3, Scalar( 0, 255, 0), 2 );line( img_matches,scene_corners3, scene_corners0, Scalar( 0, 255, 0), 2 );/- Show detected matchesimshow( “Good Ma

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 教育/培训

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号