SIFT+SVM 使用Bag of Features
来源:互联网 发布:矩阵计算 戈卢布 pdf 编辑:程序博客网 时间:2024/05/21 17:36
Bag-of-Features Descriptor on SIFT Features with OpenCV (BoF-SIFT)
通过DICTIONARY_BUILD 1 建立字典通过DICTIONARY_BUILD 2 计算特征
通过特征可以进行SVM训练
// 0515_BoFSIFT.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <opencv/cv.h>#include <opencv/highgui.h>#include <opencv2/nonfree/features2d.hpp>#include <fstream>using namespace cv;using namespace std;#define DICTIONARY_BUILD 1 // set DICTIONARY_BUILD 1 to do Step 1, otherwise it goes to step 2int _tmain(int argc, _TCHAR* argv[]){#if DICTIONARY_BUILD == 1//Step 1 - Obtain the set of bags of features.vector<string> img_path; int nLine = 0; string buf; ifstream svm_data( "_list.txt" ); while( svm_data ) { if( getline( svm_data, buf ) ) { img_path.push_back( buf ); } } svm_data.close(); //to store the input file nameschar * filename = new char[100];//to store the current input imageMat input;//To store the keypoints that will be extracted by SIFTvector<KeyPoint> keypoints;//To store the SIFT descriptor of current imageMat descriptor;//To store all the descriptors that are extracted from all the images.Mat featuresUnclustered;//The SIFT feature extractor and descriptorSiftDescriptorExtractor detector;//I select 20 (1000/50) images from 1000 images to extract feature descriptors and build the vocabularyint len = img_path.size();for(int f=0;f<len;f+=50){//create the file name of an image//open the fileinput = imread(img_path[f], CV_LOAD_IMAGE_GRAYSCALE); //Load as grayscale//detect feature pointsdetector.detect(input, keypoints);//compute the descriptors for each keypointdetector.compute(input, keypoints,descriptor);//put the all feature descriptors in a single Mat object featuresUnclustered.push_back(descriptor);//print the percentageprintf("%i percent done\n",f/10);}//Construct BOWKMeansTrainer//the number of bagsint dictionarySize=200;//define Term CriteriaTermCriteria tc(CV_TERMCRIT_ITER,100,0.001);//retries numberint retries=1;//necessary flagsint flags=KMEANS_PP_CENTERS;//Create the BoW (or BoF) trainerBOWKMeansTrainer bowTrainer(dictionarySize,tc,retries,flags);//cluster the feature vectorsMat dictionary=bowTrainer.cluster(featuresUnclustered);//store the vocabularyFileStorage fs("dictionary.yml", FileStorage::WRITE);fs << "vocabulary" << dictionary;fs.release();#else//Step 2 - Obtain the BoF descriptor for given image/video frame. vector<string> img_path; int nLine = 0; string buf; ifstream svm_data( "_list.txt" ); while( svm_data ) { if( getline( svm_data, buf ) ) { img_path.push_back( buf ); } } svm_data.close(); //prepare BOW descriptor extractor from the dictionary Mat dictionary; FileStorage fs("dictionary.yml", FileStorage::READ);fs["vocabulary"] >> dictionary;fs.release();//create a nearest neighbor matcherPtr<DescriptorMatcher> matcher(new FlannBasedMatcher);//create Sift feature point extracterPtr<FeatureDetector> detector(new SiftFeatureDetector());//create Sift descriptor extractorPtr<DescriptorExtractor> extractor(new SiftDescriptorExtractor);//create BoF (or BoW) descriptor extractorBOWImgDescriptorExtractor bowDE(extractor,matcher);//Set the dictionary with the vocabulary we created in the first stepbowDE.setVocabulary(dictionary);//To store the image file namechar * filename = new char[100];//To store the image tag name - only for save the descriptor in a filechar * imageTag = new char[10];//open the file to write the resultant descriptorFileStorage fs1("descriptor.yml", FileStorage::WRITE);//the image file with the location. change it according to your image file location//read the imageMat img=imread(img_path[2],CV_LOAD_IMAGE_GRAYSCALE);//To store the keypoints that will be extracted by SIFTvector<KeyPoint> keypoints;//Detect SIFT keypoints (or feature points)detector->detect(img,keypoints);//To store the BoW (or BoF) representation of the imageMat bowDescriptor;//extract BoW (or BoF) descriptor from given imagebowDE.compute(img,keypoints,bowDescriptor);//prepare the yml (some what similar to xml) filesprintf(imageTag,"img1");//write the new BoF descriptor to the filefs1 << imageTag << bowDescriptor;//You may use this descriptor for classifying the image.//release the file storagefs1.release();#endifprintf("\ndone\n");return 0;}
0 0
- SIFT+SVM 使用Bag of Features
- Bag-of-Features Descriptor on SIFT ORB SURF (BoF-SIFT)
- Bag of features
- Bag of features
- bag of features
- Bag of Features 方法
- Bag of features
- Bag of Words/Bag of Features
- Bag of words and Bag of features
- Bag of Features (BoF) 简介
- Bag of Features (BoF) 简介
- Bag of Words/Bag of Features的Matlab源码发布
- Bag of Features (BOF)图像检索算法
- Bag of Features (BOF)图像检索算法
- matlab+VC 实现Bag of features
- Bag of Features 之于图像检索
- Bag of Features (BOF)图像检索算法
- Bag of Features (BOF)图像检索算法
- JavaScript 注入攻击
- Spring security防止用户重复登录
- Simple and Protected GSSAPI Negotiation Mechanism (SPNEGO)
- listview baseadapter
- img内联替换元素
- SIFT+SVM 使用Bag of Features
- 2101-view-autocomplete
- BlueTooth: 蓝牙基带数据传输机理分析
- Wait延时及定时查询
- html 前台通用表单
- 医调项目之C# 服务端与客户端JS通讯数据加密
- 获取 磁盘空间、文件夹等大小
- 无法打开包括文件:“afxcontrolbars.h”
- 特权级变换中调用门的学习