Edit: Shorter ImageTrim
version as pointed out by Matthias Odisio
Belisarius gave you already the important hint: Use MorphologicalComponents
. There are two points I would make differently, which is I would and utilize ComponentMeasures
to extract the "BoundingBox"
which is already the row- and column-number you can then feed directly to ImageTake
or even better to ImageTrim
. ImageTrim
has the big advantage that it can handle the bounding box coordinates directly:
img = Import@"https://i.sstatic.net/8enYZ.png";
(* ImageTrim version *)
ImageTrim[img, #2] & @@@ ComponentMeasurements[
MorphologicalComponents[Binarize[GaussianFilter[img, 3]]], "BoundingBox"]
The GaussianFilter
just smooths the image a bit to ensure that Binarize
gives all 3 big objects with a bit of space around them.
The same result can be obtained using ImageTake
but there is a disadvantage: A call to ImageReflect
and some reverse and transposing is necessary because ImageTake
works on image matrix coordinates while ComponentMeasures
gives you a reversed but more natural coordinate system. Think of it as follows: if you take the 1st image matrix row you get the top row because this comes first but if you think of the usual Cartesian system, the y=1
would be at the bottom of the image.
ImageTake[img, Sequence @@ Reverse[Transpose[#2]]] & @@@
ComponentMeasurements[MorphologicalComponents[
Binarize[GaussianFilter[ImageReflect@img, 3]]], "BoundingBox"]