@Utkarsh:我同意 SIFT 描述符需要关键点的方向和比例信息这一事实。David G. Lowe 的原始论文(来自 Scale-Invariant Keypoints 的独特图像特征)说,“为了实现方向不变性,描述符的坐标和梯度方向相对于关键点方向旋转”。而尺度信息用于在描述符计算过程中选择图像的高斯模糊程度。
然而,这篇文章的问题是关于计算给定像素的描述符。请注意,给定的像素位置不是使用所需程序计算的 SIFT 关键点。因此,在这种情况下,方向和比例信息不可用。因此,上一个答案中提到的代码以默认比例(即 1)和默认方向(不旋转邻域的梯度方向)计算给定像素的 SIFT 描述符。
@Teng Long:另外,我认为您用来匹配两个图像(原始图像和旋转图像)中的关键点的方法在某种程度上是模棱两可的。您应该分别对这两个图像运行 SIFT 关键点检测并分别计算它们对应的描述符。然后,您可以对这两组关键点使用蛮力匹配。
以下代码计算图像上的 SIFT 关键点及其 45 度旋转版本,然后使用蛮力匹配计算 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;
}
结果如下:
