randomAffine2d
Description
creates antform
= randomAffine2daffinetform2d
object that performs an identity transformation.
specifies the type of affine transformation using name-value arguments.tform
= randomAffine2d(Name,Value
)
Examples
Rotate Image By Random Angle
Read and display an image.
I = imread("kobi.png"); imshow(I)
Create a 2-D affine transformation object that rotates images. TherandomAffine2d
function picks the rotation angle randomly from a continuous uniform distribution within the interval [35, 55] degrees.
tform1 = randomAffine2d(Rotation=[35 55]);
Rotate the image and display the result.
J = imwarp(I,tform1); imshow(J)
The transformation object,tform1
, rotates all images by the same amount. To rotate an image by a different randomly selected amount, create a new geometric transformation.
tform2 = randomAffine2d(Rotation=[-10 10]); J2 = imwarp(I,tform2); imshow(J2)
Randomly Rotate Image with Custom Rotation Range
Read and display an image.
I = imread("sherlock.jpg"); imshow(I)
Create a 2-D affine transformation object that rotates images. To select a rotation angle from a custom range, specify theRotation
name-value argument as a function handle. This example specifies a function calledmyrange
(defined at the end of the example) that selects an angle from within two disjoint intervals.
tform = randomAffine2d(Rotation=@myrange);
Rotate the image and display the result.
J = imwarp(I,tform); imshow(J)
Supporting Function
This example defines themyrange
function that first randomly selects one of two intervals (-10, 10) and (170, 190) with equal probability. Within the selected interval, the function returns a single random number from a uniform distribution.
functionangle = myrange()ifrandi([0 1],1) a = -10; b = 10;elsea = 170; b = 190;endangle = a + (b-a).*rand(1);end
Input Arguments
Name-Value Arguments
Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN
, whereName
is the argument name andValue
is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.
Example:tform = randomAffine2d(XReflection=true)
Before R2021a, use commas to separate each name and value, and encloseName
in quotes.
Example:tform = randomAffine2d("XReflection",true)
XReflection
—Random horizontal reflection
false
(default) |true
Random horizontal reflection, specified asfalse
ortrue
. WhenXReflection
istrue
(1
), the transformationtform
reflects images horizontally with 50% probability. By default, the transformation does not reflect images in the horizontal direction.
YReflection
—Random vertical reflection
false
(default) |true
Random vertical reflection, specified asfalse
ortrue
. WhenYReflection
istrue
(1
), the transformationtform
reflects images vertically with 50% probability. By default, the transformation does not reflect images in the vertical direction.
Rotation
—Range of rotation
[0 0]
(default) |2-element numeric vector|function handle
Range of rotation, in degrees, applied to the input image, specified as one of the following.
A 2-element numeric vector. The second element must be larger than or equal to the first element. The rotation angle is picked randomly from a continuous uniform distribution within the specified interval.
一个处理函数。函数必须接受任何输入ut arguments and return the rotation angle as a numeric scalar. Use a function handle to pick rotation angles from a disjoint interval or using a nonuniform probability distribution. For more information about function handles, seeCreate Function Handle.
By default, the transformationtform
does not rotate images.
Example:[-45 45]
Scale
—Range of uniform scaling
[1 1]
(default) |2-element numeric vector|function handle
Range of uniform (isotropic) scaling applied to the input image, specified as one of the following.
A 2-element numeric vector. The second element must be larger than or equal to the first element. The scale factor is picked randomly from a continuous uniform distribution within the specified interval.
一个处理函数。函数必须接受任何输入ut arguments and return the scale factor as a numeric scalar. Use a function handle to pick scale factors from a disjoint interval or using a nonuniform probability distribution. For more information about function handles, seeCreate Function Handle.
By default, the transformationtform
does not scale images.
Example:[0.5 4]
XShear
—Range of horizontal shear
[0 0]
(default) |2-element numeric vector|function handle
Range of horizontal shear applied to the input image, specified as one of the following. Shear is measured as an angle in degrees, and is in the range (–90, 90).
A 2-element numeric vector. The second element must be larger than or equal to the first element. The horizontal shear angle is picked randomly from a continuous uniform distribution within the specified interval.
一个处理函数。函数必须接受任何输入ut arguments and return the horizontal shear angle as a numeric scalar. Use a function handle to pick horizontal shear angles from a disjoint interval or using a nonuniform probability distribution. For more information about function handles, seeCreate Function Handle.
By default, the transformationtform
does not shear images in the horizontal direction.
Example:[0 45]
YShear
—Range of vertical shear
[0 0]
(default) |2-element numeric vector|function handle
Range of vertical shear applied to the input image, specified as one of the following. Shear is measured as an angle in degrees, and is in the range (–90, 90).
A 2-element numeric vector. The second element must be larger than or equal to the first element. The vertical shear angle is picked randomly from a continuous uniform distribution within the specified interval.
一个处理函数。函数必须接受任何输入ut arguments and return the vertical shear angle as a numeric scalar. Use a function handle to pick vertical shear angles from a disjoint interval or using a nonuniform probability distribution. For more information about function handles, seeCreate Function Handle.
By default, the transformationtform
does not shear images in the vertical direction.
Example:[0 45]
XTranslation
—Range of horizontal translation
[0 0]
(default) |2-element numeric vector|function handle
Range of horizontal translation applied to the input image, specified as one of the following. Translation distance is measured in pixels.
A 2-element numeric vector. The second element must be larger than or equal to the first element. The horizontal translation distance is picked randomly from a continuous uniform distribution within the specified interval.
一个处理函数。函数必须接受任何输入ut arguments and return the horizontal translation distance as a numeric scalar. Use a function handle to pick horizontal translation distances from a disjoint interval or using a nonuniform probability distribution. For more information about function handles, seeCreate Function Handle.
By default, the transformationtform
does not translate images in the horizontal direction.
Example:[-5 5]
YTranslation
—Range of vertical translation
[0 0]
(default) |2-element numeric vector|function handle
Range of vertical translation applied to the input image, specified as one of the following. Translation distance is measured in pixels.
A 2-element numeric vector. The second element must be larger than or equal to the first element. The vertical translation distance is picked randomly from a continuous uniform distribution within the specified interval.
一个处理函数。函数必须接受任何输入ut arguments and return the vertical translation distance as a numeric scalar. Use a function handle to pick vertical translation distances from a disjoint interval or using a nonuniform probability distribution. For more information about function handles, seeCreate Function Handle.
By default, the transformationtform
不翻译图像在垂直长长吗n.
Example:[-5 5]
Output Arguments
tform
— Affine transformation
affinetform2d
object
Affine transformation, returned as anaffinetform2d
object.
Version History
Introduced in R2019bR2022b:Returnstform
asaffinetform2d
object
Starting in R2022b, most Image Processing Toolbox™ functions create and perform geometric transformations using the premultiply convention. Accordingly, therandomAffine2d
function now returns the output argumenttform
as anaffinetform2d
object, which supports the premultiply convention. Before, the function returnedtform
as anaffine2d
object, which supports the postmultiply convention. For more information, seeMigrate Geometric Transformations to Premultiply Convention.
See Also
imwarp
|randomWindow2d
|centerCropWindow2d
Topics
- Augment Images for Deep Learning Workflows(Deep Learning Toolbox)
Apri esempio
如果dispone di una versione modificata di questo esempio. Desideri aprire questo esempio con le tue modifiche?
Comando MATLAB
Hai fatto clic su un collegamento che corrisponde a questo comando MATLAB:
Esegui il comando inserendolo nella finestra di comando MATLAB. I browser web non supportano i comandi MATLAB.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select:.
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina(Español)
- Canada(English)
- United States(English)
Europe
- Belgium(English)
- Denmark(English)
- Deutschland(Deutsch)
- España(Español)
- Finland(English)
- France(Français)
- Ireland(English)
- Italia(Italiano)
- Luxembourg(English)
- Netherlands(English)
- Norway(English)
- Österreich(Deutsch)
- Portugal(English)
- Sweden(English)
- Switzerland
- United Kingdom(English)