2014-08-31 86 views
2

我有背景减去图像作为输入。这个想法是通过为HOG算法使用较小的搜索区域来减少用于人员检测的搜索区域。所需的输出是人物周围的边界框以及盒子角落的像素位置。确定在MATLAB中捕获对象的最小边界框

这是输入图像:

input image

这是所需的输出:

required output

这是我到目前为止已经试过:

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 

这是我得到的输出:

actual output

+0

查找到'regionprops'并选择'BoundingBox'属性:HTTP ://www.mathworks.com/help/images/ref/regionprops.html - 这将返回一个边界框,其起点位于左上角,以及宽度和高度。然后,您可以使用它并提取出相关的图像像素并进一步进行分析。我会写一个答案,这不是一个非常关键的话题。如果你想,删除这个问题,并创建一个新的问题。我会写一个答案。 – rayryeng 2014-08-31 04:26:18

回答

2

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

我注意到图像底部有一个很薄的白色条纹,这会弄乱边界框的计算。因此,在我们继续计算最小跨越边界框之前,我将去掉图像的最后10行。要计算最小跨越边界框,您只需将所有矩形的所有边角都计算出来,然后计算最小值和最大值以及最小值和最大值y坐标。这些将对应于最小跨越边界框的左上角和最小跨越边界框的右下角。

当考虑看看使用regionpropsBoundingBox属性,每个边界框输出4元素矢量:

[x y w h] 

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

不用再说了,这里是代码。请注意,我想提取N x 4矩阵中的所有边界框坐标,其中N表示检测到的边界框的数量。你将不得不使用reshape正确地做到这一点:

% //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'); 

这是我得到的图像:

enter image description here