sift.c File Reference


Detailed Description

For internal use only.

Author:
Andrea Vedaldi

Definition in file sift.c.

#include "sift.h"
#include "imop.h"
#include "mathop.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdio.h>

Go to the source code of this file.


Defines

#define VL_SIFT_BILINEAR_ORIENTATIONS   1
 Use bilinear interpolation to compute orientations.
#define EXPN_SZ   256
#define EXPN_MAX   25.0

Functions

VL_INLINE double fast_expn (double x)
 Fast $exp(-x)$ approximation.
VL_INLINE void fast_expn_init ()
 Initialize tables for fast_expn.
static void copy_and_upsample_rows (vl_sift_pix *dst, vl_sift_pix const *src, int width, int height)
 Copy image, upsample rows and take transpose.
static void copy_and_downsample (vl_sift_pix *dst, vl_sift_pix const *src, int width, int height, int d)
 Copy and downsample an image.
VL_EXPORT VlSiftFiltvl_sift_new (int width, int height, int noctaves, int nlevels, int o_min)
 Create a new SIFT filter.
VL_EXPORT void vl_sift_delete (VlSiftFilt *f)
 Delete SIFT filter.
VL_EXPORT int vl_sift_process_first_octave (VlSiftFilt *f, vl_sift_pix const *im)
 Start processing a new image.
VL_EXPORT int vl_sift_process_next_octave (VlSiftFilt *f)
 Process next octave.
VL_EXPORT void vl_sift_detect (VlSiftFilt *f)
 Detect keypoints.
static void update_gradient (VlSiftFilt *f)
 Update gradients to current GSS octave.
VL_EXPORT int vl_sift_calc_keypoint_orientations (VlSiftFilt *f, double angles[4], VlSiftKeypoint const *k)
 Calculate the keypoint orientation(s).
VL_INLINE vl_sift_pix normalize_histogram (vl_sift_pix *begin, vl_sift_pix *end)
 Normalizes in norm L_2 a descriptor.
VL_EXPORT void vl_sift_calc_raw_descriptor (VlSiftFilt const *f, vl_sift_pix const *grad, vl_sift_pix *descr, int width, int height, double x, double y, double sigma, double angle0)
 Run the SIFT descriptor on raw data.
VL_EXPORT void vl_sift_calc_keypoint_descriptor (VlSiftFilt *f, vl_sift_pix *descr, VlSiftKeypoint const *k, double angle0)
 Compute the descriptor of a keypoint.
VL_EXPORT void vl_sift_keypoint_init (VlSiftFilt const *f, VlSiftKeypoint *k, double x, double y, double sigma)
 Initialize a keypoint from its position and scale.

Variables

double expn_tab [EXPN_SZ+1]

Define Documentation

#define EXPN_MAX   25.0

fast_expn table max

For internal use only.

Definition at line 354 of file sift.c.

Referenced by fast_expn(), and fast_expn_init().

#define EXPN_SZ   256

fast_expn table size

For internal use only.

Definition at line 353 of file sift.c.

Referenced by fast_expn(), and fast_expn_init().

#define VL_SIFT_BILINEAR_ORIENTATIONS   1

For internal use only.

Definition at line 351 of file sift.c.


Function Documentation

static void copy_and_downsample ( vl_sift_pix dst,
vl_sift_pix const *  src,
int  width,
int  height,
int  d 
) [static]

For internal use only.

Parameters:
dst output imgae buffer.
src input image buffer.
width input image width.
height input image height.
d octaves (non negative).
The function downsamples the image d times, reducing it to 1/2^d of its original size. The parameters width and height are the size of the input image. The destination image dst is assumed to be floor(width/2^d) pixels wide and floor(height/2^d) pixels high.

Definition at line 456 of file sift.c.

Referenced by vl_sift_process_first_octave(), and vl_sift_process_next_octave().

static void copy_and_upsample_rows ( vl_sift_pix dst,
vl_sift_pix const *  src,
int  width,
int  height 
) [static]

For internal use only.

Parameters:
dst output image buffer.
src input image buffer.
width input image width.
height input image height.
The output image has dimensions height by 2 width (so the destination buffer must be at least as big as two times the input buffer).

Upsampling is performed by linear interpolation.

Definition at line 417 of file sift.c.

Referenced by vl_sift_process_first_octave().

VL_INLINE double fast_expn ( double  x  ) 

For internal use only.

Parameters:
x argument.
The argument must be in the range [0, EXPN_MAX] .

Returns:
approximation of $exp(-x)$.

Definition at line 371 of file sift.c.

