Android学习九---OpenCV4android org.opencv.feature2d

     不管是在识别,配准等应用中,提取图像的特征都是很关键的一环,提取特征是先找出图像的关键点(如角点,边缘点等),然后用描述子来描述这些点,最后整幅图像就可以表示成一个特征向量,特征向量就可以利用在后续识别中。

    这个流程在matlab,opencv中都有相应的函数来实现,matlab封装性好,比较简单,只要输入图像和参数,调用函数就能够得到特征,而opencv就稍微复杂点,我们先通过opencv的c++程序来了解这个过程(资料比较好找点),接着通过阅读opencv4android文档来了解提供的API,最后实现在Android找关键点并画图。

一、OpenCV c++图像配准程序来了解

图像配准程序包括1.定义存储关键点和关键点描绘子的数据结构。2.定义要提取的特征,分别对两幅图像进行关键点检测。3.计算关键点的特征描述子。4.计算匹配点数并显示。

#include 

#include 

#include 

#include 

using namespace cv;

int main()

{

Mat image1=imread("../b1.png");

Mat image2=imread("../b2.png");

// 检测surf特征点

vector keypoints1,keypoints2;

SurfFeatureDetector detector(400);

detector.detect(image1, keypoints1);

detector.detect(image2, keypoints2);

// 描述surf特征点

SurfDescriptorExtractor surfDesc;

Mat descriptros1,descriptros2;

surfDesc.compute(image1,keypoints1,descriptros1);

surfDesc.compute(image2,keypoints2,descriptros2);

// 计算匹配点数

BruteForceMatcher>matcher;

vector matches;

matcher.match(descriptros1,descriptros2,matches);

std::nth_element(matches.begin(),matches.begin()+24,matches.end());

matches.erase(matches.begin()+25,matches.end());

// 画出匹配图

Mat imageMatches;

drawMatches(image1,keypoints1,image2,keypoints2,matches,

imageMatches,Scalar(255,0,0));

namedWindow("image2");

imshow("image2",image2);

waitKey();

return 0;

}
 

 

二、转换到OpenCV java下

    首先了解OpenCV java 特征提取的API和相应的数据结构。有关特征提取的API是存在包Package org.opencv.features2d中,主要包含以下几个类

1.DescriptorExtractor

    计算特征点的特征描述子的抽象类。

    主要使用两个methods:

1.1 create

    创建特征描述子

    Usage :public static DescriptorExtractor create(int extractorType)

    ExtractorType:

· "SIFT" -- "SIFT"

· "SURF" -- "SURF"

· "BRIEF" -- "BriefDescriptorExtractor"

· "BRISK" -- "BRISK"

· "ORB" -- "ORB"

· "FREAK" -- "FREAK"

   Example:

DescriptorExtractor descriptor=DescriptorExtractor.create(DescriptorExtractor.SIFT);

1.2 compute

    提取特征描述子

Usage:public void compute(java.util.List<Mat> images,

java.util.List<MatOfKeyPoint> keypoints,

java.util.List<Mat> descriptors)

public void compute(Mat image,

MatOfKeyPoint keypoints,

Mat descriptors)

image – 输入的图像.

keypoints – 输入的关键点,由FeatureDetector得到。

descriptors – 计算出来的特征描述子

Example:descriptor.compute(mRgba, keypoint, mask);

2. FeatureDetector

    用来提取二维图像特征点的类

    主要是两个Methods

2.1 create

     Usage :public static FeatureDetector create(int detectorType)

DetectorType:

"FAST" -- "FastFeatureDetector"

"STAR" -- "StarFeatureDetector"

"SIFT" -- "SIFT" (nonfree module)

"SURF" -- "SURF" (nonfree module)

"ORB" -- "ORB"

"BRISK" -- "BRISK"

"MSER" -- "MSER"

"GFTT" -- "GoodFeaturesToTrackDetector"

"HARRIS" -- "GoodFeaturesToTrackDetector" with Harris detector enabled

"Dense" -- "DenseFeatureDetector"

"SimpleBlob" -- "SimpleBlobDetector"

    Example:FeatureDetector detector = FeatureDetector.create(FeatureDetector.MSER);

2.2 detect

   Usage:

public void detect(java.util.List<Mat> images,

java.util.List<MatOfKeyPoint> keypoints,

java.util.List<Mat> masks)

public void detect(Mat image,

MatOfKeyPoint keypoints,

Mat mask)

