主要内容

基于点特征匹配的混沌场景目标检测

这个例子展示了如何在一个混乱的场景中检测一个特定的对象,给定一个对象的参考图像。

概述

本例介绍了一种基于查找参考点和目标图像之间的点对应关系来检测特定对象的算法。它可以在尺度变化或平面内旋转的情况下检测物体。它对少量的面外旋转和遮挡也具有鲁棒性。

这种对象检测方法最适用于显示非重复纹理模式的对象,这些纹理模式会产生唯一的特征匹配。这种技术不太可能适用于颜色均匀的物体,或包含重复图案的物体。请注意,该算法是为检测特定对象而设计的,例如,参考图像中的大象,而不是任何大象。要检测特定类别的对象,如人或脸,请参见愿景。PeopleDetector而且愿景。CascadeObjectDetector

步骤1:读取图像

读取包含感兴趣对象的参考图像。

boxImage = imread(“stapleRemover.jpg”);图;imshow (boxImage);标题(“盒子的图像”);

读取包含杂乱场景的目标图像。

sceneImage = imread(“clutteredDesk.jpg”);图;imshow (sceneImage);标题(“混乱场景的图像”);

步骤2:检测特征点

检测两幅图像中的特征点。

boxPoints = detectSURFFeatures(boxImage);scenePoints = detectSURFFeatures(sceneImage);

可视化在参考图像中发现的最强特征点。

图;imshow (boxImage);标题(“盒子图像的100个最强特征点”);持有;情节(selectStrongest (boxPoints, 100));

可视化目标图像中发现的最强特征点。

图;imshow (sceneImage);标题(“来自场景图像的300个最强特征点”);持有;情节(selectStrongest (scenePoints, 300));

步骤3:提取特征描述符

在两个图像的兴趣点处提取特征描述符。

[boxFeatures, boxPoints] = extractFeatures(boxImage, boxPoints);[sceneFeatures, scenePoints] = extractFeatures(sceneImage, scenePoints);

第四步:找到假定的匹配点

使用特征描述符匹配特征。

boxPairs = matchFeatures(boxFeatures, sceneFeatures);

显示假定匹配的特征。

matchedBoxPoints = boxPoints(boxPairs(:, 1),:);matchedScenePoints = scenePoints(boxPairs(:, 2),:);图;showMatchedFeatures(boxImage, sceneImage, matchedBoxPoints,...matchedScenePoints,“蒙太奇”);标题(假定匹配点(包括异常值));

步骤5:使用假定匹配在场景中定位对象

estgeotform2d计算与匹配点相关的转换,同时消除异常值。这种转换允许我们在场景中本地化对象。

[tform, inlierIdx] = estgeotform2d(matchedBoxPoints, matchedScenePoints,仿射的);inlierBoxPoints = matchedBoxPoints(inlierIdx,:);inlierScenePoints = matchedScenePoints(inlierIdx,:);

显示移除异常值后的匹配点对

图;showMatchedFeatures(boxImage, sceneImage, inlierBoxPoints,...inlierScenePoints,“蒙太奇”);标题(匹配点(仅限Inliers));

获取参考图像的边界多边形。

boxPolygon = [1,1;...%左上的size(boxImage, 2), 1;...%右上的size(boxImage, 2), size(boxImage, 1);...%右下角1, size(boxImage, 1);...%左下侧1,1];%左上角再次关闭多边形

将多边形转换为目标图像的坐标系。转换后的多边形表示物体在场景中的位置。

newBoxPolygon = transformPointsForward(tform, boxPolygon);

显示检测到的对象。

图;imshow (sceneImage);持有;line(newBoxPolygon(:, 1), newBoxPolygon(:, 2), Color=“y”);标题(“检测盒”);

步骤6:检测另一个对象

使用与前面相同的步骤检测第二个对象。

读取包含第二个感兴趣对象的图像。

大象的年龄= imread(“elephant.jpg”);图;imshow (elephantImage);标题(“大象的形象”);

检测和可视化特征点。

elephantPoints = detectSURFFeatures(elephantImage);图;imshow (elephantImage);持有;情节(selectStrongest (elephantPoints, 100));标题(“大象图像的100个最强特征点”);

提取特性描述符。

[elephantFeatures, elephantPoints] = extractFeatures(elephantage, elephantPoints);

匹配功能

大象配对= matchFeatures(大象特征,sceneFeatures, MaxRatio=0.9);

显示假定匹配的特征。

matchedElephantPoints = elephantPoints(elephantPairs(:, 1),:);matchedScenePoints = scenePoints(elephantPairs(:, 2),:);图;showMatchedFeatures(elephantage, sceneImage, matchedElephantPoints,...matchedScenePoints,“蒙太奇”);标题(假定匹配点(包括异常值));

估计几何变换和排除异常值

[tform, inlierElephantPoints, inlierScenePoints] =...estimateGeometricTransform (matchedElephantPoints matchedScenePoints,仿射的);图;showMatchedFeatures(elephantImage, sceneImage, inlierElephantPoints,...inlierScenePoints,“蒙太奇”);标题(匹配点(仅限Inliers));

同时显示两个对象

大象多边形= [1,1;...%左上的size(elephantImage, 2), 1;...%右上的size(elephantImage, 2), size(elephantImage, 1);...%右下角1,大小(elephantImage, 1);...%左下侧1,1];%左上角再次关闭多边形newElephantPolygon = transformPointsForward(tform, elephantPolygon);图;imshow (sceneImage);持有;line(newBoxPolygon(:, 1), newBoxPolygon(:, 2), Color=“y”);line(newElephantPolygon(:, 1), newElephantPolygon(:, 2), Color=‘g’);标题(《发现大象和箱子》);