1.問題描述:
SIFT,即尺度不變特征變換(Scale-invariant feature transform,SIFT),是用于圖像處理領域的一種描述,具有非常強的穩健性。那先來了解一下它的特點和作用:
SIFT算法的特點有:
SIFT特征是圖像的局部特征,其對旋轉、尺度縮放、亮度變化保持不變性,對視角變化、仿射變換、噪聲也保持一定程度的穩定性;
獨特性好,資訊量豐富,适用于在海量特征資料庫中進行快速、準确的比對;
多量性,即使少數的幾個物體也可以産生大量的SIFT特征向量;
高速性,經優化的SIFT比對算法甚至可以達到實時的要求;
可擴充性,可以很友善的與其他形式的特征向量進行聯合。
SIFT算法可以解決的問題:
目标的自身狀态、場景所處的環境和成像器材的成像特性等因素影響圖像配準/目辨別别跟蹤的性能。而SIFT算法在一定程度上可解決:
目标的旋轉、縮放、平移
圖像仿射/投影變換
光照影響
目标遮擋
雜物場景
噪聲
2、SIFT算法
按照參考部落格所寫的内容可以将SIFT算法分解為如下四步:
尺度空間極值檢測:搜尋所有尺度上的圖像位置。通過高斯差分函數來識别潛在的對于尺度和旋轉不變的興趣點。
關鍵點定位:在每個候選的位置上,通過一個拟合精細的模型來确定位置和尺度。關鍵點的選擇依據于它們的穩定程度。
方向确定:基于圖像局部的梯度方向,配置設定給每個關鍵點位置一個或多個方向。所有後面的對圖像資料的操作都相對于關鍵點的方向、尺度和位置進行變換,進而提供對于這些變換的不變性。
關鍵點描述:在每個關鍵點周圍的鄰域内,在標明的尺度上測量圖像局部的梯度。這些梯度被變換成一種表示,這種表示允許比較大的局部形狀的變形和光照變化。
2.部分程式:
% [matchLoc1 matchLoc2] = siftMatch(img1, img2)
%
% This function reads two images, finds their SIFT features, and
% displays lines connecting the matched keypoints. A match is accepted
% only if its distance is less than distRatio times the distance to the
% second closest match.
% It returns the matched points of both images, matchLoc1 = [x1,y1;x2,y2;...]
%
% Example: match('scene.pgm','book.pgm');
function [matchLoc1 matchLoc2] = siftMatch(img1, img2)
% load matchdata
% load img1data
% load img2data
%{,
% Find SIFT keypoints for each image
[des1, loc1] = sift(img1);
[des2, loc2] = sift(img2);
% save img1data des1 loc1
% save img2data des2 loc2
% For efficiency in Matlab, it is cheaper to compute dot products between
% unit vectors rather than Euclidean distances. Note that the ratio of
% angles (acos of dot products of unit vectors) is a close approximation
% to the ratio of Euclidean distances for small angles.
%
% distRatio: Only keep matches in which the ratio of vector angles from the
% nearest to second nearest neighbor is less than distRatio.
distRatio = 0.6;
% For each descriptor in the first image, select its match to second image.
des2t = des2'; % Precompute matrix transpose
matchTable = zeros(1,size(des1,1));
for i = 1 : size(des1,1)
dotprods = des1(i,:) * des2t; % Computes vector of dot products
[vals,indx] = sort(acos(dotprods)); % Take inverse cosine and sort results
% Check if nearest neighbor has angle less than distRatio times 2nd.
if (vals(1) < distRatio * vals(2))
matchTable(i) = indx(1);
else
matchTable(i) = 0;
end
end
% save matchdata matchTable
%}
% Create a new image showing the two images side by side.
img3 = appendimages(img1,img2);
% Show a figure with lines joining the accepted matches.
figure('Position', [100 100 size(img3,2) size(img3,1)]);
colormap('gray');
imagesc(img3);
hold on;
cols1 = size(img1,2);
for i = 1: size(des1,1)
if (matchTable(i) > 0)
line([loc1(i,2) loc2(matchTable(i),2)+cols1], ...
[loc1(i,1) loc2(matchTable(i),1)], 'Color', 'c');
end
end
hold off;
num = sum(matchTable > 0);
fprintf('Found %d matches.\n', num);
idx1 = find(matchTable);
idx2 = matchTable(idx1);
x1 = loc1(idx1,2);
x2 = loc2(idx2,2);
y1 = loc1(idx1,1);
y2 = loc2(idx2,1);
matchLoc1 = [x1,y1];
matchLoc2 = [x2,y2];