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

更新时间:2023-07-31 20:31:41 阅读: 评论:0

OpenCV学习笔记(九)——2维特征Feature2D
    基于特征点的图像匹配是图像处理中经常会遇到的问题,手动选取特征点太麻烦了。比较经典常用的特征点自动提取的办法有Harris特征、SIFT特征、SURF特征。先介绍利用SURF特征的特征描述办法,其操作封装在类SurfFeatureDetector中,利用类内的detect函数可以检测出SURF特征的关键点,保存在vector容器中。第二部利用SurfDescriptorExtractor类进行特征向量的相关计算。将之前的vector变量变成向量矩阵形式保存在Mat中。最后强行匹配两幅图像的特征向量,利用了类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/features2d.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( argv[1], CV_LOAD_IMAGE_GRAYSCALE );    Mat img_2 = imread( argv[2], CV_LOAD_IMAGE_GRAYSCALE );        if( !img_1.data || !img_2.data )    { return -1; }      //--
世界足球先生 Step 1: Detect the keypoints using SURF Detector    int minHessian = 400;      SurfFeatureDetector detector( minHessian );      std::vector<KeyPoint> keypoints_1, keypoints_2;      detector.detect( img_1, keypoints_1 );    detector.detect( img_2, keypoints_2 );      //-- Step 2: Calculate descriptors (feature vectors)    SurfDescriptorExtractor extractor;      Mat descriptors_1, descriptors_2;      pute( img_1, keypoints_1, descriptors_1 );    pute( img_2, keypoints_2, descriptors_2 );      //-- Step 3: Matching descriptor vectors with a brute force matcher    BruteForceMatcher< L2<float> > matcher;    std::vector< DMatch > matches;    matcher.match( descriptors_1, descriptors_2, matches );      //-- Draw matches    Mat img_matches;    drawMatches( img_1, keypoints_1, img_2, keypoints_2, matches, img_matches );      //-- Show detected matches    imshow("Matches", img_matches );      waitKey(0);      return 0;  }    /**  * @function readme  */  void readme()  { std::cout << " Usage: ./SURF_descriptor <img1> <img2>" << std::endl; }  文征明简介
当然,进行强匹配的效果不够理想,这里再介绍一种FLANN特征匹配算法。前两步与上述代码相同,第三步利用FlannBadMatcher类进行特征匹配,并只保留好的特征匹配点,
代码如下:view plain//-- Step 3: Matching descriptor vectors using FLANN matcher  FlannBadMatcher matcher;  std::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 < ws; i++ )  { double dist = matches[i].distance;    if( dist < min_dist ) min_dist = dist;    if( dist > max_dist ) max_dist = dist;  }    printf("-- Max dist : %f \n", max_dist );  printf("-- Min dist : %f \n", min_dist );    //-- Draw only "good" matches (i.e. who distance is less than 2*min_dist )  //-- PS.- radiusMatch can also be ud here.  std::vector< DMatch > good_matches;    for( int i = 0; i < ws; i++ )  { if( matches[i].distance < 2*min_dist )    { good_matches.push_back( matches[i]); }  }      //-- Draw only "good" matches  Mat img_matches;  drawMatches( img_1, keypoints_1, img_2, keypoints_2,                good_matches, img_matches, Scalar::all(-1), Scalar::all(-1),                vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );    //-- Show detected matches  imshow( "Good Matches", img_matches ); 
海阳之窗官网
熔化焊在FLANN特征匹配的基础上,还可以进一步利用Homography映射找出已知物体。具体来说就是利用findHomography函数利用匹配的关键点找出相应的变换,再利用perspectiveTransform函数映射点群。具体代码如下:view plain//-- Localize the object from img_1 in img_2  std::vector<Point2f> 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_matches[i].queryIdx ].pt );    scene.push_back( keypoints_2[ good_matches[i].trainIdx ].pt );  }    Mat H = findHomography( obj, scene, CV_RANSAC );    //-- Get the corners from the image_1 ( the object to be "detected" )  Point2f obj_corners[4] = { cvPoint(0,0), cvPoint( ls, 0 ), cvPoint( ls, ws ), cvPoint( 0, ws ) };  Point scene_corners[4];    //-- Map the corners in the scene ( image_2)  for( int i = 0; i < 4; i++ )  {    double x = obj_corners[i].x;    double y = obj_corners[i].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<double>(1,2) )*Z;    scene_corners[i] = cvPoint( cvRound(X) + ls, cvRound(
国庆节手抄报初一
学习规律Y) );  }      //-- Draw lines between the corners (the mapped object in the scene - image_2 )  line( img_matches, scene_corners[0], scene_corners[1], Scalar(0, 255, 0), 2 );  line( img_matches, scene_corners[1], scene_corners[2], Scalar( 0, 255, 0), 2 );  line( img_matches, scene_corners[2], scene_corners[3], Scalar( 0, 255, 0), 2 );  line( img_matches, scene_corners[3], scene_corners[0], Scalar( 0, 255, 0), 2 );    //-- Show detected matches  imshow( "Good Matches & Object detection", img_matches ); 

本文发布于:2023-07-31 20:31:41,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/1125068.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

下一篇:news feature
标签:特征   利用   匹配   学习   进行
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图