Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Overlapping Bounding Boxes Problem
Replies: 0

 Khalid Iqbal Posts: 33 Registered: 12/23/09
Overlapping Bounding Boxes Problem
Posted: May 9, 2013 4:28 AM

Dear Friends
I am trying to draw rectangles using bounding boxes coordinates which should not overlap. I am not getting the required output. I want to draw different rectangles after removing their intersecting aspect. Please reply, it is very urgent.

Matlab Code:
recBox contains information [x,y,width,height]
recBox=[1 5 3 2;3 4 2 4;1 10 3 2;2 9 3 2;7 5 5 4;8 6 3 2];
figure(1);
for n=1:size(recBox,1)
rectangle('Position',recBox(n,1:4),'EdgeColor','g','LineWidth',2);
pause(0.5);
end
--------------------------------------------------------------------
for j=1:size(recBox,1)-1
for k=j:size(recBox,1)
box = intersectBoxes(recBox(j,:), recBox(k,:));
recBox(j,1)=box(1);
recBox(j,2)=box(2);
recBox(j,3)=box(3);
recBox(j,4)=box(4);
end
end
---------------------------------------------------------------------------
function box = intersectBoxes(box1, box2)
%INTERSECTBOXES Intersection of two bounding boxes
%% RES = intersectBoxes(BOX1, BOX2)
%% Example:
% box1 = [5 20 5 30];
% box2 = [0 15 0 15];
% intersectBoxes(box1, box2)
% ans = 5 15 5 15
% boxes2d, drawBox, mergeBoxes
% unify sizes of data
if size(box1,1) == 1
box1 = repmat(box1, size(box2,1), 1);
elseif size(box2, 1) == 1
box2 = repmat(box2, size(box1,1), 1);
elseif size(box1,1) ~= size(box2,1)
end

% compute extreme coords
mini = min(box1(:,[2 4]), box2(:,[2 4]));
maxi = max(box1(:,[1 3]), box2(:,[1 3]));

% concatenate result into a new box structure
box = [maxi(:,1) mini(:,1) maxi(:,2) mini(:,2)];
--------------------------------------------------------------------------
recBoxx=[];recBoxx=unique(recBox,'rows');
for n=1:size(recBoxx,1)
rectangle('Position',recBoxx(n,1:4),'EdgeColor','r','LineWidth',2);
pause(0.5);
end
hold off;
------------------------------------------------------------------------
I am in urgency and need your help.