MATLAB: Get the width of bounding box

imaimage processingImage Processing Toolboximage segmentation

i'm trying to make a size sorter but ran into a problem; does anyone know how to make matlab draw a box around this potato and then determine the width of this box?

Best Answer

It seems that regionprops function will be helpful, like:
% Read the image and binarize
I = imread('aardappels.jpg');
% Create the region of interest (ROI)
Igray = rgb2gray(I);
ROI = ~imbinarize(Igray, 0.9);
ROI = bwconvhull(ROI);
% Measure properties of the ROI
stats = regionprops(ROI, 'BoundingBox','MajorAxisLength','MinorAxisLength');
% Show the result
imshow(I);
hold on;
rectangle('Position', stats.BoundingBox,'EdgeColor','b');
The major and minor axis of the bounding box is stored in stats.
>> stats
stats =
BoundingBox: [53.5000 68.5000 698 883]
MajorAxisLength: 899.6647
MinorAxisLength: 708.4894