Main Content

centerCropWindow2d

Create rectangular center cropping window

Description

win = centerCropWindow2d(inputSize,targetSize) determines the window to crop from a 2-D input image of size inputSize such that the size of the cropped image is targetSize. The coordinates of the window are centered in the input image.

example

Examples

collapse all

Read and display an image.

chips = imread('coloredChips.png');
imshow(chips)

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

Specify the target size of the cropping window.

targetSize = [256 256];

Create a center crop window.

win1 = centerCropWindow2d(size(chips),targetSize);

Crop the original image using the center crop window.

B1 = imcrop(chips,win1);

Display the cropped image.

imshow(B1)

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

Read and display a second image of a different size.

kobi = imread('kobi.png');
imshow(kobi)

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

Try applying the center crop window to this image. The cropped region does not come from the center of the image because the center crop window uses the spatial extents of the chips image.

B2 = imcrop(kobi,win1);
imshow(B2)

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

To crop the kobi image from the center, specify a new center crop window.

win2 = centerCropWindow2d(size(kobi),targetSize);
B3 = imcrop(kobi,win2);
imshow(B3)

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

Input Arguments

collapse all

Input image size, specified as one of the following.

Type of Input ImageFormat of inputSize
2-D grayscale or binary image2-element vector of positive integers of the form [height width]
2-D RGB or multispectral image of size 3-element vector of positive integers of the form [height width channels]

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

Target image size, specified as one of the following.

Type of Target ImageFormat of targetSize
2-D grayscale or binary image2-element vector of positive integers of the form [height width]
2-D RGB or multispectral image of size 3-element vector of positive integers of the form [height width channels]

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

Output Arguments

collapse all

Cropping window, returned as a Rectangle object.

Version History

Introduced in R2019b