Measuring the width and hight for smallest Bounding Boxes of detected objects
Show older comments
Hi guys,
Kindly looking for Measuring the width and highth for the smallest Bounding Boxex of detected object in the following code
load('Detector.mat');
vidReader = VideoReader('vs_002_00.avi');
vidPlayer = vision.DeployableVideoPlayer;
i = 1;
results = struct('Boxes',[],'Scores',[]);
while(hasFrame(vidReader))
I = readFrame(vidReader);
% PROCESS
[bboxes, scores, label] = detect(detector,I,'MiniBatchSize', 128);
% Select strongest detection
% New - Find those bounding boxes that surpassed a threshold
T = 0.5; % Define threshold here
idx = scores >= T;
% Retrieve those scores that surpassed the threshold
s = scores(idx);
% Do the same for the labels as well
lbl = label(idx);
bboxes = bboxes(idx, :); % This logic doesn't change
for ii = 1 : size(bboxes, 1)
annotation = sprintf('%s: (Confidence = %f)', lbl(ii), s(ii)); % Change
I = insertObjectAnnotation(I, 'rectangle', bboxes(ii,:), annotation); % New - Choose the right box
end
boundingBoxArea = prod(boundingBox(3:4));
step(vidPlayer,I);
i = i+1;
end
results = struct2table(results);
release(vidPlayer);
2 Comments
Guillaume
on 20 Jan 2020
Is the variable boundingBox defined somewhere in your code? It's not clear what its relationship is to bboxes.
You haven't actually asked a question, so I'm not exactly sure what you want.
Abdussalam Elhanashi
on 20 Jan 2020
Accepted Answer
More Answers (1)
Image Analyst
on 20 Jan 2020
0 votes
If you want boxes aligned with the image edges, then use regionprops() and ask for 'BoundingBox'.
If you want boxes at any angle, use bwferet().
Or you could use John's program. https://www.mathworks.com/matlabcentral/fileexchange/34767-a-suite-of-minimal-bounding-objects
16 Comments
Abdussalam Elhanashi
on 20 Jan 2020
Image Analyst
on 20 Jan 2020
I appreciate getting the original RGB image but where is your code and your segmented (binary) image?
Somehow you must have segmented that image to get a pixel map of the "detected object" pixels. I have no idea what object in that photo you detected.
Or you might have used Deep Learning to find something in the image, like orange blobs or rectangular regions or something. I have no idea how you found your "detected object" or even what the "detected" object is.
Abdussalam Elhanashi
on 20 Jan 2020
Guillaume
on 20 Jan 2020
@ImageAnalyst, in Abdulassam code, the segmentation is performed by the detect function of the computer vision toolbox. This function directly returns the bounding boxes of the detected objects (according to a preconfigured detector).
Image Analyst
on 20 Jan 2020
That is not always the smallest, though I'm not convinced he actually needs the smallest. Using one aligned with image edges may be good enough.
But you're right about him not defining boundingBox (maybe put that as a separate answer). Is that the overall bounding box of all the smaller, interior bounding boxes?
Does he want the total area of all bounding boxes? So maybe this:
boundingBoxArea = 0;
for ii = 1 : size(bboxes, 1)
annotation = sprintf('%s: (Confidence = %f)', lbl(ii), s(ii)); % Change
I = insertObjectAnnotation(I, 'rectangle', bboxes(ii,:), annotation); % New - Choose the right box
boundingBoxArea = boundingBoxArea + bboxes(ii, 3) * bboxes(ii, 4);
end
% Don't use this line boundingBoxArea = prod(boundingBox(3:4));
No idea what he wants.
Abdussalam Elhanashi
on 20 Jan 2020
Image Analyst
on 21 Jan 2020
So index it and take the min
boundingBoxArea(ii) = bboxes(ii, 3) * bboxes(ii, 4);
then after the loop
minBoxArea = min(boundingBoxArea)
Abdussalam Elhanashi
on 22 Jan 2020
Guillaume
on 22 Jan 2020
Abdussalam Elhanashi
on 22 Jan 2020
Image Analyst
on 22 Jan 2020
Maybe try
bboxes = bboxes(idx, :); % This line from your code. Unchanged
bboxesarea = prod(bboxes(:, 3:4), 2); %calculate the area of all the bounding boxes by multiplying height by width. produces a column vector
[smallestarea, boxindex] = min(bboxesarea); %get index of smallest bounding box (and area if you need it).
%optional: label the bounding box in red
textLabel = sprintf('%s(Confidence = %0.02f, Area=%d pixels = %d wide by %d tall', ...
lbl(boxindex), s(boxindex), smallestarea, bboxes(boxindex, 3), bboxes(boxindex, 4))
I = insertObjectAnnotation(I, 'rectangle', bboxes(boxindex,:), textLabel);
Abdussalam Elhanashi
on 22 Jan 2020
Image Analyst
on 22 Jan 2020
Yes, but you have to say for width and height if you're using whole pixels, or the distance from pixel center to pixel center. For example, in this vector [0 1 1 1 0] what would you say the "width" of the 1 region is? Is it 3 or 2? It's 3 if you're considering whole pixels, but 2 if you go from the center of the left pixel to the center of the right pixel. Either could be "right" -- it really depends on the physics and optics of your image capture situation. And MATLAB functions aren't always consistent on which they use. For example regionprops() will compute the area based on whole pixels, while bwarea() does not (it uses a more complicated algorithm).
Abdussalam Elhanashi
on 22 Jan 2020
Image Analyst
on 23 Jan 2020
Like I said, yes. Make sure you understand my last comment though.
Abdussalam Elhanashi
on 19 May 2020
Categories
Find more on Object Detection in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

