- SIFT 特征匹配算法(识别精准度较高,速度较慢)
主要调用函数为vl_sift、vl_ubcmatch,其中vl_ubcmatch方法的第三个参数可用作筛选特征点,值越高,筛选力度越大;
addpath('vlfeat/toolbox');
vl_setup;
img1 = imread('C:\Users\Administrator\Desktop\picture\1683856107084.jpeg');
img2 = imread('C:\Users\Administrator\Desktop\picture\1683855996442.jpeg');
gray_img1 = single(rgb2gray(img1));
gray_img2 = single(rgb2gray(img2));
[f1, d1] = vl_sift(gray_img1);
[f2, d2] = vl_sift(gray_img2);
matches = vl_ubcmatch(d1, d2, 10); % vlfeat 内置函数用于匹配特征点
img3 = cat(2, img1, img2); % 拼接为一张图像
figure;
imshow(img3);
hold on;
x1 = f1(1, matches(1,:)) ;
x2 = f2(1, matches(2,:)) + size(img1,2) ;
y1 = f1(2, matches(1,:)) ;
y2 = f2(2, matches(2,:)) ;
line([x1 ; x2], [y1 ; y2], 'Color', 'g', 'LineWidth', 1); % 画出匹配的连线
% vl_plotframe(f1(:,matches(1,:))) ; % 标注特征点
% f2(1,:) = f2(1,:) ;
% vl_plotframe(f2(:,matches(2,:))) ;
plot(x1,y1,'r.','MarkerSize',16);
plot(x2,y2,'r.','MarkerSize',16);
hold on;
测试效果:
- SURF特征匹配算法(精度较差,速度较快)
clc
clear
im = imread('C:\Users\Administrator\Desktop\picture\1.jpeg');
im2 = imread('C:\Users\Administrator\Desktop\picture\2.jpeg');
im_gray = rgb2gray(im);
im2_gray = rgb2gray(im2);
points = detectSURFFeatures(im_gray);
points2 = detectSURFFeatures(im2_gray);
[features1, validPoints1] = extractFeatures(im_gray, points,'SURFSize',128);
[features2, validPoints2] = extractFeatures(im2_gray, points2,'SURFSize',128);
% 特征点匹配
indexPairs = matchFeatures(features1, features2, ...
'MatchThreshold', 20, 'MaxRatio', 0.8, 'Metric', 'SSD');
matchedPoints1 = validPoints1(indexPairs(1:10, 1), :);
matchedPoints2 = validPoints2(indexPairs(1:10, 2), :);
[tform, inlierBoxPoints, inlierScenePoints] = estimateGeometricTransform(matchedPoints1, matchedPoints2, 'affine');
figure(3);
showMatchedFeatures(im, im2, matchedPoints1, matchedPoints2, 'montage');
测试效果: