确定在MATLAB中捕获对象的最小边界框 [英] Determine the minimum bounding box to capture an object in MATLAB

查看:620
本文介绍了确定在MATLAB中捕获对象的最小边界框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将背景减去图像作为输入。该想法是通过使用用于HOG算法的较小搜索区域来减少用于人检测的搜索区域。所需的输出是围绕人的边界框和框角的像素位置。

I have background subtracted images as input. The idea is to reduce search areas for person detection by using a smaller search area for the HOG algorithm. The output required is a bounding box around the person and the pixel positions of the box corners.

这是输入图像:

这是必需的输出:

这是我试过的远:

x=imread('frame 0080.png');   
y=im2bw(x);
s=regionprops(y);    

imshow(y);    
hold on

for i=1:numel(s)  
   rectangle('Position',s(i).BoundingBox,'edgecolor','y')
end

这是我得到的输出:

推荐答案

看起来你已经尝试了我的建议。但是,您需要封装整个对象的边界框。这可以通过使用 BoundingBox 属性,然后计算每个矩形的四个角中的每一个来轻松完成。然后,您可以计算封装最终封装整个对象的所有矩形的最小跨越边界框。

It looks like you have tried what I have suggested. However, you want the bounding box that encapsulates the entire object. This can easily be done by using the BoundingBox property, then calculating each of the four corners of each rectangle. You can then calculate the minimum spanning bounding box that encapsulates all of the rectangles which ultimately encapsulates the entire object.

我注意到底部有一个薄的白色条带你的图像,这将搞乱边界框计算。因此,在我们继续计算最小跨越边界框之前,我将剪切图像的最后10行。要计算最小跨越边界框,您所要做的就是占用所有矩形的所有角,然后计算最小值和最大值 x 坐标和最小值和最大 y 坐标。这些将对应于最小跨越边界框的左上角和最小跨越边界框的右下角。

I do notice that there is a thin white strip at the bottom of your image, and that will mess up the bounding box calculations. As such, I'm going to cut the last 10 rows of the image before we proceed calculating the minimum spanning bounding box. To calculate the minimum spanning bounding box, all you have to do is take all of the corners for all of the rectangles, then calculate the minimum and maximum x co-ordinates and minimum and maximum y co-ordinates. These will correspond to the top left of the minimum spanning bounding box and the bottom right of the minimum spanning bounding box.

查看 BoundingBox 使用 regionprops 的属性,每个边界框输出一个4元素向量:

When taking a look at the BoundingBox property using regionprops, each bounding box outputs a 4 element vector:

[x y w h]

x,y 表示边界框的左上角坐标。 x 将是列, y 将是左上角的行。 w,h 表示边界框的宽度和高度。我们将使用它并计算检测到的每个矩形的左上角,右上角,左下角和右下角。完成此操作后,将所有这些矩形坐标堆叠成一个2D矩阵,然后计算最小值和最大值 x y 坐标。要计算矩形,只需使用最小 x y 坐标作为左上角,然后计算宽度和高度分别减去最大值和最小值 x y 坐标。

x,y denote the top left co-ordinate of your bounding box. x would be the column and y would be the row of the top left corner. w,h denote the width and the height of the bounding box. We would use this and compute top left, top right, bottom left and bottom right of every rectangle that is detected. Once you complete this, stack all of these rectangle co-ordinates into a single 2D matrix, then calculate the minimum and maximum x and y co-ordinates. To calculate the rectangle, simply use the minimum x and y co-ordinates as the top left corner, then calculate the width and height by subtracting the maximum and minimum x and y co-ordinates respectively.

不用多说,这是代码。请注意,我想在 N x 4 矩阵中提取所有边界框坐标,其中 N 表示检测到的边界框数量。你必须使用 reshape 正确执行此操作:

Without further ado, here's the code. Note that I want to extract all of the bounding box co-ordinates in a N x 4 matrix where N denotes the number of bounding boxes that are detected. You would have to use reshape to do this correctly:

% //Read in the image from StackOverflow
x=imread('http://i.stack.imgur.com/mRWId.png');

% //Threshold and remove last 10 rows
y=im2bw(x);
y = y(1:end-10,:);

% //Calculate all bounding boxes
s=regionprops(y, 'BoundingBox');

%// Obtain all of the bounding box co-ordinates
bboxCoords = reshape([s.BoundingBox], 4, []).';

% // Calculate top left corner
topLeftCoords = bboxCoords(:,1:2);

% // Calculate top right corner
topRightCoords = [topLeftCoords(:,1) + bboxCoords(:,3) topLeftCoords(:,2)];

% // Calculate bottom left corner
bottomLeftCoords = [topLeftCoords(:,1) topLeftCoords(:,2) + bboxCoords(:,4)];

% // Calculate bottom right corner
bottomRightCoords = [topLeftCoords(:,1) + bboxCoords(:,3) ...
    topLeftCoords(:,2) + bboxCoords(:,4)];

% // Calculating the minimum and maximum X and Y values
finalCoords = [topLeftCoords; topRightCoords; bottomLeftCoords; bottomRightCoords];
minX = min(finalCoords(:,1));
maxX = max(finalCoords(:,1));
minY = min(finalCoords(:,2));
maxY = max(finalCoords(:,2));

% Draw the rectangle on the screen
width = (maxX - minX + 1);
height = (maxY - minY + 1);
rect = [minX minY width height];

% // Show the image
imshow(y);
hold on;
rectangle('Position', rect, 'EdgeColor', 'yellow');






这是我得到的图像:


This is the image I get:

这篇关于确定在MATLAB中捕获对象的最小边界框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