public void detect(Mat image,

MatOfKeyPoint keypoints,)

image –输入图像.

keypoints – 检测得到的关键点

mask – 模板,指定要取关键点度的位置. It must be a 8-bit integer matrix with non-zero values in the region of interest.

3. KeyPoint

    采用detect检测得到关键点的数据结构

    包括:一些构造函数和相应的域,构造函数不介绍,介绍对应的有哪些域。

angle

public float angle

计算特征点的方向.

class_id

public int class_id

物体的标签,可以用来分类关键点属于哪个类。

octave

public int octave

关键点使在金字塔的哪一层被提取的.

pt

public Point pt

关键点的坐标

response

public float response

响应,对应于哪个关键点的位置。.

size

public float size

有用关键点的邻接区域的半径。

4. DescriptorMatcher

    同前面的特征提取,需要create和match

4.1 create

   public static DescriptorMatcher create(int matcherType)

   采用默认参数创建一个特征描述子匹配

matcherType:模式匹配的方式,算法

static int BRUTEFORCE

static int BRUTEFORCE_HAMMING

static int BRUTEFORCE_HAMMINGLUT

static int BRUTEFORCE_L1

static int BRUTEFORCE_SL2

static int FLANNBASED

4.2 Match

   match

public void match(Mat queryDescriptors,

Mat trainDescriptors,

MatOfDMatch matches)

Finds the best match for each descriptor from a query set.

给定查询集合中的每个特征描述子,寻找 最佳匹配

Parameters:

queryDescriptors -特征描述子查询集.

trainDescriptors - 待训练(模板)的特征描述子集. 这个集没被加载到类的对象中.

matches –匹配点数. 匹配点数的大小小于待查询的特征描述子的个数。

knnMatch

public void knnMatch(Mat queryDescriptors,

Mat trainDescriptors,

java.util.List matches,

int k,

Mat mask,

boolean compactResult)

给定查询集合中的每个特征描述子,寻找 k个最佳匹配.

radiusMatch

public void radiusMatch(Mat queryDescriptors,

java.util.List matches,

float maxDistance)

对于每一个查询特征描述子, 在特定距离范围内寻找特征描述子.

5. DMatch

   保存匹配特征的数据结构

float distance

两个特征向量之间的欧氏距离,越小表明匹配度越高。

int imgIdx

训练图像的索引(若有多个)

int queryIdx

此匹配对应的查询图像的特征描述子索引

int trainIdx

此匹配对应的训练(模板)图像的特征描述子索引

三、完整的C++匹配代码

  来自http://blog.csdn.net/masibuaa/article/details/8998601

 

 

 

 

#include "opencv2/highgui/highgui.hpp"

#include "opencv2/imgproc/imgproc.hpp"

#include "opencv2/nonfree/nonfree.hpp"

#include "opencv2/nonfree/features2d.hpp"

#include 

#include 

#include 

using namespace cv;

using namespace std;

int main()

{

initModule_nonfree();//初始化模块,使用SIFT或SURF时用到

Ptr detector = FeatureDetector::create( "SIFT" );//创建SIFT特征检测器

Ptr descriptor_extractor = DescriptorExtractor::create( "SIFT" );//创建特征向量生成器

Ptr descriptor_matcher = DescriptorMatcher::create( "BruteForce" );//创建特征匹配器

if( detector.empty() || descriptor_extractor.empty() )

cout<<"fail to create detector!";

//读入图像

Mat img1 = imread("desk.jpg");

Mat img2 = imread("desk_glue.jpg");

//特征点检测

double t = getTickCount();//当前滴答数

vector keypoints1,keypoints2;

detector->detect( img1, keypoints1 );//检测img1中的SIFT特征点,存储到keypoints1中

detector->detect( img2, keypoints2 );

cout<<"图像1特征点个数:"<compute( img1, keypoints1, descriptors1 );

descriptor_extractor->compute( img2, keypoints2, descriptors2 );

t = ((double)getTickCount() - t)/getTickFrequency();

cout<<"SIFT算法用时:"< matches;//匹配结果

descriptor_matcher->match( descriptors1, descriptors2, matches );//匹配两个图像的特征矩阵

cout<<"Match个数:"< max_dist) max_dist = dist;

}

cout<<"最大距离:"< goodMatches;

for(int i=0; i 
  

四、Android实现

待更新

你可能感兴趣的:(Android)