这个例子展示了如何在一个杂乱的场景中检测一个特定的物体,给定一个物体的参考图像。
该示例介绍了一种用于基于参考和目标图像之间的查找点对应关系来检测特定对象的算法。尽管缩放变化或面内旋转,它可以检测对象。它对少量外平面旋转和闭塞也是强大的。
这种对象检测方法最适合展示非重复纹理模式的对象,这导致独特的特征匹配。这种技术不太可能适用于均匀彩色的物体,也不太适用于包含重复模式的物体。注意,该算法被设计用于检测特定对象,例如参考图像中的大象,而不是任何大象。检测特定类别的对象,例如人或面部,见Vision.Peopledetector
和Vision.cascadeObjectDetector.
。
读取包含感兴趣对象的参考图像。
boximage = imread('stapleremover.jpg');图;imshow(盒子);标题(“盒子的图像”);
阅读目标图像包含一个混乱的场景。
sceneimage = imread('cluttereddesk.jpg');图;imshow (sceneImage);标题(“混乱场景的图像”);
检测两幅图像中的特征点。
boxpoints = detectsurfeatures(bondimage);情景=检测到(SceeImage);
可视化参考图像中最强的特征点。
图;imshow(盒子);标题('来自Box Image的100个最强大的功能点');抓住在;情节(selectStrongest (boxPoints, 100));
可视化目标图像中找到的最强的特征点。
图;imshow (sceneImage);标题(从场景图像中的300个最强的特征点');抓住在;绘图(Selectstrongest(SenfePoints,300));
在两幅图像的兴趣点处提取特征描述符。
[BoxFeatures,Boxpoints] = ExtractFeatures(BoxImage,Boxpoints);[scenefeatures,scenepoints] =提取物(sceneImage,scenepoints);
使用特征描述符匹配特征。
boxPairs = matchFeatures(boxFeatures, sceneFeatures);
显示匹配的功能。
matchedBoxPoints = boxPoints(boxPairs(:, 1),:);matchedScenePoints = scenePoints(boxPairs(:, 2),:);图;showMatchedFeatures (boxImage sceneImage matchedBoxPoints,......matchedscenepoints,'剪辑');标题(“假定匹配点(包括异常值)”);
estimateGeometricTransform2D
计算与匹配点相关的转换,同时消除异常值。此转换允许我们本地化场景中的对象。
[tform,inlieridx] =......estimationGeometricTransform2d(MatchedBoxPoints,MatchedScenepoints,'仿射');inlierBoxPoints = matchedBoxPoints(inlierIdx,:);inlierScenePoints = matchedScenePoints(inlierIdx,:);
显示匹配点对具有删除的异常值
图;ShowMatchedFeatures(Bondimage,SceneImage,InlierboxPoints,......inlierscenepoints,'剪辑');标题('匹配点(仅限最基于)');
得到参考图像的边界多边形。
boxpolygon = [1,1;......%左上的尺寸(boxImage, 2), 1;......% 右上大小(boxImage, 2),大小(boxImage, 1);......%右下方1,尺寸(盒子,1);......% 左下方1,1];%重新左侧再次关闭多边形
将多边形转换为目标图像的坐标系统。变换后的多边形表示物体在场景中的位置。
newboxpolygon = transformpointsforword(tform,boxpolygon);
显示检测到的对象。
图;imshow (sceneImage);抓住在;line(newBoxPolygon(:, 1), newBoxPolygon(:, 2),“颜色”那'是');标题('检测到的盒子');
使用与前面相同的步骤检测第二个对象。
读取包含第一个感兴趣对象的图像。
elephantImage = imread (“elephant.jpg”);图;imshow(elephantimage);标题('大象的形象');
检测和可视化特征点。
Elephantpoints =检测(ElephantImage);图;imshow(elephantimage);抓住在;情节(selectStrongest (elephantPoints, 100));标题(“大象图像的100个最强特征点”);
提取特征描述符。
[Elephantfeatures,Elephantpoints] =提取物(Elephantimage,Elephantpoints);
匹配功能
Elephantpairs = MatchFeatures(象形物质,风景,'maxratio', 0.9);
显示匹配的功能。
matchedElephantPoints = elephantPoints(elephantPairs(:, 1),:);matchedScenePoints = scenePoints(elephantPairs(:, 2),:);图;showMatchedFeatures (elephantImage sceneImage matchedElephantPoints,......matchedscenepoints,'剪辑');标题(“假定匹配点(包括异常值)”);
估计几何变换并消除异常值
[tform,Inlierelehantpoints,Inlierscenepoints] =......estimateGeometricTransform (matchedElephantPoints matchedScenePoints,'仿射');图;showMatchedFeatures (elephantImage sceneImage inlierElephantPoints,......inlierscenepoints,'剪辑');标题('匹配点(仅限最基于)');
显示两个对象
大象多边形= [1,1;......%左上的尺寸(elephantimage,2),1;......% 右上尺寸(elephantimage,2),尺寸(elephantimage,1);......%右下方1、大小(elephantImage, 1);......% 左下方1,1];%重新左侧再次关闭多边形Newelephantpolygon = Transformpointsforward(Tform,ElephantPolygon);图;imshow (sceneImage);抓住在;line(newBoxPolygon(:, 1), newBoxPolygon(:, 2),“颜色”那'是');线(Newelephantpolygon(:,1),Newelephantpolygon(:,2),“颜色”那'G');标题(《被发现的大象和盒子》);