Main Content

imcrop

Description

Crop Image Interactively

Note

The interactive syntaxes do not support categorical images. For categorical images, you must specify the crop region,rect.

Icropped= imcropcreates an interactive Crop Image tool associated with the grayscale, truecolor, or binary image displayed in the current figure.imcropreturns the cropped image,Icropped.

With this syntax and the other interactive syntaxes, the Crop Image tool blocks the MATLAB®command line until you complete the operation. For more information about using the Crop Image tool, seeInteractive Behavior.

example

Icropped= imcrop(I)displays the grayscale, truecolor, or binary imageIin a figure window and creates an interactive Crop Image tool associated with the image.

Xcropped= imcrop(X,cmap)displays the indexed imageXin a figure using the colormapcmap, and creates an interactive Crop Image tool associated with that image.imcropreturns the cropped indexed image,Xcropped, which also has the colormapcmap.

___= imcrop(h)creates an interactive Crop Image tool associated with the image specified by the handleh.

Crop Image by Specifying Crop Region

example

Icropped= imcrop(I,rect)crops the imageIaccording to the position and dimensions specified in the crop rectanglerect. The cropped image includes all pixels in the input image that are completelyor partiallyenclosed by the rectangle.

The actual size of the output image does not always correspond exactly with the width and height specified byrect. For example, supposerectis[20 20 40 30], using the default spatial coordinate system. The upper left corner of the specified rectangle is the center of the pixel with spatial(x,y)coordinates (20,20). The lower right corner of the rectangle is the center of the pixel with spatial(x,y)coordinates (60,50). The resulting output image has size 31-by-41 pixels, not 30-by-40 pixels.

example

Xcropped= imcrop(X,cmap,rect)crops the indexed imageXwith colormapcmapaccording to the position and dimensions specified in the crop rectanglerect.imcropreturns the cropped indexed image,Xcropped, which also has the colormapcmap.

___= imcrop(xref,yref,___)crops the input image using the world coordinate system defined byxrefandyref. After thexrefandyrefinput arguments, you can specify the arguments of any syntax that includes an input imageIorX.

Specify Additional Output Options

example

[___,rectout] = imcrop(___)also returns the position of the crop rectangle inrectout. You can use the input arguments of any other syntax.

[xrefout,yrefout,___] = imcrop(___)还返回输入的图像限制image inxrefoutandyrefout.

imcrop(___)without output arguments displays the cropped image in a new figure window. This syntax does not support categorical images.

Examples

collapse all

Read image into the workspace.

I = imread('llama.jpg');

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

[J,rect] = imcrop(I);

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

使用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 cropped image appears in the figure window.

The Crop Image tool returns the cropped area in the return variable,J. The variablerectis the four-element position vector describing the crop rectangle you specified. Get information about the returned variables.

whos
Name Size Bytes Class Attributes I 876x1314x3 3453192 uint8 J 413x483x3 598437 uint8 rect 1x4 32 double

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')

Figure contains 2 axes objects. Axes object 1 with title Original Image contains an object of type image. Axes object 2 with title Cropped Image contains an object of type image.

Read and display an image.

I = imread('parkavenue.jpg'); imshow(I)

Figure contains an axes object. The axes object contains an object of type image.

Specify a target window size as a two-element vector of the form [width,height].

targetSize = [300 600];

Create a矩形object that specifies the spatial extent of the crop window.

r = centerCropWindow2d(size(I),targetSize);

Crop the image to the spatial extents. Display the cropped region.

J = imcrop(I,r); imshow(J)

Figure contains an axes object. The axes object contains an object of type image.

Load indexed image with its associated map into the workspace.

loadtrees

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')

Figure contains 2 axes objects. Axes object 1 with title Original Image contains an object of type image. Axes object 2 with title Cropped Image contains an object of type image.

Input Arguments

collapse all

Image to be cropped, specified as one of the following.

  • m-by-nnumeric matrix representing a grayscale image

  • m-by-n-by-3 numeric array representing a truecolor image

  • m-by-nlogical matrix representing a binary mask.

  • m-by-ncategorical matrix representing a label image.

    Note

    For categorical input, you must specify a crop rectangle,rect. The interactive syntaxes do not support categorical input.

When you use an interactive syntax,imcropcalls theimshowfunction and accepts whatever image classesimshowaccepts.

Data Types:single|double|int8|int16|uint8|uint16|logical|categorical

Indexed image to be cropped, specified as a matrix of integers.

Data Types:single|double|int8|int16|uint8|uint16|logical

Colormap associated with the indexed imageX, specified as ac-by-3 numeric matrix. Each row is a three-element RGB triplet that specifies the red, green, and blue components of a single color of the colormap. Values with data typesingleordoublemust be in the range [0, 1].

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

Size and position of the crop rectangle in spatial coordinates, specified as a 4-element numeric vector of the form[xmin ymin width height]or animages.spatialref.Rectangleobject.

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

Input image, specified as a handle to a figure, axes, uipanel, or image graphics object. Ifhis an axes or figure handle, thenimcropuses the first image returned byfindobj(H,'Type','image').

Image limits in world coordinates along thex-dimension, specified as a 2-element numeric vector of the form [xminxmax] wherexmaxis greater thanxmin. The value ofxrefsets the imageXData.

Image limits in world coordinates along they-dimension, specified as a 2-element numeric vector of the form [yminymax] whereymaxis greater thanymin. The value ofyrefsets the imageYData.

Output Arguments

collapse all

Cropped image, returned as a numeric array, numeric matrix, logical matrix, or categorical matrix.

  • If you specify an input imageI, then the output image has the same data type as the input image.

  • If you do not specify an input image, then the output image generally has the same data type as the input image. However, if the input image has data typeint16orsingle, then the output image has data typedouble.

Cropped indexed image, returned as a numeric matrix.

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

Image limits in world coordinates along thex-dimension, returned as a 2-element numeric vector of the form [xminxmax]. If you specify image limits in a world coordinate system usingxref, thenxrefoutis equal toxref. Otherwise,xrefoutis equal to the original imageXData.

Image limits in world coordinates along they-dimension, returned as a 2-element numeric vector of the form [yminymax]. If you specify image limits in a world coordinate system usingyin, thenyrefoutis equal toyin. Otherwise,yrefoutis equal to the original imageYData.

More About

collapse all

Interactive Behavior

The Crop Image tool is a moveable, resizeable rectangle that you can position over the image and perform the crop operation interactively using the mouse.

When the Crop Image tool is active in a figure, the pointer changes to cross hairs当你把它移到目标图像。使用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. You can also chooseCrop Imagefrom the context menu. The figure illustrates the Crop Image tool with the context menu displayed.

Interactive Behavior Description
删除the Crop Image tool.

PressBackspace,Escapeor删除, or right-click inside the crop rectangle and selectCancelfrom the context menu.

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

Resize 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.
Move 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.
Change the color used to display the crop rectangle. 钻井平台ht-click inside the boundary of the crop rectangle and selectSet Colorfrom the context menu.
Crop the image.

Double-click the left mouse button or right-click inside the boundary of the crop rectangle and selectCrop Imagefrom the context menu.

Retrieve the coordinates of the crop rectangle. 钻井平台ht-click inside the boundary of the crop rectangle and selectCopy Positionfrom the context menu.imcrop册第4单元位置向量([xmin ymin width height]) to the clipboard.

Extended Capabilities

Version History

Introduced before R2006a