References EXPN_MAX, EXPN_SZ, expn_tab, and vl_floor_d().

Referenced by vl_sift_calc_keypoint_descriptor(), vl_sift_calc_keypoint_orientations(), and vl_sift_calc_raw_descriptor().

VL_INLINE void fast_expn_init (  ) 

For internal use only.

Definition at line 391 of file sift.c.

References EXPN_MAX, EXPN_SZ, and expn_tab.

Referenced by vl_sift_new().

VL_INLINE vl_sift_pix normalize_histogram ( vl_sift_pix begin,
vl_sift_pix end 
)

For internal use only.

Parameters:
begin begin of histogram.
end end of histogram.

Definition at line 1329 of file sift.c.

References VL_EPSILON_F, and vl_fast_sqrt_f().

static void update_gradient ( VlSiftFilt f  )  [static]

For internal use only.

Parameters:
f SIFT filter.
The function makes sure that the gradient buffer is up-to-date with the current GSS data.

Remarks:
The minimum octave size is 2x2xS.

Definition at line 1065 of file sift.c.

References _VlSiftFilt::grad, _VlSiftFilt::grad_o, _VlSiftFilt::o_cur, _VlSiftFilt::s_max, _VlSiftFilt::s_min, vl_sift_get_octave(), vl_sift_get_octave_height(), and vl_sift_get_octave_width().

Referenced by vl_sift_calc_keypoint_descriptor(), and vl_sift_calc_keypoint_orientations().

VL_EXPORT void vl_sift_calc_keypoint_descriptor ( VlSiftFilt f,
vl_sift_pix descr,
VlSiftKeypoint const *  k,
double  angle0 
)

Parameters:
f SIFT filter.
descr SIFT descriptor (output)
k keypoint.
angle0 keypoint direction.
The function computes the SIFT descriptor of the keypoint k of orientation angle0. The function fills the buffer descr which must be large enough to hold the descriptor.

The function assumes that the keypoint is on the current octave. If not, it does not do anything.

Definition at line 1543 of file sift.c.

References fast_expn(), _VlSiftFilt::grad, normalize_histogram(), _VlSiftKeypoint::o, _VlSiftFilt::o_cur, _VlSiftFilt::s_max, _VlSiftFilt::s_min, update_gradient(), vl_abs_f(), vl_floor_f(), VL_MAX, VL_MIN, vl_mod_2pi_f(), and VL_PI.

VL_EXPORT int vl_sift_calc_keypoint_orientations ( VlSiftFilt f,
double  angles[4],
VlSiftKeypoint const *  k 
)

Parameters:
f SIFT filter.
angles orientations (output).
k keypoint.
The function computes the orientation(s) of the keypoint k. The function returns the number of orientations found (up to four). The orientations themselves are written to the vector angles.

Remarks:
The function requires the keypoint octave k->o to be equal to the filter current octave vl_sift_get_octave. If this is not the case, the function returns zero orientations.

The function requires the keypoint scale level k->s to be in the range s_min+1 and s_max-2 (where usually s_min=0 and s_max=S+2). If this is not the case, the function returns zero orientations.

Returns:
number of orientations found.

Definition at line 1177 of file sift.c.

References fast_expn(), _VlSiftKeypoint::o, _VlSiftFilt::o_cur, _VlSiftFilt::s_max, _VlSiftFilt::s_min, update_gradient(), vl_floor_d(), VL_MAX, VL_MIN, and VL_PI.

VL_EXPORT void vl_sift_calc_raw_descriptor ( VlSiftFilt const *  f,
vl_sift_pix const *  grad,
vl_sift_pix descr,
int  width,
int  height,
double  x,
double  y,
double  sigma,
double  angle0 
)

Parameters:
f SIFT filter.
grad image gradients.
descr SIFT descriptor (output).
width image width.
height image height.
x keypoint x coordinate.
y keypoint y coordinate.
sigma keypoint scale.
angle0 keypoint orientation.
The function runs the SIFT descriptor on raw data. Here image is a 2 x width x height array (by convention, the memory layout is a s such the first index is the fastest varying one). The first width x height layer of the array contains the gradient magnitude and the second the gradient angle (in radians, between 0 and $ 2\pi $). x, y and sigma give the keypoint center and scale respectively.

In order to be equivalent to a standard SIFT descriptor the image gradient must be computed at a smoothing level equal to the scale of the keypoint. In practice, the actual SIFT algorithm makes the following additional approximation, which influence the result:

  • Scale is discretized in S levels.
  • The image is downsampled once for each octave (if you do this, the parameters x, y and sigma must be scaled too).

Definition at line 1379 of file sift.c.

References fast_expn(), normalize_histogram(), vl_abs_f(), VL_EPSILON_D, vl_floor_f(), VL_MAX, VL_MIN, vl_mod_2pi_f(), and VL_PI.

VL_EXPORT void vl_sift_delete ( VlSiftFilt f  ) 

Parameters:
f SIFT filter to delete.
The function frees the resources allocated by vl_sift_new().

Definition at line 565 of file sift.c.

References vl_free().

VL_EXPORT void vl_sift_detect ( VlSiftFilt f  ) 

The function detect keypoints in the current octave filling the internal keypoint buffer. Keypoints can be retrieved by vl_sift_get_keypoints().

Parameters:
f SIFT filter.

Index GSS

For internal use only.

Index matrix A

Definition at line 771 of file sift.c.

References _VlSiftFilt::keys, _VlSiftFilt::keys_res, _VlSiftFilt::nkeys, _VlSiftFilt::o_cur, _VlSiftFilt::S, _VlSiftFilt::sigma0, vl_abs_d(), vl_malloc(), vl_realloc(), and vl_sift_get_octave().

VL_EXPORT void vl_sift_keypoint_init ( VlSiftFilt const *  f,
VlSiftKeypoint k,
double  x,
double  y,
double  sigma 
)

Parameters:
f SIFT filter.
k SIFT keypoint (output).
x x coordinate of the center.
y y coordinate of the center.
sigma scale.
The function initializes the structure k from the location x and y and scale sigma of the keypoint.

Definition at line 1732 of file sift.c.

References _VlSiftFilt::O, _VlSiftFilt::o_min, _VlSiftFilt::S, _VlSiftFilt::s_max, _VlSiftFilt::s_min, _VlSiftKeypoint::sigma, _VlSiftFilt::sigma0, vl_floor_d(), VL_MAX, and VL_MIN.

VL_EXPORT VlSiftFilt* vl_sift_new ( int  width,
int  height,
int  noctaves,
int  nlevels,
int  o_min 
)

Parameters:
width image width.
height image height.
noctaves number of octaves.
nlevels number of levels per octave.
o_min first octave index.
The function allocates and returns a new SIFT filter for the specified image and scale space geometry.

Setting O to a negative value sets the number of octaves to the maximum possible value depending on the size of the image.

Returns:
the new SIFT filter.
See also:
vl_sift_delete().

Definition at line 493 of file sift.c.

References fast_expn_init(), _VlSiftFilt::s_max, _VlSiftFilt::s_min, _VlSiftFilt::sigma0, _VlSiftFilt::sigmak, vl_malloc(), VL_MAX, VL_MIN, and VL_SHIFT_LEFT.

VL_EXPORT int vl_sift_process_first_octave ( VlSiftFilt f,
vl_sift_pix const *  im 
)

Parameters:
f SIFT filter.
im image data.
The function starts processing a new image by computing its Gaussian scale space at the lower octave. It also empties the internal keypoint buffer.

Returns:
error code. The function returns VL_ERR_EOF if there are no more octaves to process.
See also:
vl_sift_process_next_octave().

Definition at line 595 of file sift.c.

References copy_and_downsample(), copy_and_upsample_rows(), _VlSiftFilt::height, _VlSiftFilt::nkeys, _VlSiftFilt::O, _VlSiftFilt::o_cur, VL_ERR_EOF, VL_ERR_OK, vl_imsmooth_f(), VL_SHIFT_LEFT, vl_sift_get_octave(), and _VlSiftFilt::width.

VL_EXPORT int vl_sift_process_next_octave ( VlSiftFilt f  ) 

Parameters:
f SIFT filter.
The function computes the next octave of the Gaussian scale space. Notice that this clears the record of any feature detected in the previous octave.

Returns:
error code. The function returns the error VL_ERR_EOF when there are no more octaves to process.
See also:
vl_sift_process_first_octave().

Definition at line 701 of file sift.c.

References copy_and_downsample(), _VlSiftFilt::height, _VlSiftFilt::o_cur, VL_ERR_EOF, VL_ERR_OK, vl_imsmooth_f(), VL_MIN, VL_SHIFT_LEFT, vl_sift_get_octave(), vl_sift_get_octave_height(), vl_sift_get_octave_width(), and _VlSiftFilt::width.


Variable Documentation

double expn_tab[EXPN_SZ+1]

fast_expn table

For internal use only.

Definition at line 355 of file sift.c.

Referenced by fast_expn(), and fast_expn_init().