博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
学习OpenCV——BOW特征提取函数(特征点篇)
阅读量:6445 次
发布时间:2019-06-23

本文共 7640 字,大约阅读时间需要 25 分钟。

from:  http://www.xuebuyuan.com/582331.html

简单的通过特征点分类的方法:                                                                      

一、train

1.提取+/- sample的feature,每幅图提取出的sift特征个数不定(假设每个feature有128维)

2.利用聚类方法(e.g K-means)将不定数量的feature聚类为固定数量的(比如10个)words即BOW(bag of word)

(本篇主要完成以上的工作!)

3.normalize,并作这10个类的直方图e.g [0.1,0.2,0.7,0...0];

4.将each image的这10个word作为feature_instance 和 (手工标记的) label(+/-)进入SVM训练

 

二、predict

1. 提取test_img的feature(如137个)

2. 分别求each feature与10个类的距离(e.g. 128维欧氏距离),确定该feature属于哪个类

3. normalize,并作这10个类的直方图e.g [0,0.2,0.2,0.6,0...0];

4. 应用SVM_predict进行结果预测

 

 

通过OpenCV实现feature聚类 BOW                                                             

首先在此介绍一下OpenCV的特征描述符与BOW的通用函数。

主要的通用接口有:

 

1.特征点提取

Ptr<FeatureDetector> FeatureDetector::create(const string& detectorType)

Ptr
FeatureDetector::create(const string& detectorType)// "FAST" – FastFeatureDetector // "STAR" – StarFeatureDetector // "SIFT" – SIFT (nonfree module)//必须使用 initModule_nonfree()初始化// "SURF" – SURF (nonfree module)//同上; // "ORB" – ORB // "MSER" – MSER // "GFTT" – GoodFeaturesToTrackDetector // "HARRIS" – GoodFeaturesToTrackDetector with Harris detector enabled // "Dense" – DenseFeatureDetector // "SimpleBlob" – SimpleBlobDetector

 

根据以上接口,测试不同的特征点:

对同一幅图像进行水平翻转前后的两幅图像检测特征点检测结果,

检测到的特征点的坐标类型为:pt: int / float(与keyPoint的性质有关)

数量分别为num1, num2,

 

 "FAST" – FastFeatureDetector           pt:int (num1:615  num2:618)

 "STAR" – StarFeatureDetector           pt:int (num1:43   num2:42 )
 "SIFT" – SIFT (nonfree module)          pt:float(num1:155  num2:135)            //必须使用 initModule_nonfree()初始化
 "SURF" – SURF (nonfree module)     pt:float(num1:344  num2:342)           
//同上; 
 "ORB" – ORB                                        pt:float(num1:496  num2:497)
 "MSER" – MSER                                 pt:float(num1:51   num2:45 )
 "GFTT" – GoodFeaturesToTrackDetector        pt:int (num1:744  num2:771)
 "HARRIS" – GoodFeaturesToTrackDetector with Harris detector enabled         pt:float(num1:162  num2:160)
 "Dense" – DenseFeatureDetector          pt:int (num1:3350 num2:3350)

 

2.特征描述符提取

Ptr<DescriptorExtractor> DescriptorExtractor::create(const string& descriptorExtractorType)

//  Ptr
DescriptorExtractor::create(const string& descriptorExtractorType) // "SIFT" – SIFT // "SURF" – SURF // "ORB" – ORB // "BRIEF" – BriefDescriptorExtractor

 

3.描述符匹配

Ptr<DescriptorMatcher> descriptorMatcher = DescriptorMatcher::create(const string& descriptorMatcherType)

// 	descriptorMatcherType – Descriptor matcher type. //	Now the following matcher types are supported: // 		BruteForce (it uses L2 ) // 		BruteForce-L1 // 		BruteForce-Hamming // 		BruteForce-Hamming(2) // 		FlannBased 	Ptr
descriptorMatcher = DescriptorMatcher::create( "BruteForce" );

 

4.class BOWTrainer

class BOWKmeansTrainer::public BOWTrainer:Kmeans训练

