Main Content

randomWindow2d

Randomly select rectangular region in image

Description

example

win= randomWindow2d(inputSize,targetSize)selects a rectangular region of sizetargetSizefrom a random position in an image of sizeinputSize.

example

win= randomWindow2d(inputSize,'Scale',scale,'DimensionRatio',dimensionRatio)selects a rectangular region, specifying the size of the region relative to the input image,scale, and the aspect ratio of the region,dimensionRatio.

Examples

collapse all

Read and display an image.

I = imread("flamingos.jpg"); imshow(I)

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

Specify the size of the input image and the target size of the rectangular region.

inputSize = size(I); targetSize = [40 60];

Select a region of the target size from a random location in the image.

rect = randomWindow2d(inputSize,targetSize);

Convert the region from aRectangleobject to a 4-element vector of the form [xmin ymin width height].

rectXYWH = [rect.XLimits(1) rect.YLimits(1)...diff(rect.XLimits)+1 diff(rect.YLimits)+1];

Display the boundary of the rectangular region overlaid on the original image.

annotatedI = insertShape(I,"Rectangle",rectXYWH,"LineWidth",3); imshow(annotatedI)

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

Read and display an image.

I = imread("strawberries.jpg"); imshow(I)

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

Specify the size of the input image.

inputSize = size(I);

Specify a fractional area of the region between 2% and 13% of the area of the input image. Specify a range of aspect ratios between 1:5 and 4:3.

scale = [0.02 0.13]; dimensionRatio = [1 5;4 3];

Specify a region with a randomly selected fractional area and aspect ratio from a random location in the image.

rect = randomWindow2d(inputSize,"Scale",scale,"DimensionRatio",dimensionRatio);

Crop the original image to the randomly selected region and display the result.

Icrop = imcrop(I,rect); imshow(Icrop)

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 3-element vector of positive integers of the form[height width channels]

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 3-element vector of positive integers of the form[height width channels]

Region area as a fraction of the input image area, specified as one of these values.

  • 2-element nondecreasing numeric vector with values in the range [0, 1]. The elements define a minimum and maximum fractional area of the region, respectively.randomWindow2d选择一个随机值范围内使用the fractional region area. To use a fixed region area, specify the same value for both elements.

  • Function handle. The function must take no input arguments and return one number in the range [0, 1] specifying a valid fractional region area. For more information about function handles, seeCreate Function Handle.

Range of aspect ratios of the rectangular region, specified as one of these values.

  • 2-by-2 matrix of positive numbers. The first row defines the defines the minimum aspect ratio and the second row defines the maximum aspect ratio.randomWindow2d选择一个随机值范围内使用the aspect ratio. To use a fixed aspect ratio, specify identical values for the first and second rows.

  • Function handle. The function must take no input arguments and return one positive number specifying a valid dimension ratio. For example, a value of1.2specifies a 5:4 aspect ratio. For more information about function handles, seeCreate Function Handle.

Example:[1 8;1 4]selects an aspect ratio in the range 1:8 to 1:4

Output Arguments

collapse all

Rectangular window, returned as aRectangleobject.

版本历史

Introduced in R2021a