Main Content

centerCropWindow2d

Create rectangular center cropping window

Description

example

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

Examples

collapse all

Read and display an image.

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

Figure contains an axes object. The 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 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 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芯片image.

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

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

To crop thekobiimage 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 axes object contains an object of type image.

Input Arguments

collapse all

Input image size, specified as one of the following.

Type of Input Image Format ofinputSize
2-D grayscale or binary image 2-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 Image Format oftargetSize
2-D grayscale or binary image 2-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 aRectangleobject.

版本嗨story

Introduced in R2019b