MATLAB Answers

How to define coordinates of orientated BoundingBox for every case?

5 views (last 30 days)
r_com3 on 8 Jan 2020
Commented: Image Analyst on 8 Jan 2020
In my project I am using orientated Bounding Box to fit it to my objects in my binary image.
The oriented bounding box is taken from the algorythm in this link :
And on my images it looks like this:
In my project i need to find the coordinates of the each corner of each bounding Box.
So i set coordinates from the function i mentioned to the output of the function, and i saved them in a structure and it looks like this:
And [ vx(k),vy(k)] are the coordinates of 4 corners of bounding box.
But my problem is, for different images the order of coordinates is changing. What i mean is , in one case the first coordinate [vx(1),vy(1)] is the coordinate of top left corner of bounding box, second coordinate is the coordinate of top right, etc..
But in the other case, the first coordinate can be the coordinate of bottom right corner, second coordinate can be coordinate of the top left, etc..
My question is, how can i determine the output of the function so that these coordinates are regular, so that first coordinate ALWAYS responds to one corner, second ALWAYS to the other , third and fourt too.
If anyone has idea how to perform it, I'd be very grateful.
Other solution could be using some mathematical conditions based on the values of both x and y, but its quite hard for me to work out.


Sign in to comment.

Accepted Answer

Matt J
Matt J on 8 Jan 2020
Edited: Matt J on 8 Jan 2020
How about if you find the centroid (ic,jc) of each region and then identify the bounding box corner (i,j) satisfying the condition
isUpperLeft = ic>i & jc>j
This, would define the "upper-left" corner of the box, and you could rotate the lists so that this corner always appears first. You could also create definitions of the other corners based on the signs of ic-i and jc-j, in a similar way.


Matt J
Matt J on 8 Jan 2020
You're welcome, but please Accept-click one of the answers if it solves the issue.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 8 Jan 2020
I just did something very similar last week. What I did was to get the coordinates of the 4 corners of the image and then use pdist2() to find out which corner was closest to which box coordinate. I adapted the program slightly, and I think this should work if you pass in the 4 box corners. Repeat for each of the boxes.
% Now make sure we know which point is which.
% Row 1 = (x, y) for the upper left box point.
% Row 2 = (x, y) for the upper right box point.
% Row 3 = (x, y) for the lower left box point.
% Row 4 = (x, y) for the lower right box point.
function outputXY = ArrangePoints(xy, rows, columns)
% Make an array for the 4 corners of the image.
corners = [1, 1; columns, 1; 1, rows; columns, rows];
% Get distances of each of our 4 box points to each of the 4 image corners.
distances = pdist2(corners, xy);
% Find the indexes of points 1, 2, 3, and 4. They will have min distance to their respective corners.
index = zeros(4, 1);
[d1, index(1)] = min(distances(1,:));
[d2, index(2)] = min(distances(2,:));
[d3, index(3)] = min(distances(3,:));
[d4, index(4)] = min(distances(4,:));
% Sort the points so that 1 will be the upper left, 2=upper right, 3 = lower left, and 4 = lower right.
outputXY = xy(index, :);
catch ME
% Some error happened if you get here.
errorMessage = GetErrorMessage(ME); % Get error message with call traceback and file's date.
WarnUser(errorMessage); % Pop up error message to show user what it is. Also prints to command window or console window (if program is an executable).
return; % ArrangePoints


r_com3 on 8 Jan 2020
okay, so i have 4 coordinates of corners. I dont know which one responds to upper left, which to upper right, etc.
could u explain more precisely how to implement it in your code please?
thanks in advance
Image Analyst
Image Analyst on 8 Jan 2020
outputXY has 4 rows with the coordinates of your box. Each row is one of your box corner coordinates, thus it is a 4 row by 2 column array. Like the comments say
% Row 1 = (x, y) for the upper left box point.
% Row 2 = (x, y) for the upper right box point.
% Row 3 = (x, y) for the lower left box point.
% Row 4 = (x, y) for the lower right box point.
To call it you'd do something like
[rows, columns, numberOfColorChannels] = size(yourImage)
for k = 1 : numberOfBoxes
xy = % however you get the 4 coordinates for one box
outputXY = ArrangePoints(xy, rows, columns)
% Now that you know which row (coordinate) corresponds to which corner
% do something with outputXY.

Sign in to comment.

Sign in to answer this question.

Translated by