opencv 中sift 的使用

来源:互联网 发布:正大软件学校 编辑:程序博客网 时间:2024/05/29 07:53

opencv 中Algorithm 封装了很多算法,实验过程中,发现sift,surf等在nonfree模块中的代码,需要initModule_nonfree();来注册算法,不然create 会返回空指针,这在opencv文档中有介绍。

Ptr<Feature2D> sift = Algorithm::create<Feature2D>("Feature2D.FAST");

对于上面这样的代码虽然可以通过编译,但是没有意义,因为Feature2D 仅有的操作是detect and compute,而Fast 仅仅实现了detect,因此运行时会出错,但是不知道为什么opencv 让Feature2D.FAST 继承了 Feature2D.

实际上应该这样用:

Ptr<FeatureDetector> Fast_detect = Algorithm::create<FeatureDetector>("Feature2D.FAST");


一些测试代码:

#include <opencv2/opencv.hpp>
#include <opencv2/nonfree/nonfree.hpp>
#include <opencv2/nonfree/features2d.hpp>
using namespace std;
using namespace cv;

void main()
{
Mat image = imread("f:\\fruits.jpg");
Mat descriptors;
vector<KeyPoint> keypoints;
initModule_nonfree();
Ptr<Feature2D> sift = Algorithm::create<Feature2D>("Feature2D.SIFT");
(*sift)(image, noArray(), keypoints, descriptors);
Ptr<FeatureDetector> Fast_detect = Algorithm::create<FeatureDetector>("Feat ure2D.FAST");
//Ptr<DescriptorExtractor> Fast_extract = Algorithm::create<Feature2D>("Featur e2D.FAST");
//Fast_detect->detect(image,keypoints);
//Fast_extract->compute(img,kepoints,descriptors);
drawKeypoints(image, keypoints, image, Scalar(255,0,0),4);
imshow("test", image);
waitKey();

}


原创粉丝点击