MATLAB: How to cluster a binary image

image processingImage Processing Toolboximage segmentation

I have the following image. Now I want to separate the black and white pixel from the image so that I can fit an ellipse to each black pixel area and find the length of the major axis of each ellipse. How can I separate black pixel from the white pixel and fit an ellipse?

Best Answer

Basically, here are the steps.
% Invert your image so the black things are white.
binaryImage = grayImage < 128;
% Call bwlabel
[labeledImage, numberOfRegions] = bwlabel(binaryImage);
% Call regionprops
props = regionprops(labeledImage, 'MajorAxisLength', 'Orientation');
It's all shown in Steve's blog: