Documentation

imcrop

Syntax

  • I2 = imcrop
  • I2 = imcrop(I)
    example
  • X2 = imcrop(X,map)
  • ___ = imcrop(h)
  • I2 = imcrop(I,rect)
    example
  • X2 = imcrop(X,map,rect)
    example
  • ___ = imcrop(XData,YData,___)
  • [___,rect2] = imcrop(___)
    example
  • [XData2,YData2,___] = imcrop(___)

Description

I2 = imcrop creates an interactive image cropping tool associated with the image displayed in the current figure, called the target image. The Crop Image tool is a moveable, resizable rectangle that you can position over the image and perform the crop operation interactively using the mouse. For more information about using the Crop Image tool, see Interactive Behavior. imcrop returns the cropped image, I2. With this syntax and the other interactive syntaxes, the Crop Image tool blocks the MATLAB® command line until you complete the operation.

example

I2 = imcrop(I) displays the image I in a figure window and creates a cropping tool associated with that image. I can be a grayscale image, a truecolor image, or a logical array. imcrop returns the cropped image, I2.

X2 = imcrop(X,map) displays the indexed image X in a figure using the colormap map, and creates a cropping tool associated with that image. After you crop the image, imcrop returns the cropped indexed image, X2.

___ = imcrop(h) creates a cropping tool associated with the image specified by handle h. h may be an image, axes, uipanel, or figure handle. If h is an axes, uipanel, or figure handle, the cropping tool acts on the first image found in the container object.

example

I2 = imcrop(I,rect) crops the image I. rect is a four-element position vector of the form [xmin ymin width height] that specifies the size and position of the crop rectangle. imcrop returns the cropped image, I2.

example

X2 = imcrop(X,map,rect) crops the indexed image X. map specifies the colormap used with X. rect is a four-element position vector [xmin ymin width height] that specifies the size and position of the cropping rectangle. imcrop returns the cropped indexed image, X2.

___ = imcrop(XData,YData,___) specifies a nondefault spatial coordinate system for the target image.

example

[___,rect2] = imcrop(___) returns the cropping rectangle in rect2, a four-element position vector, in addition to the cropped image.

[XData2,YData2,___] = imcrop(___) returns two-element vectors that specify the XData and YData of the target image.

Code Generation support: Yes.

MATLAB Function Block support: Yes.

Examples

collapse all

Crop Image Using Crop Image Interactive Tool

Read image into the workspace.

I = imread('cameraman.tif');

Open Crop Image tool associated with this image. Specify a variable in which to store the cropped image. The example includes the optional return value rect in which imcrop returns the four-element position vector of the rectangle you draw.

[I2, rect] = imcrop(I);

When you move the cursor over the image, it changes to a cross-hairs . The Crop Image tool blocks the MATLAB command line until you complete the operation.

Using the mouse, draw a rectangle over the portion of the image that you want to crop.

Perform the crop operation by double-clicking in the crop rectangle or selecting Crop Image on the context menu.

The Crop Image tool returns the cropped area in the return variable, I2. The variable rect is the four-element position vector describing the crop rectangle you specified.

whos
Name        Size             Bytes  Class    Attributes

  I         256x256            65536  uint8              
  I2        121x126            15246  uint8          
  rect        1x4                 32  double          

Crop Image By Specifying Crop Rectangle

Read image into the workspace.

I = imread('circuit.tif');

Crop image, specifying crop rectangle.

I2 = imcrop(I,[75 68 130 112]);

Display original image and cropped image.

subplot(1,2,1)
imshow(I)
title('Original Image')
subplot(1,2,2)
imshow(I2)
title('Cropped Image')

Crop Indexed Image Specifying Crop Rectangle

Load indexed image with its associated map into the workspace.

load trees

Crop indexed image, specifying crop rectangle.

X2 = imcrop(X,map,[30 30 50 75]);

Display original image and cropped image.

subplot(1,2,1)
imshow(X,map)
title('Original Image')
subplot(1,2,2)
imshow(X2,map)
title('Cropped Image')

Input Arguments

collapse all

I — Image to be croppedreal, nonsparse, numeric array

Image to be cropped, specified as a real, nonsparse, numeric array. With the syntaxes where you specify rect as an input argument, the input image can be logical or numeric, and must be real and nonsparse. With the other syntaxes,imcrop calls imshow and accepts whatever image classes imshow accepts. I can be logical, uint8, uint16, int16, single, or double. A truecolor image can be uint8, int16, uint16, single, or double.

Example: I = imread('cameraman.tif'); I2 = imcrop(I);

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical

X — Indexed image to be croppedreal, nonsparse numeric array

Indexed image to be cropped, specified as a real, nonsparse numeric array. With the syntaxes where you specify rect as an input argument, the input image can be logical or numeric, and must be real and nonsparse. With the other syntaxes,imcrop calls imshow and accepts any image class that. imshow accepts. For an indexed image, X can be logical, uint8, uint16, single, or double.

Example: load trees; X2 = imcrop(X,map);

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical

map — Color map associated with indexed imagem-by-3 numeric array

Color map associated with indexed image, specified as an m-by-3 numeric array.

Example: load trees; X2 = imcrop(X,map);

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical

rect — Size and position of the crop rectanglefour-element position vector

Size and position of the crop rectangle, specified as a four-element position vector of the form [xmin ymin width height].

Example: I2 = imcrop(I,[20 20 40 90]);

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical

h — Handle to image to be croppedhandle to an image, axes, uipanel, or figure

Handle to image to be cropped, specified as a handle to an image, axes, uipanel, or figure.

Example: I = imread('cameraman.tif'); h = imshow(I); I2 = imcrop(h);

Data Types: double

XData — X limits of nondefault coordinate systemtwo-element vector

X limits of nondefault coordinate system, specified as a two-element vector.

Example: I2 = imcrop([10 256],[20 256],I);

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

YData — Y limits of nondefault coordinate systemtwo-element vector

Y limits of nondefault coordinate system, specified as a two-element vector.

Example: I2 = imcrop([10 256],[20 256],I);

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Output Arguments

collapse all

I2 — Cropped imagereal, nonsparse, numeric array

Cropped image, returned as a real, nonsparse, numeric array.

If you specify an input image, the output image has the same class as the input image.

If you don't specify an input image, i.e., you call imcrop with no input arguments or a handle, the output image has the same class as the input image except for int16 or single. If the input image is int16 or single, the output image is double.

X2 — Cropped indexed imagereal, nonsparse, numeric array

Cropped image, returned as a real, nonsparse, numeric array.

If you specified an input image, the output image has the same class as the input image. If you don't specify an image as an input argument, i.e., you call imcrop with no input arguments or a handle, the output image has the same class as the input image except for int16 or single. If the input image is int16 or single, the output image is double.

XData2 — X limits of nondefault coordinate systemtwo-element vector

X limits of nondefault coordinate system, returned as a two-element vector.

YData2 — Y limits of nondefault coordinate systemtwo-element vector

Y limits of nondefault coordinate system, returned as a two-element vector.

rect2 — Size and position of the crop rectanglefour-element position vector

Size and position of the crop rectangle, returned as a four-element position vector of the form [xmin ymin width height].

More About

expand all

Interactive Behavior

When the Crop Image tool is active in a figure, the pointer changes to cross hairs when you move it over the target image. Using the mouse, you specify the crop rectangle by clicking and dragging the mouse. You can move or resize the crop rectangle using the mouse. When you are finished sizing and positioning the crop rectangle, create the cropped image by double-clicking the left mouse button or by choosing Crop Image from the context menu. imcrop returns the cropped image, I2. The following figure illustrates the Crop Image tool with the context menu displayed. For more information about the interactive capabilities of the tool, see the table that follows.

Interactive BehaviorDescription
Deleting the Crop Image tool.

Press Backspace, Escape or Delete, or right-click inside the crop rectangle and select Cancel from the context menu.

Note: If you delete the ROI, the function returns empty values.

Resizing the Crop Image tool.Select any of the resize handles on the crop rectangle. The pointer changes to a double-headed arrow . Click and drag the mouse to resize the crop rectangle.
Moving the Crop Image tool.Move the pointer inside the boundary of the crop rectangle. The pointer changes to a fleur shape . Click and drag the mouse to move the rectangle over the image.
Changing the color used to display the crop rectangle.Right-click inside the boundary of the crop rectangle and select Set Color from the context menu.
Cropping the image.

Double-click the left mouse button or right-click inside the boundary of the crop rectangle and select Crop Image from the context menu.

Retrieving the coordinates of the crop rectangle.Right-click inside the boundary of the crop rectangle and select Copy Position from the context menu. imcrop copies a four-element position vector ([xmin ymin width height]) to the clipboard.

Code Generation

This function supports the generation of C code using MATLAB Coder™. For more information, see Code Generation for Image Processing.

When generating code, note the following:

  • The interactive syntaxes are not supported, including:

    • I2 = imcrop

    • I2 = imcrop(I)

    • X2 = imcrop(X,map)

    • I2 = imcrop(h)

  • Indexed image are not supported, including the noninteractive syntax X2 = imcrop(X,map,rect);

MATLAB Function Block

You can use this function in the MATLAB Function Block in Simulink.

Tips

  • Because rect is specified in terms of spatial coordinates, the width and height elements of rect do not always correspond exactly with the size of the output image. For example, suppose rect is [20 20 40 30], using the default spatial coordinate system. The upper-left corner of the specified rectangle is the center of the pixel (20,20) and the lower-right corner is the center of the pixel (50,60). The resulting output image is 31-by-41, not 30-by-40, because the output image includes all pixels in the input image that are completely or partially enclosed by the rectangle.

See Also

|

Introduced before R2006a

Was this topic helpful?