最近在使用opencv中自带的sift特征提取器,学艺不精导致很简单的东西搞了好几天没出来,今天解决了特意纪录下;
// opencv_empty_proj.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <opencv2/opencv.hpp>
#include <opencv2/features2d/features2d.hpp>
#include<opencv2/nonfree/nonfree.hpp>
#include<opencv2/legacy/legacy.hpp>
#include<vector>
#include <fstream>
using namespace std;
using namespace cv;
int _tmain(int argc, _TCHAR* argv[])
{
//从文件中读入图像
Mat img = imread("img.jpg");
//如果读入图像失败
if (img.empty()){
fprintf(stderr, "Can not load image \n");
return -1;
}
//显示图像
imshow("image before", img);
//sift特征检测
SiftFeatureDetector siftdtc;
vector<KeyPoint>kp;
siftdtc.detect(img, kp);//到这里其实只是提取到了特征点的具体位置和角度,其保存在kp中
Mat outimg;
drawKeypoints(img, kp, outimg);
imshow("image keypoints", outimg);//之前看别的人博客,到这就可以看到特征点画在图上了,一直以为那些点就是自己要用的那个,其实真正要用的在下边
SiftDescriptorExtractor extractor;//特征提取器
Mat descriptor; //这个描述符才是做实验要用的特征
extractor.compute(img, kp, descriptor);
ofstream file("./img.feature");//提取到的特征保存在这个文件中,128维,整数做实验以后再归一化一下
file << endl << descriptor << endl;
//此函数等待按键,按键盘任意键就返回
waitKey();
return 0;
}