OPENCV Feature Point Detection

Source: Internet
Author: User
Tags scalar

First, Harris Corner point detection

Harris Corner Point:
Assuming a feature point, observing the average change in the intensity of each direction in the small window (such as 3x3) around the feature point, the direction of the maximum value of the average intensity change is obtained, and the intensity of the vertical direction of the direction is checked for a large change (satisfying the set threshold). This condition is met, which is the Harris corner point.
Mathematical expression:
I (x, y) represents the strength value of the point (X, y). For an offset vector (u,v), the offset vector can be seen as a direction. The change in its average strength is:

Sum overrides adjacent pixel points within a predefined window. The formula is unfolded by Taylor series and expressed in matrix form:

Obviously, the middle is a covariance matrix that represents the rate of change in intensity across all directions. The two eigenvalues of the matrix give the maximum average strength change and the average intensity change in the vertical direction. If, two eigenvalues are higher, higher than the given threshold value. This point is the corner point.

To avoid calculating eigenvalues (time-consuming), use the following formula instead:

The determinant of the Det matrix, traces of the trace matrix. It has been proved that the value of this equation is higher when two eigenvalues are high. The parameter k is generally set to 0.05~0.5.
Harris Corner detection, can be improved by maximal value inhibition, that is, to ensure that the strength of the corner is the local maximum value, the removal of the local maximum value of the corner point.

Code:
The OPENCV provides an interface for calculating Harris corner points.