BOWKMeansTrainer ::BOWKmeansTrainer(int clusterCount, const TermCriteria& termcrit=TermCriteria(), int attempts=3, int flags=KMEANS_PP_CENTERS)

parameter same as 

 

实现:                                                                                                                    

1.画特征点。

2.特征点聚类,每一种颜色代表一个类别。

 

#include "opencv2/highgui/highgui.hpp"#include "opencv2/calib3d/calib3d.hpp"#include "opencv2/imgproc/imgproc.hpp"#include "opencv2/features2d/features2d.hpp"#include "opencv2/nonfree/nonfree.hpp"#include 
using namespace cv;using namespace std;#define ClusterNum 10void DrawAndMatchKeypoints(const Mat& Img1,const Mat& Img2,const vector
& Keypoints1, const vector
& Keypoints2,const Mat& Descriptors1,const Mat& Descriptors2){ Mat keyP1,keyP2; drawKeypoints(Img1,Keypoints1,keyP1,Scalar::all(-1),0); drawKeypoints(Img2,Keypoints2,keyP2,Scalar::all(-1),0); putText(keyP1, "drawKeyPoints", cvPoint(10,30), FONT_HERSHEY_SIMPLEX, 1 ,Scalar :: all(-1)); putText(keyP2, "drawKeyPoints", cvPoint(10,30), FONT_HERSHEY_SIMPLEX, 1 ,Scalar :: all(-1)); imshow("img1 keyPoints",keyP1); imshow("img2 keyPoints",keyP2); Ptr
descriptorMatcher = DescriptorMatcher::create( "BruteForce" ); vector
matches; descriptorMatcher->match( Descriptors1, Descriptors2, matches ); Mat show; drawMatches(Img1,Keypoints1,Img2,Keypoints2,matches,show,Scalar::all(-1),CV_RGB(255,255,255),Mat(),4); putText(show, "drawMatchKeyPoints", cvPoint(10,30), FONT_HERSHEY_SIMPLEX, 1 ,Scalar :: all(-1)); imshow("match",show);}//测试OpenCV:class BOWTrainervoid BOWKeams(const Mat& img, const vector
& Keypoints, const Mat& Descriptors, Mat& centers){ //BOW的kmeans算法聚类; BOWKMeansTrainer bowK(ClusterNum, cvTermCriteria (CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 10, 0.1),3,2); centers = bowK.cluster(Descriptors); cout<
<<"< cluster num: "<
<<" >"<
descriptorMatcher = DescriptorMatcher::create( "BruteForce" ); vector
matches; descriptorMatcher->match(Descriptors,centers,matches);//const Mat& queryDescriptors, const Mat& trainDescriptors第一个参数是待分类节点,第二个参数是聚类中心; Mat demoCluster; img.copyTo(demoCluster); //为每一类keyPoint定义一种颜色 Scalar color[]={CV_RGB(255,255,255), CV_RGB(255,0,0),CV_RGB(0,255,0),CV_RGB(0,0,255), CV_RGB(255,255,0),CV_RGB(255,0,255),CV_RGB(0,255,255), CV_RGB(123,123,0),CV_RGB(0,123,123),CV_RGB(123,0,123)}; for (vector
::iterator iter=matches.begin();iter!=matches.end();iter++) { cout<<"< descriptorsIdx:"<
queryIdx<<" centersIdx:"<
trainIdx <<" distincs:"<
distance<<" >"<
queryIdx].pt; circle(demoCluster,center,2,color[iter->trainIdx],-1); } putText(demoCluster, "KeyPoints Clustering: 一种颜色代表一种类型", cvPoint(10,30), FONT_HERSHEY_SIMPLEX, 1 ,Scalar :: all(-1)); imshow("KeyPoints Clusrtering",demoCluster); }int main(){ cv::initModule_nonfree();//使用SIFT/SURF create之前,必须先initModule_
(); cout << "< Creating detector, descriptor extractor and descriptor matcher ..."; Ptr
detector = FeatureDetector::create( "SIFT" ); Ptr
descriptorExtractor = DescriptorExtractor::create( "SIFT" ); Ptr
descriptorMatcher = DescriptorMatcher::create( "BruteForce" ); cout << ">" << endl; if( detector.empty() || descriptorExtractor.empty() ) { cout << "Can not create detector or descriptor exstractor or descriptor matcher of given types" << endl; return -1; } cout << endl << "< Reading images..." << endl; Mat img1 = imread("D:/demo0.jpg"); Mat img2 = imread("D:/demo1.jpg"); cout<
<<">"<
keypoints1,keypoints2; detector->detect( img1, keypoints1 ); detector->detect( img2, keypoints2 ); cout <<"img1:"<< keypoints1.size() << " points img2:" <
<< " points" << endl << ">" << endl; //compute descriptors for keypoints; cout << "< Computing descriptors for keypoints from images..." << endl; Mat descriptors1,descriptors2; descriptorExtractor->compute( img1, keypoints1, descriptors1 ); descriptorExtractor->compute( img2, keypoints2, descriptors2 ); cout<
<<"< Descriptoers Size: "<
<<" >"<
" << endl; //Draw And Match img1,img2 keypoints //匹配的过程是对特征点的descriptors进行match; DrawAndMatchKeypoints(img1,img2,keypoints1,keypoints2,descriptors1,descriptors2); Mat center; //对img1提取特征点,并聚类 //测试OpenCV:class BOWTrainer BOWKeams(img1,keypoints1,descriptors1,center); waitKey();}

 

 

通过Qt实现DrawKeypoints:

void Qt_test1::on_DrawKeypoints_clicked(){	//initModule_nonfree();	Ptr
detector = FeatureDetector::create( "FAST" ); vector
keypoints; detector->detect( src, keypoints ); Mat DrawKeyP; drawKeypoints(src,keypoints,DrawKeyP,Scalar::all(-1),0); putText(DrawKeyP, "drawKeyPoints", cvPoint(10,30), FONT_HERSHEY_SIMPLEX, 0.5 ,Scalar :: all(255)); cvtColor(DrawKeyP, image, CV_RGB2RGBA); QImage img = QImage((const unsigned char*)(image.data), image.cols, image.rows, QImage::Format_RGB32); QLabel *label = new QLabel(this); label->move(50, 50);//图像在窗口中所处的位置; label->setPixmap(QPixmap::fromImage(img)); label->resize(label->pixmap()->size()); label->show();}

由于initModule_nonfree()总是出错,无法对SIFT与SURF特征点提取,

而且无法实现聚类因为运行/BOW的kmeans算法聚类:BOWKMeansTrainer bowK(ClusterNum, cvTermCriteria (CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 10, 0.1),3,2);总是出错,不知道咋解决~~~~~(>_<)~~~~ 需要继续学习

转载于:https://www.cnblogs.com/zengcv/p/5582965.html

你可能感兴趣的文章
html css 伪样式
查看>>
超级账本Fabric区块链用弹珠游戏Marbles 部署
查看>>
网易星球增加网易云音乐任务,将用户流量变现是正确的做法
查看>>
最强面试题汇总:BAT最新前端题出炉,碰到技术HR这些题必考!
查看>>
自定义View之IndexView进度条(二)
查看>>
牢补基础,话说Service那点事
查看>>
云计算固然好 但挑战依然存在
查看>>
云服务器 ECS 最佳实践:借助于实例 RAM 角色访问其它云产品 API
查看>>
《软件需求工程(第2版)》一2.3 需求工程在软件开发中的地位
查看>>
海思全力部署智能城市NB-IoT应用
查看>>
微软将于8月15日发布新版Authenticator应用 统一用户和企业入口
查看>>
DELL推出Openrating System 10,进击的开放网络
查看>>
Maven多模块项目
查看>>
Oracle、PostgreSQL与Mysql数据写入性能对比
查看>>
整理Java基础知识--选择与判断
查看>>
Linux查看程序端口占用情况
查看>>
jar包冲突案例分析.md
查看>>
控制圈复杂度的9种重构技术总结
查看>>
当软件项目全部能靠自己搞定了,也能接几万元的软件项目时,未必适合创业...
查看>>
数据分析--数字找朋友
查看>>