This example shows how to perform a simple affine transformation called a translation. In a translation, you shift an image in coordinate space by adding a specified value to the x- and y coordinates. (You can also use the imtranslate function to perform translation.)
Read the image to be transformed. This example creates a checkerboard image using the checkerboard function.
cb = checkerboard; imshow(cb)
Get spatial referencing information about the image. This information is useful when you want to display the result of the transformation.
Rcb = imref2d(size(cb))
Rcb = imref2d with properties: XWorldLimits: [0.5000 80.5000] YWorldLimits: [0.5000 80.5000] ImageSize: [80 80] PixelExtentInWorldX: 1 PixelExtentInWorldY: 1 ImageExtentInWorldX: 80 ImageExtentInWorldY: 80 XIntrinsicLimits: [0.5000 80.5000] YIntrinsicLimits: [0.5000 80.5000]
Create a geometric transformation object that defines the translation you want to perform. A translation transformation is a type of 2-D affine transformation so it uses the affine2d geometric transformation object. You can use a 3-by-3 transformation matrix to define the transformation. In this matrix, A(3,1) specifies the number of pixels to shift the image in the horizontal direction and A(3,2) specifies the number of pixels to shift the image in the vertical direction. Now create a geometric transformation object using the transformation matrix.
tform = affine2d([1 0 0; 0 1 0; 20 20 1]);
Perform the transformation. Call the imwarp function specifying the image you want to transform and the geometric transformation object. imwarp returns the transformed image. This example gets the optional spatial referencing object return value which contains information about the transformed image. View the original and the transformed image side-by-side using the subplot function in conjunction with imshow . When viewing the translated image, it might appear that the transformation had no effect. The transformed image looks identical to the original image. The reason that no change is apparent in the visualization is because imwarp sizes the output image to be just large enough to contain the entire transformed image but not the entire output coordinate space. Notice, however, that the coordinate values have been changed by the transformation.
[out,Rout] = imwarp(cb,tform); figure; subplot(1,2,1); imshow(cb,Rcb); subplot(1,2,2); imshow(out,Rout)
To see the entirety of the transformed image in the same relation to the origin of the coordinate space as the original image, use imwarp with the 'OutputView' parameter, specifying a spatial referencing object. The spatial referencing object specifies the size of the output image and how much of the output coordinate space is included in the output image. To do this, the example makes a copy of the spatial referencing object associated with the original image and modifies the world coordinate limits to accommodate the full size of the transformed image. The example sets the limits of the output image in world coordinates to include the origin from the input
Rout = Rcb; Rout.XWorldLimits(2) = Rout.XWorldLimits(2)+20; Rout.YWorldLimits(2) = Rout.YWorldLimits(2)+20; [out,Rout] = imwarp(cb,tform,'OutputView',Rout); figure, subplot(1,2,1); imshow(cb,Rcb); subplot(1,2,2); imshow(out,Rout)