您的位置:首页 > 运维架构

opencv 中sift 的使用

2013-09-16 22:59 459 查看
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();

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: