主要内容

基于点特征匹配的杂乱场景目标检测

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

概述

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

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

第一步:读取图像

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

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

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

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

第二步:检测特征点

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

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);

步骤4:找到假定的点匹配

使用描述符匹配特征。

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;...%左上的尺寸(boxImage, 2), 1;...%右上的size(boxImage, 2), size(boxImage, 1);...%右下角1、大小(boxImage, 1);...%左下侧1,1];%左上角再次关闭多边形

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

newBoxPolygon = transformPointsForward(tform, boxPolygon);

显示检测到的对象。

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

步骤6:检测另一个对象

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

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

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

检测和可视化特征点。

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

提取特征描述符。

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

匹配功能

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

显示假定匹配的特征。

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

几何变换估计与离群值剔除

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

显示两个对象

大象多边形= [1,1;...%左上的尺寸(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’);标题(“发现大象和盒子”);

Baidu
map