Function Reference
— Function File: warped = imperspectivewarp(im, P, interp, bbox, extrapval)
— Function File: [ warped, valid] = imperspectivewarp(...)

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 resulting image warped is computed using an interpolation method that can be selected through the interp argument. This must be one of the following strings

"nearest"
Nearest neighbor interpolation.
"linear"
"bilinear"
Bilinear interpolation. This is the default behavior.
"cubic"
"bicubic"
Bicubic interpolation.

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 is keept.

All values of the result that fall outside the original image will be set to extrapval. For images of class double extrapval defaults to NA and for other classes it defaults to 0.

The optional output valid is a matrix of the same size as warped that contains the value 1 in pixels where warped contains an interpolated value, and 0 in pixels where warped contains an extrapolated value.

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);
 figure(), imshow(warped);

Produces the following output

warning: imshow: pixels with NaN or NA values are set to minimum pixel value

and the following figures

Figure 1 Figure 2