Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Overlapping Bounding Boxes Problem

Subject: Overlapping Bounding Boxes Problem

From: Mark Gul

Date: 9 May, 2013 08:28:10

Message: 1 of 1

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
% See also
% 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)
    error('Bad size for inputs');
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.

Thanks in advance

Khalid

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us