imcrop() - Image Processing
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.
exampleI2 = 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.
exampleI2 = 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.
exampleX2 = 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.
Syntax
I2 = imcropI2 = imcrop(I) exampleX2 = imcrop(X,map)___ = imcrop(h)I2 = imcrop(I,rect) exampleX2 = imcrop(X,map,rect) example___ = imcrop(XData,YData,___)[___,rect2] = imcrop(___) example[XData2,YData2,___]
= imcrop(___)
Example
Crop Image Using Crop Image Interactive ToolRead 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.whosName Size Bytes Class Attributes
I 256x256 65536 uint8
I2 121x126 15246 uint8
rect 1x4 32 double Crop Image By Specifying Crop RectangleOpen This Example
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 RectangleOpen This Example
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')
Output / Return Value
Limitations
Alternatives / See Also
Reference