主要内容

randomWindow2d

Randomly select rectangular region in image

Description

example

= randomWindow2d(输入,targetSize)selects a rectangular region of sizetargetSizefrom a random position in an image of size输入.

example

= randomWindow2d(输入,'规模',规模,'DimensionRatio',DimensionRatio)selects a rectangular region, specifying the size of the region relative to the input image,规模, and the aspect ratio of the region,DimensionRatio.

Examples

collapse all

阅读并显示图像。

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);靶标= [40 60];

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

rect = randomWindow2d(inputSize,targetSize);

从一个区域转换Rectangleobject to a 4-element vector of the form [xmin ymin宽度高度]。

rectxywh = [rect.xlimits(1)rect.ylimits(1)...diff(rect.XLimits)+1 diff(rect.YLimits)+1];

显示在原始图像上覆盖的矩形区域的边界。

annotatedI = insertShape(I,"Rectangle",rectXYWH,“行宽”,3); imshow(annotatedI)

Figure contains an axes object. The axes object contains an object of type 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.

规模= [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

输入图像大小,指定为以下内容之一。

Type of Input Image 格式输入
2-D grayscale or binary image 正整数的2元素向量of the form[height width]
2-D RGB或多光谱图像 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 格式targetSize
2-D grayscale or binary image 正整数的2元素向量of the form[height width]
2-D RGB或多光谱图像 3-element vector of positive integers of the form[height width channels]

区域区域是输入图像区域的一部分,指定为这些值之一。

  • 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 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正数矩阵. The first row defines the defines the minimum aspect ratio and the second row defines the maximum aspect ratio.randomWindow2dselects a random value within the range to use as the aspect ratio. To use a fixed aspect ratio, specify identical values for the first and second rows.

  • 功能句柄。该函数必须不采用输入参数,并返回一个指定有效维度比率的正数。例如,一个值的值1.2指定5:4的纵横比。有关功能处理的更多信息,请参阅Create Function Handle.

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

Output Arguments

collapse all

Rectangular window, returned as aRectangle目的。

Introduced in R2021a