Crop an Image
Note
You can also crop an image interactively using the Image Viewer app. For details, see Crop Image Using Image Viewer App.
To extract a rectangular portion of an image, use the imcrop
function. Using imcrop
, you can specify the crop
region interactively using the mouse or programmatically by specifying the size and position
of the crop region.
This example illustrates an interactive syntax. The example reads an image into the
MATLAB® workspace and calls imcrop
specifying the image as an
argument. imcrop
displays the image in a figure window and waits for you to
draw the crop rectangle on the image. When you move the pointer over the image, the shape of
the pointer changes to cross hairs . Click and drag the pointer to specify the size and position
of the crop rectangle. You can move and adjust the size of the crop rectangle using the mouse.
When you are satisfied with the crop rectangle, double-click to perform the crop operation, or
right-click inside the crop rectangle and select Crop Image from the
context menu. imcrop
returns the cropped image in J
.
I = imread("circuit.tif")
J = imcrop(I);
You can also specify the size and position of the crop rectangle as parameters when you
call imcrop
. Specify the crop rectangle as a four-element position vector,
[xmin ymin width height]
.
In this example, you call imcrop
specifying the image to crop,
I
, and the crop rectangle. imcrop
returns the cropped
image in J
.
I = imread("circuit.tif");
J = imcrop(I,[60 40 100 90]);