OpenCV 如何计算给定像素的 SIFT 描述符

标签 opencv computer-vision sift feature-descriptor

大家好,我正在使用 opencv3 和 contrib。问题是我想计算给定像素的筛选描述符(不使用检测到的关键点)。

我正在尝试用给定的像素构建一个关键点向量。但是,要创建关键点,除了像素位置外,我还需要知道大小信息。

KeyPoint (Point2f _pt, float _size, float _angle=-1, float _response=0, int _octave=0, int _class_id=-1)

谁能告诉我构造函数中的大小是多少?我需要角度信息来计算筛选描述符吗?以及如何使用 poencv3 计算它们。

最佳答案

@Utkarsh:我同意 SIFT 描述符需要关键点的方向和比例信息这一事实。 David G. Lowe 的原始论文(Distinctive Image Features from Scale-Invariant Keypoints)说,“为了实现方向不变性,描述符的坐标和梯度方向相对于关键点方向旋转”。而尺度信息用于在计算描述符时选择图像的高斯模糊级别

但是,这篇文章中的问题是关于计算给定像素的描述符。请注意,给定的像素位置不是使用所需过程计算的 SIFT 关键点。因此在这种情况下方向和比例信息不可用。所以 code上一个答案中提到的以默认比例(即 1)和默认方向(不旋转邻域的梯度方向)在给定像素处计算 SIFT 描述符。

@Teng Long:另外,我认为您用于匹配两幅图像(原始图像和旋转图像)中的关键点的方法在某种程度上是模棱两可的。您应该分别在两个图像上运行 SIFT 关键点检测并分别计算它们相应的描述符。然后,您可以使用蛮力匹配这两组关键点。

以下代码计算图像及其 45 度旋转版本的 SIFT 关键点,然后使用蛮力匹配计算 SIFT 关键点描述符。

# include "opencv2/opencv_modules.hpp"
# include "opencv2/core/core.hpp"
# include "opencv2/features2d/features2d.hpp"
# include "opencv2/highgui/highgui.hpp"
# include "opencv2/nonfree/features2d.hpp"
# include "opencv2\imgproc\imgproc.hpp"
# include <stdio.h>

using namespace cv;

int main( int argc, char** argv )
{
    Mat img_1, img_2;

    // Load image in grayscale format
    img_1 = imread( "scene.jpg", CV_LOAD_IMAGE_GRAYSCALE );     

    // Rotate the input image without loosing the corners 
    Point center = Point(img_1.cols / 2, img_1.rows / 2);
    double angle = 45, scale = 1;
    Mat rot = getRotationMatrix2D(center, angle, scale);
    Rect bbox = cv::RotatedRect(center, img_1.size(), angle).boundingRect();
    rot.at<double>(0,2) += bbox.width/2.0 - center.x;
    rot.at<double>(1,2) += bbox.height/2.0 - center.y;
    warpAffine(img_1, img_2, rot, bbox.size());

    // SIFT feature detector
    SiftFeatureDetector detector;
    std::vector<KeyPoint> keypoints_1, keypoints_2;

    detector.detect( img_1, keypoints_1 );
    detector.detect( img_2, keypoints_2 );

    // Calculate descriptors 
    SiftDescriptorExtractor extractor;
    Mat descriptors_1, descriptors_2;

    extractor.compute( img_1, keypoints_1, descriptors_1 );
    extractor.compute( img_2, keypoints_2, descriptors_2 );

    // Matching descriptors using Brute Force
    BFMatcher matcher(NORM_L2);
    std::vector<DMatch> matches;
    matcher.match(descriptors_1, descriptors_2, matches);


    //-- Quick calculation of max and min distances between Keypoints
    double max_dist = 0; double min_dist = 100;

    for( int i = 0; i < descriptors_1.rows; i++ )
    { double dist = matches[i].distance;
      if( dist < min_dist ) min_dist = dist;
      if( dist > max_dist ) max_dist = dist;
    }   

    // Draw only "good" matches (i.e. whose distance is less than 2*min_dist,
    //-- or a small arbitary value ( 0.02 ) in the event that min_dist is very
    //-- small)
    std::vector< DMatch > good_matches;

    for( int i = 0; i < descriptors_1.rows; i++ )
    { if( matches[i].distance <= max(2*min_dist, 0.02) )
      { 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 );

    waitKey(0);
    return 0;
  }

结果如下:

enter image description here

关于OpenCV 如何计算给定像素的 SIFT 描述符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39263646/

相关文章:

python - 我遇到了这个错误(-215 :Assertion failed) ! ssize.empty() in function 'resize' in OpenCV

opencv - 在 OpenCV 中查找像素的世界空间坐标

c++ - 任意二维集的类 RANSAC 实现

python - OpenCV:去除背景噪音并增加信号强度

python - 从OpenCV中的图像定位和提取(未知)书籍

c++ - 透视矩阵的旋转角度

image-processing - 带有 SIFT/VLFEAT 的图像描述符

computer-vision - 使用 TensorFlow 构建简单的图像搜索

android - 使用 opencv for android 绘制火柴

OpenCV3.0 - 模块没有属性 SIFT