我正在尝试在所有重叠的较小边界框周围绘制一个外边界框。整个图像中可能有许多这样的区域。
e.g。
到目前为止,我的矩形矢量称为rects。
overlaps = rectint(rects, rects);
我检查彼此重叠的地方,因为它会与自身进行比较,我会删除对角线,如下所示:
overlaps(logical(eye(size(overlaps)))) = 0;
然后找到重叠的位置
[r,c] = find(overlaps > 0);
但是,我不知道如何处理这个,因为它不是返回的方形矩阵中的简单双向映射,因为该区域可能存在多个重叠。
非常感谢任何有关我如何进行的建议。
由于
答案 0 :(得分:5)
这里有一些随机矩形的示例:
% Generate fake data, 3 rects with format [x,y,w,h]:
rects=20+randi(60,3,4);
% plot the rects :
for n=1:size(rects,1)
rectangle('Position',rects(n,:));
end
% get min max
xmin=min(rects(:,1));
ymin=min(rects(:,2));
xmax=max(rects(:,1)+rects(:,3));
ymax=max(rects(:,2)+rects(:,4));
% define outer rect:
outer_rect=[xmin ymin xmax-xmin ymax-ymin];
hold on
rectangle('Position',outer_rect,'EdgeColor','r','LineStyle',':');
答案 1 :(得分:0)
function localBoxes = maxBoxVals(finalBoxes)
rects = [];
for es = 1 : length(finalBoxes)
y = finalBoxes(es, 1);
x = finalBoxes(es, 2);
y2 = finalBoxes(es, 3);
x2 = finalBoxes(es, 4);
rects = [rects ; [x, y, y2-y, x2 - x]];
end
overlaps = rectint(rects, rects);
%overlaps(logical(eye(size(overlaps)))) = 0;
[r,c] = find(overlaps > 0);
localBoxes = [];
for i = 1 : length(overlaps)
col = overlaps(:,i);
col2 = find(col > 0);
if(~length(col2) > 0)
continue;
end
localRects = [rects(i,:)];
for j = 1 : length(col2)
localRects = [localRects ; rects(col2(j),:)];
end
if(max(localRects) > 0)
minX = min(localRects(:,1));
minY = min(localRects(:,2));
maxX = max(localRects(:,1))+max(localRects(:,3));
maxY = max(localRects(:,2))+max(localRects(:,4));
localBoxes = [localBoxes ; [minX,minY,abs(maxX - minX), abs(maxY - minY)]];
end
end
localBoxes = unique(localBoxes,'rows')
end
这是我想出来解决的问题。没有矢量化,绝对不是最佳的,可能是一些错误,但似乎完成了我需要的工作,并适用于整个图像。