If I do a ...

components = MorphologicalComponents[img, 0.3, Method->"BoundingBox"];

followed by a ...

binCounts = BinCounts[Flatten[components]];

I find that there is a prime number binCounts. Regardless of padding and other edge effects, shouldn't each entry in binCounts be the product of two integers; viz. the height and width of a (non-overlapping) bounding box? Perhaps the sides of the bounding box aren't parallel to the axes? If so, can one ask MorphologicalComponents for bounding boxes that are?


1 Answer 1


To experiment, I picked an image from the documentation for MorphologicalComponents:

Mathematica graphics

components = MorphologicalComponents[img, 0.3, Method -> "BoundingBox"];
components // Colorize

Mathematica graphics

This image shows that the bounding boxes are aligned with the axes. Let's now select all components with a prime number of elements:

SelectComponents[components, PrimeQ[#Count] &] // Colorize

Mathematica graphics

The only component that has a prime number of elements is the one that is partially overlapped. This means that the count for this particular component is not the product of the width and the height since the component is not rectangular.

This probably explains your problem as well.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.