Function File: warped = imperspectivewarp(im, P, interp, bbox, extrapval)

Applies the spatial perspective homogeneous transformation P to the image im. The transformation matrix P must be a 3x3 homogeneous matrix, or 2x2 or 2x3 affine transformation matrix.

The optional argument method defines the interpolation method to be used. All methods supported by interp2 can be used. By default, the linear method is used.

For MATLAB compatibility, the methods bicubic (same as cubic), bilinear and triangle (both the same as linear) are also supported.

By default the resulting image contains the entire warped image. In some situation you only parts of the warped image. The argument bbox controls this, and can be one of the following strings

"loose"

The entire warped result is returned. This is the default behavior.

"crop"

The central part of the image of the same size as the input image is returned.

"same"

The size and coordinate system, of the input image, are kept.

All values of the result that fall outside the original image will be set to extrapval. The default value of extrapval is 0.

See also: imremap, imrotate, imresize, imshear, interp2.

Demonstration 1

The following code

 ## Generate a synthetic image and show it
 I = tril(ones(100)) + abs(rand(100)); I(I>1) = 1;
 I(20:30, 20:30) = !I(20:30, 20:30);
 I(70:80, 70:80) = !I(70:80, 70:80);
 figure(), imshow(I);
 ## Resize the image to the double size and show it
 P = diag([1, 1, 0.5]);
 warped = imperspectivewarp(I, P);
 figure(), imshow(warped);

Produces the following figures

Figure 1 Figure 2

Demonstration 2

The following code

 ## Generate a synthetic image and show it
 I = tril(ones(100)) + abs(rand(100)); I(I>1) = 1;
 I(20:30, 20:30) = !I(20:30, 20:30);
 I(70:80, 70:80) = !I(70:80, 70:80);
 figure(), imshow(I);
 ## Rotate the image around (0, 0) by -0.4 radians and show it
 R = [cos(-0.4) sin(-0.4); -sin(-0.4) cos(-0.4)];
 warped = imperspectivewarp(I, R, :, :, 0);
 figure(), imshow(warped);

Produces the following figures

Figure 1 Figure 2

Package: image