1. 程式人生 > >基於特徵點的物體檢測

基於特徵點的物體檢測

本教程是一種用於基於在參考影象和目標影象之間找到點對應關係來檢測特定物件的演算法。儘管尺度變化或平面內旋轉,它仍可以檢測物體。它對於少量的平面外旋轉和遮擋也很穩健。

這種物件檢測方法最適用於呈現非重複紋理圖案的物件,這會產生獨特的特徵匹配。對於均勻著色的物件或包含重複圖案的物件,此技術不太適用。請注意,此演算法用於檢測特定物件,例如參考影象中的大象,而不是任何大象。

使用的圖片
在這裡插入圖片描述
在這裡插入圖片描述


完整的Matlab程式碼

%% 清楚工作空間
clc;
clear all;

%% 讀取參考圖片 
boxImage = imread('stapleRemover.jpg');
boxImage = rgb2gray(boxImage);
figure;
imshow(boxImage);
title('Image of a Box');

%% 讀取要處理的圖片
sceneImage = imread('clutteredDesk.jpg');
sceneImage = rgb2gray(sceneImage);
figure;
imshow(sceneImage);
title('Image of a Cluttered Scene');

%% 提取特徵點
boxPoints = detectSURFFeatures(boxImage);
scenePoints = detectSURFFeatures(sceneImage);

%% Visualize the strongest feature points found in the reference image.
figure;
imshow(boxImage);
title('100 Strongest Feature Points from Box Image');
hold on;
plot(selectStrongest(boxPoints, 100));

%% Visualize the strongest feature points found in the target image.
figure;
imshow(sceneImage);
title('300 Strongest Feature Points from Scene Image');
hold on;
plot(selectStrongest(scenePoints, 300));

%% 提取特徵描述
[boxFeatures, boxPoints] = extractFeatures(boxImage, boxPoints);
[sceneFeatures, scenePoints] = extractFeatures(sceneImage, scenePoints);

%% 找到匹配點
boxPairs = matchFeatures(boxFeatures, sceneFeatures);

%% 顯示匹配效果
matchedBoxPoints = boxPoints(boxPairs(:, 1), :);
matchedScenePoints = scenePoints(boxPairs(:, 2), :);
figure;
showMatchedFeatures(boxImage, sceneImage, matchedBoxPoints, ...
    matchedScenePoints, 'montage');
title('Putatively Matched Points (Including Outliers)');

%% 通過匹配找到特定的物體
[tform, inlierBoxPoints, inlierScenePoints] = ...
    estimateGeometricTransform(matchedBoxPoints, matchedScenePoints, 'affine');
%% 顯示匹配效果
figure;
showMatchedFeatures(boxImage, sceneImage, inlierBoxPoints, ...
    inlierScenePoints, 'montage');
title('Matched Points (Inliers Only)');

boxPolygon = [1, 1;...                           % top-left
        size(boxImage, 2), 1;...                 % top-right
        size(boxImage, 2), size(boxImage, 1);... % bottom-right
        1, size(boxImage, 1);...                 % bottom-left
        1, 1];                   % top-left again to close the polygon
newBoxPolygon = transformPointsForward(tform, boxPolygon);

%% 顯示被檢測到的物體
figure;
imshow(sceneImage);
hold on;
line(newBoxPolygon(:, 1), newBoxPolygon(:, 2), 'Color', 'y');
title('Detected Box');