SIFT调用例程——VLFeat

Jeremy Lin

SIFT是一个很好的特征检测器,它能够从一张图片中检测出一组不受光照、视线等影响的特征点。同时它本身就是一个很好的特征描述子,它能够充分反映特征点附近局部图像的形状和纹理结构特性,具有较好的鲁棒性和独特性。

●提取特征点区域和特征描述子

检测特征点和获取特征描述子都是通过Matlab命令 vl_sift来实现的。

打开Matlab,载入测试图像:

I = vl_impattern('roofs1') ;
image(I) ;

SIFT调用例程——VLFeat_第1张图片

Input image.

vl_sift函数的输入是一个单精度的灰度图像,灰度值区间归一化到[0, 255]。因此图像 I 需要通过下面的函数转成相应的格式:

I = single(rgb2gray(I)) ;
然后,我们通过vl-sift( )函数计算SIFT frames(关键点)和描述子:

[f,d] = vl_sift(I) ;

上面vl_sift( )的输出矩阵 f 的每一列是一个frame,其中一列的前两个f(1:2)代表着frame的中心位置,f(3)代表scale,f(4)代表orientation。

我们通过如下命令可以随机选择50个features显示:

perm = randperm(size(f,2)) ;
sel = perm(1:50) ;
h1 = vl_plotframe(f(:,sel)) ;
h2 = vl_plotframe(f(:,sel)) ;
set(h1,'color','k','linewidth',3) ;
set(h2,'color','y','linewidth',2) ;

SIFT调用例程——VLFeat_第2张图片

Some of the detected SIFT frames.

We can also overlay the descriptors by

h3 = vl_plotsiftdescriptor(d(:,sel),f(:,sel)) ;
set(h3,'color','g') ;

SIFT调用例程——VLFeat_第3张图片

A test image for the peak threshold parameter.

●简单的匹配

SIFT 描述子一般用来寻找两幅图像的相同区域。在VLFeat库中使用vl_ubcmatch函数来实现简单的匹配。Let Ia and Ib be images of the same object or scene. We extract and match the descriptors by:

Ia = vl_impattern('roofs1') ;
Ia = single(rgb2gray(Ia)) ;
Ib = vl_impattern('roofs2');
Ib = single(rgb2gray(Ib)) ;
[fa, da] = vl_sift(Ia) ;
[fb, db] = vl_sift(Ib) ;
[matches, scores] = vl_ubcmatch(da, db) ;


[drop, perm] = sort(scores, 'descend') ;%%降序排列两个点的欧式距离,perm是索引,drop是降序结果,drop=scores(perm)
matches = matches(:, perm) ;            %%得到排序后??
scores  = scores(perm) ;                %%降序后的欧式距离

figure(1) ; clf ;
imagesc(cat(2, Ia, Ib)) ;
axis image off ;
vl_demo_print('sift_match_1', 1) ;

figure(2) ; clf ;
imagesc(cat(2, Ia, Ib)) ;

xa = fa(1,matches(1,:)) ;%%each fa is a feature frame and has the format [x;y;s;th],where x,y is the center of the frame,s is the scale 
xb = fb(1,matches(2,:)) + size(Ia,2) ;
ya = fa(2,matches(1,:)) ;
yb = fb(2,matches(2,:)) ;

hold on ;
h = line([xa ; xb], [ya ; yb]) ;
set(h,'linewidth', 1, 'color', 'b') ;

vl_plotframe(fa(:,matches(1,:))) ;
fb(1,:) = fb(1,:) + size(Ia,2) ;
vl_plotframe(fb(:,matches(2,:))) ;
axis image off ;

vl_demo_print('sift_match_2', 1) ;


SIFT调用例程——VLFeat_第4张图片

SIFT调用例程——VLFeat_第5张图片


Top: A pair of images of the same scene. 

Bottom: Matching of SIFT descriptors with vl_ubcmatch.

For each descriptor in da, vl_ubcmatch finds the closest descriptor in db (as measured by the L2 norm of the difference between them).

 The index of the original match and the closest descriptor is stored in each column of matches and the distance between the pair is stored in scores.

Matches also can be filtered for uniqueness by passing a third parameter to vl_ubcmatch which specifies a threshold. Here, the uniqueness of a pair is measured as the ratio of the distance between the best matching keypoint and the distance to the second best one (see vl_ubcmatch for further details).

Detector parameters

The SIFT detector is controlled mainly by two parameters: the peak threshold and the (non) edge threshold.

The peak threshold filters peaks of the DoG scale space that are too small (in absolute value). For instance, consider a test image of 2D Gaussian blobs:

I = double(rand(100,500) <= .005) ;

I = (ones(100,1) * linspace(0,1,500)) .* I ;

I(:,1) = 0 ; I(:,end) = 0 ;

I(1,:) = 0 ; I(end,:) = 0 ;

I = 2*pi*4^2 * vl_imsmooth(I,4) ;

I = single(255 * I) ;

A test image for the peak threshold parameter.

We run the detector with peak threshold peak_thresh by

f = vl_sift(I, 'PeakThresh', peak_thresh) ;

obtaining fewer features as peak_thresh is increased.

   

Detected frames for increasing peak threshold.
From top: peak_thresh = {0, 10, 20, 30}.

The edge threshold eliminates peaks of the DoG scale space whose curvature is too small (such peaks yield badly localized frames). For instance, consider the test image

I = zeros(100,500) ;

for i=[10 20 30 40 50 60 70 80 90]

I(50-round(i/3):50+round(i/3),i*5) = 1 ;

end

I = 2*pi*8^2 * vl_imsmooth(I,8) ;

I = single(255 * I) ;

A test image for the edge threshold parameter.

We run the detector with edge threshold edge_thresh by

f = vl_sift(I, 'edgethresh', edge_thresh) ;

obtaining more features as edge_thresh is increased:

   

Detected frames for increasing edge threshold.
From top: edge_thresh = {3.5, 5, 7.5, 10}

Custom frames

The MATLAB command vl_sift (and the command line utility) can bypass the detector and compute the descriptor on custom frames using the Frames option.

For instance, we can compute the descriptor of a SIFT frame centered at position (100,100), of scale 10 and orientation -pi/8 by

fc = [100;100;10;-pi/8] ;

[f,d] = vl_sift(I,'frames',fc) ;

Custom frame at with fixed orientation.

Multiple frames fc may be specified as well. In this case they are re-ordered by increasing scale. The Orientations option instructs the program to use the custom position and scale but to compute the keypoint orientations, as in

fc = [100;100;10;0] ;

[f,d] = vl_sift(I,'frames',fc,'orientations') ;

Custom frame with computed orientations.

Notice that, depending on the local appearance, a keypoint may have multiple orientations. Moreover, a keypoint computed on a constant image region (such as a one pixel region) has no orientations!

原文地址:http://www.vlfeat.org/overview/sift.html


本文地址:http://blog.csdn.net/linj_m/article/details/9833473

更多相关资源请关注 博客:LinJM-机器视觉  微博:林建民-机器视觉

你可能感兴趣的:(图像处理与分析,OpenCV学习笔记)