In the recognition, registration and other applications, the extraction of image features are a key link, the extraction feature is to find the key points of the image (such as corner points, edge points, etc.), and then use descriptors to describe these points, the final image can be expressed as a feature vector, eigenvector can be used in subsequent recognition.
This process in MATLAB,OPENCV has the corresponding functions to achieve, MATLAB encapsulation is good, simple, as long as the input image and parameters, call function can get features, and OPENCV is slightly more complex, we first through the OpenCV C + + Program to understand the process (information is easier to find), and then read the Opencv4android document to understand the API provided, and finally realize the key points and draw on Android.
First, OpenCV C + + Image registration program to understand
The image registration program consists of 1. Defines the data structure that stores the key points and the key points that depict the child. 2. Define the features to be extracted, and detect the key points for two images respectively. 3. Compute the feature descriptors for the key points. 4. Calculate the matching points and display them.
#include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/nonfree/ Features2d.hpp> #include <opencv2/legacy/legacy.hpp>using namespace Cv;int main () {Mat image1=imread (".. /b1.png "); Mat Image2=imread (".. /b2.png ");//detection surf feature point vector<keypoint> keypoints1,keypoints2; Surffeaturedetector Detector (+);d etector.detect (Image1, Keypoints1);d etector.detect (Image2, keypoints2);// Describe surf feature point Surfdescriptorextractor surfdesc; Mat Descriptros1,descriptros2;surfdesc.compute (IMAGE1,KEYPOINTS1,DESCRIPTROS1); Surfdesc.compute (Image2, KEYPOINTS2,DESCRIPTROS2);//Calculate matching points bruteforcematcher<l2<float>>matcher;vector<dmatch> matches; Matcher.match (descriptros1,descriptros2,matches); Std::nth_element (Matches.begin (), Matches.begin () +24, Matches.end ()); Matches.erase (Matches.begin () +25,matches.end ());//Draw matching graph Mat imagematches;drawmatches (Image1, Keypoints1,image2,keypoints2,matches,imagematches,scalar (255,0,0)); Namedwindow ("Image2"); Imshow ("Image2", IMAGE2); Waitkey (); return 0;}
second, convert to OpenCV Java
First understand the OPENCV Java feature Extraction API and the corresponding data structure. The API for feature extraction is present in the package org.opencv.features2d, which mainly contains the following classes
1.Descriptorextractor
Abstract class that calculates feature descriptors for feature points.
The main use of two methods:
1.1 Create
Create a feature descriptor
Usage:p ublic 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
Extracting feature descriptors
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– the input image.
keypoints– input key points, obtained by Featuredetector.
descriptors– computed feature descriptors
Example:descriptor.compute (Mrgba, KeyPoint, mask);
2.Featuredetector
Class for extracting feature points of two-dimensional images
It's mainly two 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– the input image.
Key points detected by keypoints–
mask– template that specifies the location to take the key point degree. It must is a 8-bit integer matrix with Non-zero values in the region of interest.
3. KeyPoint
Data structure of key points using detect detection
Include: some constructors and corresponding fields, constructors are not introduced, the corresponding fields are described.
Angle
public float angle
Calculates the direction of the feature point.
class_id
public int class_id
The object's label, which can be used to classify which class the key points belong to.
Octave
public int Octave
The key points are extracted at which layer of the pyramid.
Pt
Public Point PT
Coordinates of key points
Response
Public float response
Response, which corresponds to the location of the key point:
Size
public float size
The radius of the adjacent area of the useful key.
4.Descriptormatcher
With the previous feature extraction, create and match are required
4.1 Create
Public Static Descriptormatcher Create (int matchertype)
Create a feature description sub-match with default parameters
Matchertype: Pattern matching method, algorithm
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 is descriptor from a query set.
For each feature descriptor in a given query collection, find the best match
Parameters:
Querydescriptors-Feature descriptor subquery set.
Traindescriptors-A subset of feature descriptions for the training (template). This set is not loaded into the object of the class.
matches– matches the number of points. The size of the matching points is less than the number of feature descriptors to query.
Knnmatch
public void Knnmatch (Mat querydescriptors,
Mat Traindescriptors,
Java.util.list<matofdmatch> matches,
int k,
Mat Mask,
Boolean Compactresult)
For each feature descriptor in the given query collection, look for K best Bets.
Radiusmatch
public void Radiusmatch (Mat querydescriptors,
Java.util.list<matofdmatch> matches,
float maxdistance)
For each query feature descriptor, look for a feature descriptor within a specific distance range.
5. Dmatch
Save data structures for matching features
float distance
The Euclidean distance between two eigenvectors indicates a higher degree of matching.
int Imgidx
Index of the training image (if there are multiple)
int Queryidx
This match corresponds to the feature Description sub-index of the query image
int Trainidx
This match corresponds to the feature Description sub-index of the Training (template) image
Third, complete C + + matching code
From 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 <iostream> #include <stdio.h> #include <stdlib.h >using namespace Cv;using namespace Std;int main () {initmodule_nonfree ();//Initialize module, use SIFT or surf when ptr< featuredetector> detector = featuredetector::create ("SIFT");//create SIFT feature detector ptr<descriptorextractor> Descriptor_extractor = Descriptorextractor::create ("SIFT");//create feature vector generator ptr<descriptormatcher> descriptor_ Matcher = Descriptormatcher::create ("Bruteforce");//Create feature match if (Detector.empty () | | | descriptor_extractor.empty ()) cout<< "fail to create detector!"; /read-in image mat IMG1 = Imread ("desk.jpg"); Mat Img2 = Imread ("desk_glue.jpg");//feature point detection double t = getTickCount ();//Current tick number vector<keypoint> keypoints1, Keypoints2;detector->detect (IMG1, keypoints1);//Detect SIFT feature points in IMG1, store keypoints1 in Detector->detect (Img2, KEYPOINTS2);cout<< "Image 1 feature point number:" <≪keypoints1.size () <<endl;cout<< "Image 2 feature point number:" <<keypoints2.size () <<endl;//calculates a feature description sub-matrix based on a feature point , the feature vector matrix Mat descriptors1,descriptors2;descriptor_extractor->compute (IMG1, Keypoints1, descriptors1);d Escriptor _extractor->compute (Img2, keypoints2, descriptors2); t = ((double) getTickCount ()-T)/gettickfrequency ();cout< < "Sift algorithm spents:" <<t<< "Seconds" <<endl;cout<< "image 1 feature description matrix size:" <<descriptors1.size () << " , Number of eigenvectors: "<<descriptors1.rows<<", Dimension: "<<descriptors1.cols<<endl;cout<<" Image 2 Feature description matrix size: "<<descriptors2.size () <<", eigenvector Number: "<<descriptors2.rows<<", Dimension: "<< descriptors2.cols<<endl;//draw the feature point Mat img_keypoints1,img_keypoints2;drawkeypoints (img1,keypoints1,img_ Keypoints1,scalar::all ( -1), 0);d rawkeypoints (Img2,keypoints2,img_keypoints2,scalar::all ( -1), 0);//imshow ("Src1", IMG_KEYPOINTS1);//imshow ("Src2", img_keypoints2);//feature matching vector<dmatch> matches;//matching result descriptor_matcher- >match (DescriptorS1, descriptors2, matches);//matching two images of the feature matrix cout<< "match number:" <<matches.size () <<endl;// Calculate the maximum and minimum distance between the matching results//distance is the Euclidean distance between two eigenvectors, indicating the difference of two features, the smaller the value, the closer the two feature points are to double max_dist = 0;double min_dist = 100;for (int i=0; i< Matches.size (); i++) {Double dist = matches[i].distance;if (Dist < min_dist) min_dist = dist;if (Dist > max_dist) max_dist = dist;} cout<< "Maximum distance:" <<max_dist<<endl;cout<< "minimum Distance:" <<min_dist<<endl;// Filter out a good match point vector<dmatch> goodmatches;for (int i=0; i<matches.size (); i++) {if (Matches[i].distance < 0.31 * Max_dist) {goodmatches.push_back (matches[i]);}} cout<< "Goodmatch Number:" <<goodmatches.size () <<endl;//Draw matching Results mat img_matches;//Red Connection is a matching feature point pair, Green is an unmatched feature point drawmatches (Img1,keypoints1,img2,keypoints2,goodmatches,img_matches,scalar::all ( -1)/*CV_RGB (255,0,0) */,cv_rgb (0,255,0), Mat (), 2), Imshow ("Matchsift", Img_matches), Waitkey (0); return 0;}Iv. Android Implementation
Pending update
Android Learning Nine---opencv4android org.opencv.feature2d