资源简介
实现对图像的sift特征提取,并利用bow模型进行聚类,并对bow下的特征进行提取和保存
代码片段和文件信息
#include “stdafx.h“
#include “BowTrain.h“
#include “stdafx.h“
#include
#include
#include
#include
#include
#include “opencv2/core/core.hpp“
#include “opencv2/features2d/features2d.hpp“
#include “opencv2/calib3d/calib3d.hpp“
BowTrain::BowTrain()
{
}
BowTrain::~BowTrain()
{
}
void BowTrain::SiftExtract(string path)
{
//Mat features;
Directory dir;
string path2 = “.\\DATA\\“;
string exten2 = “*“;//“Image*“;//“*“
bool addPath2 = true;//false
vector foldernames = dir.GetListFolders(path2 exten2 addPath2);
int i = 0;
int j = 0;
int cnt = 0;
//Mat features;
//vector vectimageName;
int clusterNum =50;
cout << clusterNum << endl;
BOWKMeansTrainer trainer(clusterNum);
for each (String var in foldernames)
{
String dirtmp = foldernames.at(i);
vector foldernamesTmp = dir.GetListFiles(dirtmp exten2 addPath2);
cout << foldernames.at(i) << “ begin“ << endl;
j = 0;
for each (String vari in foldernamesTmp)
{
Mat curImg;
curImg = imread(foldernamesTmp.at(j).c_str() -1);
vector keyPoints;
Mat descriptor;
//sift关键点检测
SiftFeatureDetector detector;
detector.detect(curImg keyPoints);
//sift关键点描述,角度,强度等
SiftDescriptorExtractor extractor;
extractor.compute(curImg keyPoints descriptor);
//descriptor是每幅图像的sift关键点描述
trainer.add(descriptor);
j++;
}
cout << foldernames.at(i) << “ success“ << endl;
i++;
}
Mat dictionary = trainer.cluster();
//将提取的特征存放到xml文件方便使用
//cout << features.cols << “ “ << features.rows << endl;
/*FileStorage fs(“.\\Lab.xml“ FileStorage::WRITE);
fs << “features“ << features;
fs.release();*/
FileStorage fs(“.\\dictionary50.xml“ FileStorage::WRITE);
fs << “dictionary
- 上一篇:C++实现的欧拉放大代码
- 下一篇:CMAES算法C++
评论
共有 条评论