Getting started with opencv nine feature points detection and Image Matching

Source: Internet
Author: User
Tags scalar

 

Feature Points, also known as points of interest and key points, are highlighted and representative points in the image. Through these points, we can identify images, perform image registration, and perform 3D reconstruction. This article mainly introduces several functions in opencv to locate and represent key points.

I. Harris corner

A corner is the most basic key point in an image. It is composed of some knots of geometric structures in the image, many of which are the intersection points generated between lines. Harris is a classic type of corner points. Its basic principle is to calculate the average value of the change rate between each point and the surrounding points in an image.

(1)

(2)

I (x + u, Y + u) represents the gray scale value of the point (x, y) neighbor. The above formula can be changed into a covariance matrix to find the feature value (2). The specific mathematical principles are not described in this article.

The hairrs corner detection function of opencv is cornerhairrs (), but its output is a floating point value image. The higher the floating point value, the more likely it is a feature corner, we need to set a threshold for the image. We use a building image to display:

 1 int main() 2 { 3     Mat image=imread("../buliding.png"); 4     Mat gray; 5     cvtColor(image,gray,CV_BGR2GRAY); 6     Mat cornerStrength; 7     cornerHarris(gray,cornerStrength,3,3,0.01); 8     threshold(cornerStrength,cornerStrength,0.001,255,THRESH_BINARY); 9     return 0;10 }

First, we will explain the meaning of the cornerhairrs () function parameter:

The first two parameters are input and output, the input is a grayscale image, and the output is a floating point image. The third parameter specifies the neighbor of corner analysis, in fact, the size of the core window of the gradient image is calculated during the calculation of the 4th parameters. The 5th parameters are a coefficient in its principle formula (2.

From the results of the above example, we can see that many corner points are stuck together. We will add non-maximum suppression to further remove some corner points sticking together.

The principle of non-maximum suppression is that, in a window, if there are multiple corner points, the biggest corner point will be used, and all other corner points will be deleted, window Size here we use 3*3, the program through the expansion operation of the image to achieve the purpose of detecting the maximum value, because the expansion operation of the default parameter replaces the current grayscale value with the maximum value in the window. At the end of the program, a function is used to show the corner points in the image. This function is consistent with the function used to draw the corner points in Article 5th of this series.

1 int main () 2 {3 mat image = imread (".. /buliding.png "); 4 mat gray; 5 cvtcolor (image, gray, cv_bgr2gray); 6 mat cornerstrength; 7 cornerharris (Gray, cornerstrength, 3, 0.01); 8 double maxstrength; 9 double minstrength; 10 // find the maximum and minimum values of the image 11 minmaxloc (cornerstrength, & minstrength, & maxstrength); 12 mat dilated; 13 mat locamax; 14 // expand the image, the maximum partial values of the entire image are 15 dilate (cornerstrength, dilated, MAT (); 16 // compare is a limit. Comparison function, returns the two images with the same binary values as the corresponding points 17 compare (cornerstrength, dilated, locamax, cmp_eq); 18 mat cornermap; 19 double qualitylevel = 0.01; 20 double th = qualitylevel * maxstrength; // threshold value calculation 21 threshold (cornerstrength, cornermap, Th, 255, thresh_binary); 22 cornermap. convertize (cornermap, cv_8u); 23 // bitwise operations 24 bitwise_and (cornermap, locamax, cornermap); 25 drawcorneronimage (image, cornermap); 26 namedwindow ("result "); 27 imshow ("result ", Image); 28 waitkey (); 29 30 return 0; 31} 32 void drawcorneronimage (MAT & image, const mat & Binary) 33 {34 mat _ <uchar> :: const_iterator it = binary. begin <uchar> (); 35 mat _ <uchar>: const_iterator ITD = binary. end <uchar> (); 36 for (INT I = 0; it! = ITD; it ++, I ++) 37 {38 If (* It) 39 circle (image, point (I % image. cols, I/image. cols), 3, scalar (0,255, 0), 1); 40} 41}

Now we can get a lot better result than the default function.

Due to some disadvantages of cornerharris, opencv provides another similar function goodfeaturestotrack (), which uses the distance between corner points to prevent corner points from sticking together.

1 goodfeaturestotrack (image, corner, 2 500, // maximum detected Corner Points 3 0.01, // threshold coefficient 4 10); // minimum distance between Corner Points

The result is basically the same as the above.

Ii. Fast Feature Points

Harris features are highly complex in algorithms and cannot meet the requirements in terms of large and complex target recognition or matching applications. opencv provides a class of fastfeaturedetector for fast corner detection, in fact, fast does not mean fast, but features from accelerated segment test, but this algorithm is indeed more efficient. Let's take a look at the usage of this class.

Opencv provides a unified interface for corner detection. The detect method under the class is used to detect the corresponding corner points, and the output format is vector <keypoint>.

1 vector <keypoint> keypoints; 2 fastfeaturedetector fast (// defines the detection Class 3 40); // 40 is the detection threshold 4 fast. detect (image, keypoints); 5 drawkeypoints (image, keypoints, image, scalar (255, 0, 0), 6 drawmatchesflags: draw_over_outimg );

Drawkeypoints is a function provided by opencv to draw corner points on an image. Its Parameters allow us to mark feature points in different ways.

3. Scale-unchanged surf features

The surf feature is a scale-unchanged feature point similar to the sift feature. It has the advantage that it is more efficient than the sift feature and can meet real-time requirements in actual operations, there are many articles about the principle of surf on the Internet.

Similar to the method for fast feature points, surf can also be obtained through common interfaces. The surf feature class is surffeaturedetector, and the similar SIFT feature point detection class is siftfeaturedetector.

 1 #include <opencv2/core/core.hpp> 2 #include <opencv2/highgui/highgui.hpp> 3 #include <opencv2/nonfree/features2d.hpp> 4 using namespace cv; 5 int main() 6 { 7 Mat image=imread("../buliding.png"); 8 vector<KeyPoint> keypoints; 9  10 SurfFeatureDetector surf(2500.);11 surf.detect(image,keypoints);12 drawKeypoints(image,keypoints,image,Scalar(255,0,0),13 DrawMatchesFlags::DRAW_RICH_KEYPOINTS);14 namedWindow("result");15 imshow("result",image);16 waitKey();17  18 return 0;19 }

It is worth noting that the class definition of surffeaturedetector in opencv2.4 is moved to the header file nonfree/features2d. HPP.

So add the file to the header file, and add opencv_nonfree24xd.lib to the input familiar to the property table linker. Replace X with your current opencv version number.

The final display effect is as follows:

Iv. Description of surf features

In image registration, the description of a feature point is often not as simple as that of a location. Instead, a n-dimensional vector is used to describe a feature point, you can define the distance formula to compare the similarity between the descriptions.

Surfdescriptorextractor is a class that extracts the surf feature points and their descriptions.

The following is an example of tiled registration of wide scene images:

1 # include <opencv2/CORE/core. HPP> 2 # include <opencv2/highgui. HPP> 3 # include <opencv2/nonfree/features2d. HPP> 4 # include <opencv2/legacy. HPP> 5 using namespace CV; 6 int main () 7 {8 mat image1 = imread (".. /b1.png "); 9 mat image2 = imread (".. /b2.png "); 10 // detects surf feature points 11 vector <keypoint> keypoints1, keypoints2; 12 surffeaturedetector detector (400); 13 detector. detect (image1, keypoints1); 14 detector. detect (image2, keypoints2); 15 // describes the surf feature 16 surfdescriptorextractor surfdesc; 17 mat descriptros1, descriptros2; 18 surfdesc. compute (image1, keypoints1, descriptros1); 19 surfdesc. compute (image2, keypoints2, descriptros2); 20 // calculate the matching point 21 bruteforcematcher <L2 <float> matcher; 22 vector <dmatch> matches; 23 matcher. match (descriptros1, descriptros2, matches); 24 STD: nth_element (matches. begin (), matches. begin () + 24, matches. end (); 25 matches. erase (matches. begin () + 25, matches. end (); 26 // draw matching Figure 27 mat imagematches; 28 drawmatches (image1, keypoints1, image2, keypoints2, matches, 29 imagematches, scalar (255, 0, 0 )); 30 namedwindow ("image2"); 31 imshow ("image2", image2); 32 waitkey (); 33 34 return 0; 35}

In the program, we select 25 provisioning points. The final match is as follows:

Getting started with opencv nine feature points detection and Image Matching

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.