ImageTrim

ImageTrim[image,roi]

gives the smallest subimage of image that includes the specified region of interest roi.

ImageTrim[image,roi,r]

adds a margin of size r back to the resulting image.

ImageTrim[image,{roi1,roi2,},]

extracts multiple subimages specified by roii from image.

Details and Options

  • Image trimming, also known as image cropping, is the process of removing border pixels from an image and is typically used for extracting regions of interest.
  • ImageTrim[image,roi] returns a subimage containing the region of interest roi. The region roi can be any of the following:
  • {{x1,y1},}or {{x1,y1,z1},}a list of 2D or 3D image positions
    regiona 2D or 3D geometric region (Point, Disk, Sphere, etc.)
    maskimagenonzero pixels of a 2D or 3D object
    fa region returned by a function f
  • ImageTrim assumes the standard image coordinate system.
  • For integer coordinate values the trimmed image will include all pixels touching the point . Coordinates lie at the center of pixels. »
  • ImageTrim[image,{pt1,},{rx,ry}] specifies different horizontal and vertical margins.
  • The following options can be given:
  • DataRangeFullrange of pixel positions in image
    PaddingNonepadding scheme to use
  • By default, if fewer elements exist than are requested, ImageTrim will just return the elements that are present. By setting the Padding option to anything else, pixels outside the original image can also be included. »
  • Possible settings for DataRange include:
  • Automatic{{0,1},{0,h/w}} in 2D, {{0,1},{0,d/w},{0,h/w}} in 3D
    Full{{0,w},{0,h}} in 2D, {{0,1},{0,d},{0,h}} in 3D (default)
    {{left,right},{bottom,top}}explicit coordinate ranges in 2D
    {{left,right},{front,back},{bottom,top}}explicit coordinate ranges in 3D

Examples

open allclose all

Basic Examples  (1)

Trim an image:

Scope  (5)

Data  (2)

Trim a 2D image:

Trim a 3D image:

Region of Interest  (2)

Extract a single-pixel image:

Extract an image containing a list of positions:

Specify the region of interest using a graphics primitive:

Extract an image containing the nonzero pixels of a reference image:

Define the region of interest using a function:

Trim multiple regions of interest:

Extract three slices from a 3D image:

Trim multiple regions of interest:

Margin  (1)

By default, no extra margin is added to the trimmed regions:

This is equivalent to zero margin:

Use a larger margin:

Options  (2)

DataRange  (1)

Remove pixels from each side using the standard coordinate system:

This is equivalent to DataRange->Full:

Remove 25% of pixels from each side using a scaled coordinate system:

Padding  (1)

By default, the returned subimage is not padded to include coordinates outside of an image:

Pad the subimage to include all specified coordinates:

Applications  (7)

Trim faces detected from an image:

Trim lines of detected text from an image:

Trim the detected barcode:

Trim the area around the maximum:

Trim around an object of a specific color:

Trim areas that contain no keypoints:

Add some margins around keypoints:

Trim a 3D image to a detected region of interest:

Find the region of interest:

Trim the volume around it:

Mask the image with the region of interest before trimming:

Properties & Relations  (2)

Half-integer coordinates represent the center of the pixels and return the specific pixel:

Integer coordinates yield an image including all neighboring pixels:

Show the two pixel positions on the image:

Trimming with multiple coordinates is the same as using their bounds:

Compute the coordinate bounds:

Introduced in 2010
 (8.0)
 |
Updated in 2012
 (9.0)
2018
 (11.3)