图像拼接|——OpenCV3.4 stitching模块分析(一)续
上一篇讲了OpenCV几种特征检测方法,其中默认的是surf算法,但个人感觉sift效果更好一些。实际上在很多计算机视觉项目中,特征检测更多使用的是vlfeat的sift实现,下面我们就来看看。
stitching模块中使用SiftFeaturesFinder类来进行sift特征检测,SiftFeaturesFinder类的构造函数如下:
SiftFeaturesFinder::SiftFeaturesFinder()
{
#ifdef HAVE_OPENCV_XFEATURES2D
Ptr<SIFT> sift_ = SIFT::create();
if( !sift_ )
CV_Error( Error::StsNotImplemented, "OpenCV was built without SIFT support" );
sift = sift_;
#else
CV_Error( Error::StsNotImplemented, "OpenCV was built without SIFT support" );
#endif
}
find函数用来检测特征信息:
void SiftFeaturesFinder::find(InputArray image, ImageFeatures &features)
{
UMat gray_image;
CV_Assert((image.type() == CV_8UC3) || (image.type() == CV_8UC1));
if(image.type() == CV_8UC3)
{
cvtColor(image, gray_image, COLOR_BGR2GRAY);
}
else
{
gray_image = image.getUMat();
}
UMat descriptors;
sift->detectAndCompute(gray_image, Mat(), features.keypoints, descriptors);
features.descriptors = descriptors.reshape(1, (int)features.keypoints.size());
}
可以看出,如果想直接使用SIFT检测特征,可以这么应用:
#include
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/xfeatures2d.hpp"
using namespace cv;
using namespace cv::xfeatures2d;
int main()
{
Mat img = imread("4.jpg");
Mat gray_image;
cvtColor(img, gray_image, COLOR_BGR2GRAY);
Ptr<SIFT> sift = SIFT::create();
//关键点与描述子
std::vector<KeyPoint> keypoints;
Mat descriptors;
sift->detectAndCompute(gray_image, Mat(), keypoints, descriptors);
Mat output_img;
drawKeypoints(img, keypoints, output_img, Scalar(255, 0, 0));
std::cout << "Number of sift keypoints: " << keypoints.size() << std::endl;
namedWindow("sift");
imshow("sift", output_img);
waitKey(0);
return 0;
}
vlfeat是一个开源的轻量级的计算机视觉库,主要实现图像局部特征的提取和匹配以及一些常用的聚类算法。其对sift特征提取的各个步骤进行了封装,使用的方法如下:
完整代码:
#include
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include "vlfeat-0.9.20/vl/sift.h"
using namespace std;
using namespace cv;
void vl_sift_extract(const Mat & grey_img,
vector<VlSiftKeypoint> &kpts, vector<float*> &descriptors);
int main()
{
const string file = "4.jpg";
Mat img = imread(file, IMREAD_GRAYSCALE);
Mat color_img = imread(file);
vector<VlSiftKeypoint> kpts;
vector<float*> descriptors;
vl_sift_extract(img, kpts, descriptors);
cout << kpts.size() << endl;
//将VlSiftKeypoint转化为Point并画出
for (int i = 0; i < kpts.size(); i++)
{
//cout << "(" << kpts[i].x << ", " << kpts[i].y << ")" << endl;
Point center(cvRound(kpts[i].x), cvRound(kpts[i].y));
int radius = 3;
circle(color_img, center, radius, Scalar(0, 255, 0), 1, LINE_AA);
}
imshow("vl_sift", color_img);
waitKey(0);
return 0;
}
/*
Extract sift using vlfeat
parameters:
grey_img, 灰度图
kpts, keypoint list
descriptors, descriptor. Need to free the memory after using.
*/
void vl_sift_extract(const Mat & grey_img,
vector<VlSiftKeypoint> &kpts, vector<float*> &descriptors) {
// sift提取接受的是float类型的数据,要先将读到的数据图像转换为float
Mat grey_img_float = grey_img.clone();
grey_img_float.convertTo(grey_img_float, CV_32FC1);
const int width = grey_img.cols;
const int height = grey_img.rows;
VlSiftFilt * vl_sift = vl_sift_new(width, height, log2(min(width, height)), 3, 0);
vl_sift_set_peak_thresh(vl_sift, 0.0);
vl_sift_set_edge_thresh(vl_sift, 10.);
vl_sift_pix *data = (vl_sift_pix*)(grey_img_float.data);
// Detect keypoint and compute descriptor in each octave
if (vl_sift_process_first_octave(vl_sift, data) != VL_ERR_EOF) {
while (true) {
vl_sift_detect(vl_sift);
VlSiftKeypoint* pKpts = vl_sift->keys;
for (int i = 0; i < vl_sift->nkeys; i++) {
double angles[4];
// 计算特征点的方向,包括主方向和辅方向,最多4个
int angleCount = vl_sift_calc_keypoint_orientations(vl_sift, angles, pKpts);
// 对于方向多于一个的特征点,每个方向分别计算特征描述符
float *des = new float[128];
for (int i = 0; i < angleCount; i++) {
vl_sift_calc_keypoint_descriptor(vl_sift, des, pKpts, angles[0]);
}
descriptors.push_back(des);
kpts.push_back(*pKpts);
pKpts++;
}
// Process next octave
if (vl_sift_process_next_octave(vl_sift) == VL_ERR_EOF) {
break;
}
}
}
}
运行结果:
|
|
opencv共检测出1287个特征点,vlfeat检测出1079个特征点。
似乎vlfeat检测出的特征点分布更加均匀?以上就是opencv与vlfeat的sift实现比较,如有问题希望一起交流。
图像检索(1): 再论SIFT-基于vlfeat实现 - Brook_icv - 博客园