基本概念
尺度不變特征轉(zhuǎn)換(Scale-invariant feature transform,簡(jiǎn)稱SIFT) ,是一種用來偵測(cè)與描述影像中的局部性特征的算法,它在空間尺度中尋找極值點(diǎn),提取位置、尺度、旋轉(zhuǎn)不變量,生成特征描述子。 SIFT算法的實(shí)質(zhì)是在不同的尺度空間上查找關(guān)鍵點(diǎn)(特征點(diǎn)),并計(jì)算出關(guān)鍵點(diǎn)的方向。
計(jì)算步驟?
SIFT算法主要分以下步驟:
(1)尺度空間極值點(diǎn)檢測(cè):搜索所有尺度上的圖像位置,通過高斯微分函數(shù)來識(shí)別潛在的對(duì)于尺度和旋轉(zhuǎn)不變的興趣點(diǎn)。
(2)篩選出穩(wěn)定的關(guān)鍵點(diǎn):在每個(gè)候選的位置上,通過一個(gè)擬合精細(xì)的模型來確定位置和尺度。關(guān)鍵點(diǎn)的選擇依據(jù)于它們的穩(wěn)定程度。
(3)確定關(guān)鍵點(diǎn)方向:基于圖像局部的梯度方向,分配給每個(gè)關(guān)鍵點(diǎn)位置一個(gè)或多個(gè)方向。所有后面的對(duì)圖像數(shù)據(jù)的操作都相對(duì)于關(guān)鍵點(diǎn)的方向、尺度和位置進(jìn)行變換,從而提供對(duì)于這些變換的不變性。
(4)生成特征點(diǎn)描述子:在每個(gè)關(guān)鍵點(diǎn)周圍的鄰域內(nèi),在選定的尺度上測(cè)量圖像局部的梯度。這些梯度被變換成一種表示,這種表示允許比較大的局部形狀的變形和光照變化。
(5)特征點(diǎn)匹配文章來源:http://www.zghlxwxcb.cn/news/detail-805045.html
Matlab代碼
%該函數(shù)讀取圖像并返回其SIFT“關(guān)鍵點(diǎn)”
function [image, descriptors, locs] = sift(imageFile)
image = imread(imageFile); % 讀圖
[rows, cols] = size(image);
% 轉(zhuǎn)換為PGM格式,便于“關(guān)鍵點(diǎn)”可執(zhí)行文件的讀取
f = fopen('tmp.pgm', 'w');
if f == -1
error('Could not create file tmp.pgm.');
end
fprintf(f, 'P5\n%d\n%d\n255\n', cols, rows);
fwrite(f, image', 'uint8');
fclose(f);
%調(diào)用“關(guān)鍵點(diǎn)”可執(zhí)行文件
if isunix
command = '!./sift ';
else
command = '!siftWin32 ';
end
command = [command ' <tmp.pgm >tmp.key'];
eval(command);
g = fopen('tmp.key', 'r');
if g == -1
error('Could not open file tmp.key.');
end
[header, count] = fscanf(g, '%d %d', [1 2]);
if count ~= 2
error('Invalid keypoint file beginning.');
end
num = header(1);
len = header(2);
if len ~= 128
error('Keypoint descriptor length invalid (should be 128).');
end
% x1, y1; 起始點(diǎn)
% x2, y2; 終止點(diǎn)
function TransformLine(imsize, keypoint, x1, y1, x2, y2)
len = 6 * keypoint(3);
s = sin(keypoint(4));
c = cos(keypoint(4));
% 變換
r1 = keypoint(1) - len * (c * y1 + s * x1);
c1 = keypoint(2) + len * (- s * y1 + c * x1);
r2 = keypoint(1) - len * (c * y2 + s * x2);
c2 = keypoint(2) + len * (- s * y2 + c * x2);
line([c1 c2], [r1 r2], 'Color', 'c');
%% 該函數(shù)讀取兩張圖像,并找到它們的SIFT特征
function num = match(image1, image2)
[im1, des1, loc1] = sift(image1); %找出每張圖的SIFT關(guān)鍵點(diǎn)
[im2, des2, loc2] = sift(image2);
distRatio = 0.6;
des2t = des2'; %預(yù)計(jì)算矩陣轉(zhuǎn)置
for i = 1 : size(des1,1)
dotprods = des1(i,:) * des2t; % 點(diǎn)積向量
[vals,indx] = sort(acos(dotprods)); %取反余弦并對(duì)結(jié)果進(jìn)行排序
%檢查最近鄰的角度是否小于2*distRatio.
if (vals(1) < distRatio * vals(2))
match(i) = indx(1);
else
match(i) = 0;
end
end
% 顯示匹配點(diǎn)連接的圖像
newImg = cat(2,im1,im2); %將兩張圖像放在一張圖中
figure; imshow(newImg)
hold on
plot(loc1(:,2),loc1(:,1), 'ro','MarkerSize',5,'LineWidth',0.7)
plot(loc2(:,2)+size(im1,1),loc2(:,1), 'm*','MarkerSize',5,'LineWidth',0.7)
cols1 = size(im1,2);
for i = 1: size(des1,1)
if (match(i) > 0)
line([loc1(i,2) loc2(match(i),2)+cols1], ...
[loc1(i,1) loc2(match(i),1)], 'Color', 'c');
end
end
hold off;
num = sum(match > 0);
fprintf('Found %d matches.\n', num);
% 保存結(jié)果
frame=getframe(gcf);
im=frame2im(frame);
imwrite(im,'S圖像匹配結(jié)果.jpg');
%% 主程序
img1=imread('baby1.JPG');
img2=imread('baby2.JPG');
img1_gray=rgb2gray(img1);
img2_gray=rgb2gray(img2);
match('img1_gray.jpg',' img2_gray.jpg ');
匹配結(jié)果
文章來源地址http://www.zghlxwxcb.cn/news/detail-805045.html
到了這里,關(guān)于基于SIFT算法的圖像匹配的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!