[Opencv] Sift principle and source code analysis: Assignment of directions

Source: Internet
Author: User

Index of Articles in the sift principle and source code analysis series: Middleware. To achieve image rotation immutability, a value must be assigned to the feature point direction based on the Local Image structure of the detected key points. That is, the alcorientationhist () Statement seen in the findscalespaceextrema () function:
// Calculate the gradient histogram float omax = calcorientationhist (gauss_pyr [O * (noctavelayers + 3) + layer], point (C1, R1), cvround (sift_ori_radius * scl_octv ), sift_ori_sig_fctr * scl_octv, Hist, N );

The gradient histogram of the image is used to find the stable direction of the local structure of the key point. Gradient Direction and amplitude

In the previous article, after accurately locating the key point, we also found the scale value σ of the feature point. According to this scale value, we obtained the Gaussian image closest to this scale value:

Finite Difference is used to calculate the width and amplitude of the image gradient in a region centered on a key point and with a radius of 3 × 1. 5 σ. The formula is as follows:

Gradient Histogram

After the Gaussian image gradient calculation is completed in the neighbor of the key point, the histogram is used to calculate the gradient direction and amplitude corresponding to the pixel in the neighbor.

For more information about histograms, see digital image histogram. It can be seen as the probability representation of discrete points. The core of the direction histogram is to count the contribution of the image pixels in a certain area to the generation of the direction of the key points.

The horizontal axis of the gradient direction histogram is the gradient direction angle, and the vertical axis is the gradient amplitude accumulation value corresponding to the shaving direction angle. The histogram of the gradient direction is 0 ° ~ The range of 360 ° is divided into 36 columns, each 10 ° is a column. Is an example of obtaining a gradient from a Gaussian image and then obtaining a gradient histogram from a gradient.

When calculating the histogram, each sampling point added to the histogram is weighted using the circular Gaussian function, that isGaussian smoothing. This is mainly because the sift algorithm only considers the scale and rotation without deformation, and does not consider the affinicity. Through Gaussian smoothing, the gradient amplitude near the key points has a large weight, which partially makes up for the instability of the feature points that do not consider non-deformation.

Generally, discrete gradient histograms must be interpolated to obtain more precise direction and angle values. (This is the same as interpolation in key point search and location ).

Key Aspect

The histogram peak value represents the main direction of the image gradient in the neighborhood of the key point, that is,Main Direction of key points. In the histogram of the gradient direction, when there is another peak value equivalent to the main peak value of 80% energy, this direction is consideredSecondary direction of key points. Therefore, a key point may be detected in multiple directions, which can enhance the matching robustness. Lowe's thesis points out that there are about 15% key points with multiple directions, but the stability of these point pairs is critical.

After obtaining the main direction of the image's key points, each key point has three pieces of information (X, Y, σ, θ): Position, scale, and direction. Therefore, we can determine a sift feature area. Generally, an arrow circle or an arrow is used to represent the three values of the sift area. The center represents the feature point position, the RADIUS represents the scale of the Key Point (r = 2.5 σ), and the arrow represents the main direction. Key points with multiple directions can be copied into multiple copies, and then the direction values are assigned to the copied key points respectively. For example:

Source code
// Computes a gradient orientation histogram at a specified pixel // calculate the gradient direction histogram static float calcorientationhist (const mat & IMG, point PT, int radius, float Sigma, float * Hist, int N) {// Len: 2R + 1 is the number of rounded (square) pixels with the R radius int I, J, K, len = (radius * 2 + 1) * (radius * 2 + 1); float expf_scale =-1.f/ (2.f * Sigma * sigma ); autobuffer <float> Buf (LEN * 4 + N + 4); float * x = Buf, * Y = x + Len, * mag = x, * Ori = Y + Len, * w = Ori + Len; float * temphist = W + Len + 2; for (I = 0; I <n; I ++) temphist [I] = 0.f; // pixel range of image gradient histogram statistics for (I =-radius, K = 0; I <= radius; I ++) {int y = pt. Y + I; If (Y <= 0 | Y> = IMG. rows-1) continue; For (j =-radius; j <= radius; j ++) {int x = pt. X + J; If (x <= 0 | x> = IMG. cols-1) continue; float dx = (float) (IMG. at <short> (Y, x + 1)-IMG. at <short> (Y, x-1); float DY = (float) (IMG. at <short> (Y-1, x)-IMG. at <short> (Y + 1, x); X [k] = DX; y [k] = Dy; W [k] = (I * I + J * j) * expf_scale; k ++;} Len = K; // compute gradient values, orientations and the weights over the pixel neighborhood // calculate the gradient, amplitude angle, and amplitude exp (W, W, Len); fastatan2 (Y, X, Ori, Len, true ); magnitude (X, Y, Mag, Len); // width angle // calculate each bin of the histogram (k = 0; k <Len; k ++) {int bin = cvround (n/360.f) * Ori [k]); If (bin> = N) bin-= N; If (bin <0) bin + = N; temphist [Bin] + = W [k] * mag [k];} // smooth the histogram // Gaussian smoothing temphist [-1] = temphist [n-1]; temphist [-2] = temphist [N-2]; temphist [N] = temphist [0]; temphist [n + 1] = temphist [1]; for (I = 0; I <n; I ++) {hist [I] = (temphist [I-2] + temphist [I + 2]) * (1.f/ 16.f) + (temphist [I-1] + temphist [I + 1]) * (4.f/ 16.f) + temphist [I] * (6.f/ 16.f );} // obtain the float maxval = Hist [0]; for (I = 1; I <n; I ++) maxval = STD: max (maxval, hist [I]); Return maxval ;}

This step is relatively simple ~ See "sift principle and source code analysis".

(Reprinted please indicate the author and Source: http://blog.csdn.net/xiaowei_cqu is not allowed for commercial use)

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.