Skip to main content
deleted 109 characters in body
Source Link
halirutan
  • 113.4k
  • 7
  • 266
  • 479

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"]

Mathematica graphics

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"]

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 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"]

Mathematica graphics

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"]

Edit: Shorter ImageTrim version as pointed out by Matthias Odisio

Use MorphologicalComponents 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"]

Mathematica graphics

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"]
added 470 characters in body
Source Link
halirutan
  • 113.4k
  • 7
  • 266
  • 479

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 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";

ImageTake[img,(* SequenceImageTrim @@version Reverse[Transpose[#2]]]*)
ImageTrim[img, #2] & @@@ 
 ComponentMeasurements[MorphologicalComponents[ComponentMeasurements[
   Binarize[GaussianFilter[ImageReflect@imgMorphologicalComponents[Binarize[GaussianFilter[img, 3]]], "BoundingBox"]

Mathematica graphics

The GaussianFilter just smooths the image a bit to ensure that Binarize gives all 3 big objects with a bit of space around them. And the mysterious

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"]

Belisarius gave you already the important hint: Use MorphologicalComponents. There are two points I would make differently, which is I would utilize ComponentMeasures to extract the "BoundingBox" which is already the row- and column-number you can then feed directly to ImageTake

img = Import@"https://i.sstatic.net/8enYZ.png";

ImageTake[img, Sequence @@ Reverse[Transpose[#2]]] & @@@ 
 ComponentMeasurements[MorphologicalComponents[
   Binarize[GaussianFilter[ImageReflect@img, 3]]], "BoundingBox"]

Mathematica graphics

The GaussianFilter just smooths the image a bit to ensure that Binarize gives all 3 big objects with a bit of space around them. And the mysterious ImageReflect 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.

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 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"]

Mathematica graphics

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"]
Source Link
halirutan
  • 113.4k
  • 7
  • 266
  • 479

Belisarius gave you already the important hint: Use MorphologicalComponents. There are two points I would make differently, which is I would utilize ComponentMeasures to extract the "BoundingBox" which is already the row- and column-number you can then feed directly to ImageTake

img = Import@"https://i.sstatic.net/8enYZ.png";

ImageTake[img, Sequence @@ Reverse[Transpose[#2]]] & @@@ 
 ComponentMeasurements[MorphologicalComponents[
   Binarize[GaussianFilter[ImageReflect@img, 3]]], "BoundingBox"]

Mathematica graphics

The GaussianFilter just smooths the image a bit to ensure that Binarize gives all 3 big objects with a bit of space around them. And the mysterious ImageReflect 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.