My goal is to find the coordinates of the minimal bounding box for this image.

Binary Image

I am using the following command:

ComponentMeasurements[img, "MinimalBoundingBox"] // AbsoluteTiming

The output is the following:

{5.31663, {1 -> {{338.492, 1064.99}, {156.654, 
 71.8802}, {1084.66, -98.0355}, {1266.49, 895.079}}}}

Why does this computation take so long? It seems that ImageCrop can perform a similar computation and crop the image but it does so much much faster. Am I just using ComponentMeasurements innapropriately or is there something else I am missing?

Thanks for your advice.

  • $\begingroup$ If you can do it with ImageCrop then ComponentMeasurements is overkill, yes. It's doing something like this, to find the components, and that's more complicated than finding max and minimum coordinates of all components combined. $\endgroup$
    – C. E.
    Commented Oct 5, 2016 at 20:14
  • $\begingroup$ My problem with ImageCrop is that it doesn't return the coordinates you would use from the original image to give the minimal bounding box. $\endgroup$
    – Brady Hunt
    Commented Oct 5, 2016 at 21:22
  • 3
    $\begingroup$ ok, it makes more sense now but I just realized that MinimalBoundingBox returns an oriented bounding box, which is another important difference from ImageCrop. If it were just an axes-oriented bounding box then a good start might have been MinMax /@ Transpose[SparseArray[ImageData[img]]["NonzeroPositions"]]. Finding an oriented bounding box is more complicated. $\endgroup$
    – C. E.
    Commented Oct 5, 2016 at 22:06
  • $\begingroup$ You could use ComponentMeasurements[img, "BoundingBox"] if you just want the axis-oriented bounding box. That's much faster $\endgroup$ Commented Oct 6, 2016 at 6:24

1 Answer 1


ComponentMeasurements[img, "MinimalBoundingBox"] finds the smallest bounding box in any orientation, which is a more complex problem than what ImageCrop solves:

pts = ComponentMeasurements[img, "MinimalBoundingBox"][[1, 2]];
HighlightImage[img, Polygon[pts]]

enter image description here

If that's what you need, you can make it a little faster by passing only the perimeter to ComponentMeasurements, so it has to check fewer pixels:

 MorphologicalPerimeter@img, "MinimalBoundingBox"]

runs about 20x faster on my PC and gives the same bounding box. (At least unless your objects have holes!)

If you're only interested in the axis-aligned bounding box, use ComponentMeasurements[img, "BoundingBox"] instead:

b = ComponentMeasurements[img, "BoundingBox"][[1, 2]];
HighlightImage[img, Rectangle @@ b]

enter image description here

  • $\begingroup$ Thanks! I should've noticed there was a MinimalBoundingBox and a BoundingBox! This makes more sense. $\endgroup$
    – Brady Hunt
    Commented Oct 7, 2016 at 15:58

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.