cv::cornerHarris(image//检测图像,灰度图conerStrength,//harris角点图//预定义相邻像素点 aperture,//sobel滤波器孔径大小k//参数k);

Example: Detecting Harris Corner Point, non-maxima suppression. Displays the corner position in a circle.
Harris.h

#include <opencv2\highgui\highgui.hpp>#include <opencv2\imgproc\imgproc.hpp>#include <opencv2\core\core.hpp>classharrisdetector{Private://Corner-point Strength chartCv::mat conerstrength;//Threshold point Strength graphCv::mat Conerth;//local maxima imageCv::mat Locmax;//harris Parameters    intneighbourhood;//Domain size    intAperture//sobel (internal) filter aperture size    DoubleK//Parameters    //Maximum strength value    DoubleMaxstrength;//Threshold value    DoubleThreshold Public://InitializeHarrisdetector (): Neighbourhood (3), Aperture (3), K (0.1), Maxstrength (0.0), Threshold (0.01){}//harris Corner Point detection    voidDetectConstCv::mat &image) {//Detect Harris Value, return image each pixel is a Harris value, type 32-bit floating-point numberCv::cornerharris (image, Conerstrength, neighbourhood, Aperture, k);//non-maximal value suppression        DoubleMinstrength;//Unused parametersCv::minmaxloc (Conerstrength, &minstrength, &maxstrength);//Swell, the center point is replaced by the maximum value of the adjacent area (if the point is originally a local maximum value        //, the value of the point does not change)Cv::mat dilated; CV::d ilate (conerstrength, dilated, Cv::mat ());//Get local maxima image (the point where the image is marked as local maxima in the non-0 region)Cv::compare (Conerstrength, dilated, locmax,cv::cmp_eq); }//Get local maxima imageCv::mat Getcornermap (DoubleQualitylevel) {Cv::mat conermap;//Threshold valuethreshold = Qualitylevel*maxstrength;//Threshold value, Harris value greater than threshold, whiteCv::threshold (Conerstrength, Conerth, Threshold,255, cv_thresh_binary);//Convert image TypeConerth.convertto (Conermap, cv_8u);//With operation (non-maximal value suppression), Get corner imageCv::bitwise_and (Conermap, Locmax, Conermap);returnConermap; }voidGetcorners (STD:: vector<CV::P oint>&points,Doublequiality) {Cv::mat Conermap = Getcornermap (quiality);    Getcorners (points, conermap); }//Get the coordinates of the corner point    voidGetcorners (STD:: vector<CV::P oint>&points,Constcv::mat& Conermap) { for(inty =0; Y < conermap.rows; y++) {Constuchar* rowptr = conermap.ptr<uchar> (y); for(intx =0; x < Conermap.cols; X + +) {if(Rowptr[x])                {Points.push_back (CV::P oint (x, y)); }            }        }    }//Corner points are displayed in circles    voidDrawonimage (Cv::mat &image,STD:: vector<CV::P oint>&points, Cv::scalar color = cv::scalar (255,255,255),intRadius =3,intTickness =2)    {STD:: vector<CV::P oint>:: Const_iterator it = Points.begin (); while(It! = Points.end ())            {cv::circle (image, *it, radius, color, tickness);        it++; }    }};

Source. cpp

#include <opencv2\highgui\highgui.hpp>#include <opencv2\imgproc\imgproc.hpp>#include <opencv2\core\core.hpp>#include <iostream>#include "Harris.h"intMain () {Cv::mat image = Cv::imread ("D:/images/111.jpg",0);//Calculation program time consuming    DoubleDuration =0.0; Duration =static_cast<Double> (Cv::gettickcount ());//Detect corner pointsHarrisdetector h; H.detect (image);STD:: vector<CV::P oint>pts H.getcorners (pts,0.01);//Show corner points in circlesH.drawonimage (image, pts);//Calculation program time consumingduration=static_cast<Double> (Cv::gettickcount ())-duration; Duration/= cv::gettickfrequency ();STD::cout<<"Time:"<< Duration <<"Seconds"<<STD:: Endl; Cv::imshow ("Harris Corner Point Detection", image); Cv::waitkey (0);}


Ii. Characteristics of fast

Fast feature algorithm enables fast detection of feature points compared to Harris.
The fast feature algorithm defines a feature point as:
Assuming a candidate feature point, the point has a large difference (greater than the threshold) of a circle of pixels around it, and a large difference of pixels surrounds a circular arc with a circumference greater than 3/4. The point is the feature point. The circle radius is usually set to 3 (pixels)
This definition method, compared with the Harris corner point to calculate the neighborhood pixel gradient, greatly reduces the computational amount. It can be adopted when considering the speed of the program.

Consider, the center of the top, bottom, left, right, four points. If the center point is a feature point, at least three of these four points must be larger than the center spread. By this method, the detection points which are not feature points can be quickly removed, and the detection of the algorithm is accelerated.
Code:

std::vector<cv::KeyPoint> pts;//声明FSAT特征检测类对象,阈值设置40cv::FastFeatureDetector fast(40);//调用成员函数,将特征点的位置保存给pts    fast.detect(image, pts);

Example:

#include <opencv2\highgui\highgui.hpp>#include <opencv2\imgproc\imgproc.hpp>#include <opencv2\core\core.hpp>#include <opencv2\features2d\features2d.hpp>#include <iostream>intMain () {Cv::mat image = Cv::imread ("D:/images/111.jpg");//Calculation time    DoubleDuration =0; Duration =static_cast<Double> (Cv::gettickcount ());STD:: vector<cv::KeyPoint>pts//define fast feature detection class object with a threshold value ofCv::fastfeaturedetector Fast ( +);//Call member functionFast.detect (image, pts); /Call the feature point drawing function provided by OPENCV to display the feature point position in a circle CV::d rawkeypoints (Image, pts, image, Cv::scalar (255,255,255), CV::D rawmatchesflags::D raw_over_outimg);//Calculation timeDuration =static_cast<Double> (Cv::gettickcount ())-duration; Duration/= cv::gettickfrequency ();//Display resultsCv::imshow ("Fast feature", image);STD::cout<<"Time:"<< Duration <<"Seconds"<<STD:: Endl; Cv::waitkey (0);}


Summarize:
Although there is a certain deviation, but the approximate consumption time of the two algorithms is very different, in this example: Fast than Harris actually run nearly 10 times times faster.

OPENCV Feature Point Detection

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.