how can assign numbers to detected bounding boxes?

I have a final bounding box attributes and an Image. I need to assign a number to each bounding box from the top left side in a sequential order. I have an object detected image like this.
Now I need a numbered object detected image some this like this. (Number position and size is not an issue)
How can I do that?

1 Comment

Please attach you image and required things so that we can help you in a bit more effective manner.

Sign in to comment.

 Accepted Answer

Put the coordinates into a 2D array. sortrows() with either [1 2] or [2 1] depending upon whether the numbering priority is down ([1 2]) or across ([2 1]) . Afterwards, the row number will be the number to use for the bounding box.

8 Comments

Can you please share some example code for numbering the bounding box.

I have developed this code with manually taken boundary box positions.

Can you show me how to use directly boundary box values

I = imread('test.jpg');
%Boundary box posiiotn
boundary_box= [925, 285, 1045, 374
               706, 279, 786, 350
               5,   266, 220, 407
               947, 324, 1280, 705
               159, 303, 346, 440
               761, 282, 942, 412
               367, 300, 745, 648];
position = floor([(boundary_box(:,1)+boundary_box(:,3))/2,(boundary_box(:,2)+boundary_box(:,4))/2]) 
temp = sum(position');
[val, idx] = sort(temp);
newpositions = position(idx,:);
RGB = insertText(I,newpositions,[1:length(position)],'FontSize',22, 'AnchorPoint','LeftBottom');
figure
imshow(RGB),title('Sorted Numeric values');

The input image I have used is specified above.

The output which I got is shown here is

I do not understand your position calculation. Why not just

newpositions = sortrows(boundary_box, [1 2]);
I applied that, but I am not sure how to display number on each box with only that command.
You would use the insertText() as well.
RGB = insertText(I, newpositions, 1:size(newpositions,1), 'FontSize', 22, 'AnchorPoint' ,'LeftBottom');
insertText() needs input with two values, but this sort function is giving all four co-ordinate values.
When I tried on above input I am getting error shown in below
Expected input number 2, POSITION, to be of size Mx2 when it is actually size 7x4.
It looks like the output image you got is correct. Is there still a problem?
Be careful with sorting things, otherwise it's possible things could get mismatched. If you sort some things, like bounding box locations and labels, then you need to sort everything else the same way, otherwise, you might have the area of box 4 (say) with the area of box 2 (as shown by test on the image).
RGB = insertText(I, newpositions(:,1:2), 1:size(newpositions,1), 'FontSize', 22, 'AnchorPoint' ,'LeftBottom');

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!