|On this page…|
To perform a general 2-D geometric transformation:
Define the parameters of the geometric transformation, creating a geometric transformation object. The toolbox defines several geometric transformation objects for different types of transformations, such as affine and projective.
Perform the transformation. To do this, you pass the image to be transformed and the geometric transformation object to the imwarp function.
The following figure illustrates this process.
Before you can perform a geometric transformation, you must first define the parameters of the transformation in a geometric transformation object. The following sections describe two ways you can do this:
If you know the transformation matrix for the geometric transformation you want to perform, you can create one of the geometric transformation objects directly, passing the transformation matrix as a parameter.
For example, you can use a 3-by-3 matrix to specify any of the affine transformations. For affine transformations, the last column must contain 0 0 1 ([zeros(N,1); 1]). The following table lists affine transformations with the transformation matrix used to define them. You can combine multiple affine transformations into a single matrix.
tx specifies the displacement along the x axis
ty specifies the displacement along the y axis.
sx specifies the scale factor along the x axis
sy specifies the scale factor along the y axis.
shx specifies the shear factor along the x axis
shy specifies the shear factor along the y axis.
q specifies the angle of rotation.
The following example defines the transformation matrix for a translation and creates an affine2d geometric transformation object.
xform = [ 1 0 0 0 1 0 40 40 1 ]; tform_translate = affine2d(xform);
tform_translate = affine2d with properties: T: [3x3 double] Dimensionality: 2
You can create a geometric transformation object by passing two sets of control point pairs to the fitgeotrans function. The fitgeotrans function estimates the transformation from these points and returns one of the geometric transformation objects.
Different transformations require a varying number of points. For example, affine transformations require three non-collinear points in each image (a triangle) and projective transformations require four points (a quadrilateral).
This example passes two sets of control points to fitgeotrans, which returns an affine geometric transformation object.
movingPoints = [11 11;21 11; 21 21]; fixedPoints = [51 51;61 51;61 61]; tform = fitgeotrans(movingPoints,fixedPoints,'affine')
tform = affine2d with properties: T: [3x3 double] Dimensionality: 2
Once you specify the transformation in a geometric transformation object, you can perform the transformation by calling the imwarp function, passing it the image to be transformed and a geometric transformation object. The imwarp function performs the specified transformation on the coordinates of the input image and creates an output image.
For example, to perform a transformation of the checkerboard image using the geometric transformation object created in Define the Geometric Transformation, you would call imwarp this way:
cb_trans = imwarp(cb, tform_translate);
imwarp determines the value of pixels in the output image by mapping the new locations back to the corresponding locations in the input image (inverse mapping). imwarp interpolates within the input image to compute the output pixel value. See imwarp for more information about supported interpolation methods.
The following figure illustrates a translation transformation. By convention, the axes in input space are labeled u and v and the axes in output space are labelled x and y. In the figure, note how imwarp modifies the spatial coordinates that define the locations of pixels in the input image. The pixel at (1,1) is now positioned at (41,41). In the checkerboard image, each black, white, and gray square is 10 pixels high and 10 pixels wide. (For more information about the distinction between spatial coordinates and pixel coordinates, see Expressing Image Locations. )
Input Image Translated
When you perform a transformation, there are often pixels in the output image that are not part of the original input image. These pixels must be assigned some value, called a fill value. By default, imwarp sets these pixels to zero and they are displayed as black. Using the FillValues parameter with the imwarp function, you can specify a different color.
If the image being transformed is a grayscale image, you must specify a scalar value that specifies a shade of gray.
For example, in , where the example displays the translated checkerboard image in relation to the original coordinate space, you can specify a fill value that matches the color of the gray squares, rather than the default black color.
cb_fill = imwarp(cb, tform_translate,'FillValues', .7 ); figure, imshow(cb_fill)
Translated Image with Gray Fill Value
If the image being transformed is an RGB image, you can use either a scalar value or a 1-by-3 vector. If you specify a scalar, imwarp uses that shade of gray for each plane of the RGB image. If you specify a 1-by-3 vector, imwarp interprets the values as an RGB color value.
For example, this code translates an RGB image, specifying an RGB color value as the fill value. The example specifies one of the light green values in the image as the fill value.
rgb = imread('onion.png'); xform = [ 1 0 0 0 1 0 40 40 1 ] tform_translate = affine2d(xform); cb_rgb = imwarp(rgb,tform_translate,'FillValues',[187;192;57]); figure, imshow(cb_rgb)
Translated RGB Image with Color Fill Value
If you are transforming multiple RGB images, you can specify different fill values for each RGB image. For example, if you want to transform a series of 10 RGB images, a 4-D array with dimensions 200-by-200-by-3-by-10, you can specify a 3-by-10 array containing RGB color values for each plane.