---
title: "ImagePerspectiveTransformation"
language: "en"
type: "Symbol"
summary: "ImagePerspectiveTransformation[image, m] applies a linear fractional transform specified by a matrix m to the positions of each pixel in image. ImagePerspectiveTransformation[image, tf] uses the TransformationFunction given by tf. ImagePerspectiveTransformation[image, ..., size] gives an image of the specified size. ImagePerspectiveTransformation[video, ...] transforms frames of a video."
keywords: 
- perspective transformation
- transformation
- translation
- rotation
- scaling
- affine
- image transformation
- geometric transformation
- spatial transformation
canonical_url: "https://reference.wolfram.com/language/ref/ImagePerspectiveTransformation.html"
source: "Wolfram Language Documentation"
related_guides: 
  - 
    title: "Geometric Operations"
    link: "https://reference.wolfram.com/language/guide/ImageGeometry.en.md"
  - 
    title: "Video Computation: Update History"
    link: "https://reference.wolfram.com/language/guide/VideoComputation-UpdateHistory.en.md"
  - 
    title: "Video Editing"
    link: "https://reference.wolfram.com/language/guide/VideoEditing.en.md"
  - 
    title: "Computational Photography"
    link: "https://reference.wolfram.com/language/guide/ComputationalPhotography.en.md"
related_functions: 
  - 
    title: "TransformationFunction"
    link: "https://reference.wolfram.com/language/ref/TransformationFunction.en.md"
  - 
    title: "FindGeometricTransform"
    link: "https://reference.wolfram.com/language/ref/FindGeometricTransform.en.md"
  - 
    title: "ImageForwardTransformation"
    link: "https://reference.wolfram.com/language/ref/ImageForwardTransformation.en.md"
  - 
    title: "ImageTransformation"
    link: "https://reference.wolfram.com/language/ref/ImageTransformation.en.md"
  - 
    title: "AffineTransform"
    link: "https://reference.wolfram.com/language/ref/AffineTransform.en.md"
---
# ImagePerspectiveTransformation

ImagePerspectiveTransformation[image, m] applies a linear fractional transform specified by a matrix m to the positions of each pixel in image.

ImagePerspectiveTransformation[image, tf] uses the TransformationFunction given by tf.

ImagePerspectiveTransformation[image, …, size] gives an image of the specified size.

ImagePerspectiveTransformation[video, …] transforms frames of a video.

## Details and Options

* ``ImagePerspectiveTransformation`` is typically used to modify camera position, orientation and field of view of a scene.

[image]

* The transformation matrix ``m`` corresponds to the following case:

|                 |                              |
| --------------- | ---------------------------- |
| image 2D, m 2×2 | AffineTransform[m]           |
| image 2D, m 3×3 | LinearFractionalTransform[m] |
| image 3D, m 3×3 | AffineTransform[m]           |
| image 3D, m 4×4 | LinearFractionalTransform[m] |

* Possible settings for ``size`` are:

|                        |                                        |
| ---------------------- | -------------------------------------- |
| Automatic              | automatic image size                   |
| All                    | same as input image size               |
| width                  | explicit width, automatic height       |
| {width, height}        | explicit width and height              |
| {width, depth, height} | explicit width, depth and height in 3D |

* ``ImagePerspectiveTransformation`` can take the following options:

|             |           |                                             |
| ----------- | --------- | ------------------------------------------- |
| Background  | 0         | background color to use                     |
| DataRange   | Automatic | range of coordinates in the original image  |
| Masking     | Full      | region of interest to be transformed        |
| Padding     | 0         | padding method                              |
| PlotRange   | Automatic | range of coordinates in the resulting image |
| Resampling  | Automatic | resampling method                           |

* Typical settings for ``DataRange`` include:

|     |     |
| --- | --- |
| Automatic | $\{\{0,1\},\{0,h/w\}\}$ in 2D, $\{\{0,1\},\{0,d/w\},\{0,h/w\}\}$ in 3D |
| Full | $\{\{0,w\},\{0,h\}\}$ in 2D, $\{\{0,w\},\{0,d\},\{0,h\}\}$ in 3D |
| {{left, right}, {bottom, top}} | explicit coordinate ranges in 2D |
| {{left, right}, {front, back}, {bottom, top}} | explicit coordinate ranges in 3D |

* The coordinate system of the resulting image is specified by the ``PlotRange`` option. Typical settings include:

|                    |                                              |
| ------------------ | -------------------------------------------- |
| Automatic          | same as DataRange settings                   |
| All                | try to include all of the transformed pixels |
| Full               | same ranges as the input image               |
| {{left, right}, …} | explicit coordinate ranges                   |

* When ``PlotRange`` is not ``Automatic``, ``size`` is chosen based on the size of the original image and the ratio of ``PlotRange`` and ``DataRange``.

* ``Masking`` option can be one of the following:

|      |                         |
| ---- | ----------------------- |
| All  | input image only        |
| Full | input image and padding |
| mask | any mask image          |

* For possible ``size`` specifications, see the reference page for ``ImageResize``.

---

## Examples (30)

### Basic Examples (2)

Transform an image using a perspective transformation:

```wl
In[1]:=
ImagePerspectiveTransformation[[image], (⁠|       |       |   |
| ----- | ----- | - |
| 1     | 1 / 3 | 0 |
| 1 / 3 | 1     | 0 |
| 0     | 1 / 2 | 1 |⁠)]

Out[1]= [image]
```

---

Perspective transformation of a 3D image:

```wl
In[1]:=
ImagePerspectiveTransformation[[image], (⁠|   |       |   |   |
| - | ----- | - | - |
| 1 | 1 / 2 | 0 | 0 |
| 0 | 1     | 0 | 0 |
| 0 | 0     | 1 | 0 |
| 0 | 0     | 0 | 1 |⁠)]

Out[1]= [image]
```

### Scope (13)

#### Data (4)

Transform a grayscale image by scaling with a factor of $1/2$ :

```wl
In[1]:=
ImagePerspectiveTransformation[[image], (1/2)(⁠|   |   |
| - | - |
| 1 | 0 |
| 0 | 1 |⁠)]

Out[1]= [image]
```

---

Transform a color image:

```wl
In[1]:=
ImagePerspectiveTransformation[[image], (1/2)(⁠|   |   |
| - | - |
| 1 | 0 |
| 0 | 1 |⁠)]

Out[1]= [image]
```

---

Transform frames of a video:

```wl
In[1]:=
ImagePerspectiveTransformation[\!\(\*VideoBox["![Video Player: ExampleData/fish.mp4](video://content-83a60)"]\), (1/2)(⁠|   |   |
| - | - |
| 1 | 0 |
| 0 | 1 |⁠)]

Out[1]= [image]
```

---

Transform a 3D image:

```wl
In[1]:=
ImagePerspectiveTransformation[[image], (1/2)(⁠|   |   |   |
| - | - | - |
| 1 | 0 | 0 |
| 0 | 1 | 0 |
| 0 | 0 | 1 |⁠)]

Out[1]= [image]
```

#### Transformations (8)

##### 2D Images (4)

---

A pure rescaling:

```wl
In[1]:=
i = [image];
ImagePerspectiveTransformation[i, (1/2)(⁠|   |   |
| - | - |
| 1 | 0 |
| 0 | 1 |⁠)]

Out[1]= [image]
```

A clockwise rotation of an image:

```wl
In[2]:=
ImagePerspectiveTransformation[i, (1/2)(⁠|         |         |
| ------- | ------- |
| Sqrt[3] | -1      |
| 1       | Sqrt[3] |⁠)]

Out[2]= [image]
```

A shearing transformation:

```wl
In[3]:=
ImagePerspectiveTransformation[i, (|   |    |
| - | -- |
| 1 | -1 |
| 0 | 1  |)]

Out[3]= [image]
```

A pure translation:

```wl
In[4]:=
ImagePerspectiveTransformation[i, (⁠|   |   |       |
| - | - | ----- |
| 1 | 0 | 1 / 4 |
| 0 | 1 | 1 / 4 |
| 0 | 0 | 1     |⁠)]

Out[4]= [image]
```

A general affine transformation:

```wl
In[5]:=
ImagePerspectiveTransformation[i, (⁠|    |    |    |
| -- | -- | -- |
| 1  | .2 | .1 |
| .2 | .5 | .1 |
| 0  | 0  | 1  |⁠)]

Out[5]= [image]
```

A perspective transformation:

```wl
In[6]:=
ImagePerspectiveTransformation[i, (⁠|    |    |    |
| -- | -- | -- |
| 1  | .2 | .1 |
| .2 | .5 | .1 |
| .5 | .2 | 1  |⁠)]

Out[6]= [image]
```

---

Use a geometric transformation function to rotate an image:

```wl
In[1]:=
i = [image];
ImagePerspectiveTransformation[i, RotationTransform[10 °]]

Out[1]= [image]
```

Rotate about the opposite image corner:

```wl
In[2]:= ImagePerspectiveTransformation[i, RotationTransform[10 °, {1, 1}]]

Out[2]= [image]
```

Rotate about the image center:

```wl
In[3]:= ImagePerspectiveTransformation[i, RotationTransform[10 °, {0.5, 0.5ImageAspectRatio[i]}]]

Out[3]= [image]
```

---

Shear an image using ``ShearingTransform`` :

```wl
In[1]:= ImagePerspectiveTransformation[[image], ShearingTransform[20°, {1, 0}, {0, 1}]]

Out[1]= [image]
```

---

Transform an image using a general ``TransformationFunction`` object:

```wl
In[1]:=
ImagePerspectiveTransformation[[image], TransformationFunction[(|    |    |    |
| -- | -- | -- |
| 1  | .2 | .1 |
| .2 | .5 | .1 |
| .5 | .2 | 1  |)]]

Out[1]= [image]
```

##### 3D Images (4)

---

A pure rescaling of a 3D image:

```wl
In[1]:=
i = [image];
ImagePerspectiveTransformation[i, (3/2)(⁠|   |   |   |
| - | - | - |
| 1 | 0 | 0 |
| 0 | 1 | 0 |
| 0 | 0 | 1 |⁠)]

Out[1]= [image]
```

Rotate a 3D image around the $z$ axis:

```wl
In[2]:=
ImagePerspectiveTransformation[i, (1/2)(|         |          |   |
| ------- | -------- | - |
| Sqrt[2] | -Sqrt[2] | 0 |
| Sqrt[2] | Sqrt[2]  | 0 |
| 0       | 0        | 2 |)]

Out[2]= [image]
```

A pure translation of a 3D image in the vertical direction only:

```wl
In[3]:=
ImagePerspectiveTransformation[i, (|   |   |   |       |
| - | - | - | ----- |
| 1 | 0 | 0 | 0     |
| 0 | 1 | 0 | 0     |
| 0 | 0 | 1 | 1 / 3 |
| 0 | 0 | 0 | 1     |)]

Out[3]= [image]
```

---

Rotate a 3D image using ``RotationTransform`` :

```wl
In[1]:= ImagePerspectiveTransformation[[image], RotationTransform[30Degree, {0, 1, 0}], PlotRange -> All]

Out[1]= [image]
```

---

An affine transformation of a 3D image:

```wl
In[1]:= af = Composition[RotationTransform[30°, {0, 1, 0}], TranslationTransform[{0, 0, 1 / 3}]]

Out[1]=
TransformationFunction[(|             |   |             |               |
| ----------- | - | ----------- | ------------- |
| (Sqrt[3]/2) | 0 | (1/2)       | (1/6)         |
| 0           | 1 | 0           | 0             |
| -(1/2)      | 0 | (Sqrt[3]/2) | (1/2 Sqrt[3]) |
| 0           | 0 | 0           | 1             |)]

In[2]:= ImagePerspectiveTransformation[[image], af]

Out[2]= [image]
```

---

A linear fractional transformation of a 3D image:

```wl
In[1]:=
ImagePerspectiveTransformation[[image], TransformationFunction[(|     |     |    |     |
| --- | --- | -- | --- |
| .8  | -.5 | 0. | .6  |
| .5  | .8  | 0. | -.4 |
| .2  | .2  | .8 | -.3 |
| -.2 | 1   | .2 | .5  |)]]

Out[1]= [image]
```

#### Size (1)

The size value ``Automatic`` usually returns images of the same size as the original:

```wl
In[1]:= i = [image];

In[2]:= ImagePerspectiveTransformation[i, RotationTransform[15 °], Automatic]

Out[2]= [image]

In[3]:= ImageDimensions[i] == ImageDimensions[%]

Out[3]= True
```

When ``PlotRange`` is specified, the returned image size is derived from the original size and plot range:

```wl
In[4]:= ImagePerspectiveTransformation[i, RotationMatrix[15 °], Automatic, PlotRange -> {{0, 2}, {0, 3 / 2}}]

Out[4]= [image]
```

Using the value ``All`` always returns an image of the same size as the original:

```wl
In[5]:= ImagePerspectiveTransformation[i, RotationMatrix[15 °], All, PlotRange -> {{0, 2}, {0, 3 / 2}}]

Out[5]= [image]
```

Specify the width of the resulting image:

```wl
In[6]:= ImagePerspectiveTransformation[i, RotationTransform[15 °], 200]

Out[6]= [image]
```

Specify the width and height of the resulting image:

```wl
In[7]:= ImagePerspectiveTransformation[i, RotationTransform[15 °], {100, 100}]

Out[7]= [image]
```

Use a scaled value:

```wl
In[8]:= ImagePerspectiveTransformation[i, RotationTransform[15 °], Scaled[3]]

Out[8]= [image]
```

Use a predefined named size value:

```wl
In[9]:= ImagePerspectiveTransformation[i, RotationMatrix[15 °], Tiny]

Out[9]= [image]
```

### Options (7)

#### Background (1)

By default, a black background is used:

```wl
In[1]:=
i = [image];
ImagePerspectiveTransformation[i, RotationTransform[-10 °]]

Out[1]= [image]
```

Use a specific color for the background:

```wl
In[2]:= ImagePerspectiveTransformation[i, RotationTransform[-10 °], Background -> Gray, Masking -> All]

Out[2]= [image]
```

Use a transparent background:

```wl
In[3]:= ImagePerspectiveTransformation[i, RotationTransform[-10 °], Background -> Transparent, Masking -> All]

Out[3]= [image]
```

Images with an alpha channel use a transparent background by default:

```wl
In[4]:= ImagePerspectiveTransformation[SetAlphaChannel[i], RotationTransform[-10 °]]

Out[4]= [image]
```

#### DataRange (2)

By default, the ``Automatic`` ``DataRange`` is used:

```wl
In[1]:=
i = [image];
ImagePerspectiveTransformation[i, TranslationTransform[{-0.1, -0.1}], DataRange -> Automatic]

Out[1]= [image]
```

Use ``DataRange -> Full`` when defining translation in pixel coordinates:

```wl
In[2]:= ImagePerspectiveTransformation[i, TranslationTransform[{-20, -20}], DataRange -> Full]

Out[2]= [image]
```

Specify a custom ``DataRange`` :

```wl
In[3]:= ImagePerspectiveTransformation[i, TranslationTransform[{-0.1, -0.1}], DataRange -> {{0, 1}, {0, 0.5}}]

Out[3]= [image]
```

---

Use a custom ``DataRange`` setting in translating a 3D image:

```wl
In[1]:= ImagePerspectiveTransformation[[image], TranslationTransform[{0.3, 0.3, 0.3}], DataRange -> {{0, 1}, {0, 1}, {0, 1}}]

Out[1]= [image]
```

#### Masking (1)

By default, ``Masking -> Full`` is used, and padding value is used for pixels outside of the original image:

```wl
In[1]:=
i = [image];
ImagePerspectiveTransformation[i, RotationTransform[20 °], Padding -> "Fixed", Masking -> Full]

Out[1]= [image]
```

With ``Masking -> All``, a background value is used for pixels outside of the original image:

```wl
In[2]:= ImagePerspectiveTransformation[i, RotationTransform[20 °], Background -> Gray, Masking -> All]

Out[2]= [image]
```

Use an arbitrary mask:

```wl
In[3]:= ImagePerspectiveTransformation[i, RotationTransform[20 °], Masking -> Graphics[{White, Disk[]}, Background -> Black]]

Out[3]= [image]
```

#### Padding (1)

By default, the ``Padding -> 0`` is used:

```wl
In[1]:=
i = [image];
ImagePerspectiveTransformation[i, RotationMatrix[30 °], PlotRange -> All]

Out[1]= [image]
```

Use a named color:

```wl
In[2]:= ImagePerspectiveTransformation[i, RotationMatrix[30 °], PlotRange -> All, Padding -> LightGreen]

Out[2]= [image]
```

Use reflected padding:

```wl
In[3]:= ImagePerspectiveTransformation[i, ScalingMatrix[{.5, 1}], Padding -> "Reflected"]

Out[3]= [image]
```

#### PlotRange (1)

By default, ``PlotRange -> Automatic`` is used:

```wl
In[1]:=
i = [image];
ImagePerspectiveTransformation[i, RotationMatrix[20 °]]

Out[1]= [image]
```

Use the ``PlotRange -> All`` option to show all the transformed pixels from the original image:

```wl
In[2]:= ImagePerspectiveTransformation[i, RotationMatrix[20 °], PlotRange -> All]

Out[2]= [image]
```

Use a custom ``PlotRange`` setting:

```wl
In[3]:= ImagePerspectiveTransformation[i, RotationMatrix[20°], PlotRange -> {{0, 2}, {0, 1}}]

Out[3]= [image]
```

Use pixel coordinates with ``PlotRange -> Full`` option:

```wl
In[4]:= ImagePerspectiveTransformation[i, RotationTransform[-35°, ImageDimensions[i] / 2], PlotRange -> Full]

Out[4]= [image]
```

#### Resampling (1)

A cubic interpolant is used by default:

```wl
In[1]:= ImagePerspectiveTransformation[[image], IdentityMatrix[2], 300, Resampling -> Automatic]

Out[1]= [image]
```

Use a different resampling method:

```wl
In[2]:= ImagePerspectiveTransformation[[image], IdentityMatrix[2], 300, Resampling -> "Nearest"]

Out[2]= [image]
```

### Applications (4)

Use a perspective transformation to modify camera position in an image:

```wl
In[1]:= i = [image];
```

Obtain the geometric transformation that maps the four corners of the book to their desired positions:

```wl
In[2]:=
corners = {{39., 18.}, {13., 100.}, {203., 55.}, {162., 120.}};
newCorners = Tuples[MinMax /@ Transpose[corners]];
HighlightImage[i, {PointSize[Large], Point[corners], Blue, Point[newCorners]}]

Out[2]= [image]

In[3]:= t = Last@FindGeometricTransform[newCorners, corners]

Out[3]=
TransformationFunction[(|              |             |          |
| ------------ | ----------- | -------- |
| 0.834082     | 0.243909    | -24.6435 |
| -0.22432     | 0.913753    | 9.29849  |
| -0.000609074 | -0.00177435 | 1.       |)]
```

Apply the transformation function to the image:

```wl
In[4]:= ImagePerspectiveTransformation[i, t, DataRange -> Full]

Out[4]= [image]
```

---

Remove the perspective distortion of the road:

```wl
In[1]:= ImagePerspectiveTransformation[[image], {{1, 0, 0}, {0, 1, 0}, {0, -1 / 320, 1}}, 200, DataRange -> Full, PlotRange -> {{0, 700}, {0, 1100}}]

Out[1]= [image]
```

---

Enhance the perspective effect:

```wl
In[1]:= t = FindGeometricTransform[{{207., 54.5}, {208., 274.5}, {136., 114.5}, {136., 231.5}}, {{207, 54.5}, {208., 274.5}, {136., 94.5}, {136., 251.5}}, TransformationClass -> "Perspective"];ImagePerspectiveTransformation[[image], t[[2]], 300, DataRange -> Full, PlotRange -> All]

Out[1]= [image]
```

---

Segment out the building in an image:

```wl
In[1]:=
i = [image];
c = SelectComponents[ChanVeseBinarize[i, Darker@Yellow, {Automatic, Automatic, 1, 3}], 5000 < #Area < 8000&]

Out[1]= [image]
```

Shear the image to straighten up the building:

```wl
In[2]:=
θ = ComponentMeasurements[c, "Orientation"][[1, 2]];
ImagePerspectiveTransformation[i, ShearingTransform[π / 2 + θ, {1, 0}, {0, 1}], PlotRange -> All]

Out[2]= [image]
```

### Properties & Relations (2)

``ImagePerspectiveTransformation[image, {a, b}]`` applies ``AffineTransform[{a, b}]`` to ``image`` :

```wl
In[1]:=
a = IdentityMatrix[2];
b = {0.1, 0.2};
i = [image];
ImagePerspectiveTransformation[i, {a, b}] == ImagePerspectiveTransformation[i, AffineTransform[{a, b}]]

Out[1]= True
```

---

``ImagePerspectiveTransformation[image, {a, b, c, d}]`` applies ``LinearFractionalTransform[{a, b, c, d}]`` to ``image`` :

```wl
In[1]:=
a = (⁠|        |         |
| ------ | ------- |
| Cos[π] | -Sin[π] |
| Sin[π] | Cos[π]  |⁠);
c = {-0.2, 0.3};
b = {0.4, 0.5};
d = 1.2;
i = [image];ImagePerspectiveTransformation[i, {a, b, c, d}] == ImagePerspectiveTransformation[i, LinearFractionalTransform[{a, b, c, d}]]

Out[1]= True
```

### Possible Issues (2)

When the function maps some coordinates to infinity, truncated output is displayed:

```wl
In[1]:= ImagePerspectiveTransformation[[image], {IdentityMatrix[2], {2, 0}, {-20, 15}, 1}, 200, PlotRange -> All]
```

ImagePerspectiveTransformation::infinity: The transformation will map some of the points into infinity; only a truncated version is displayed.

```wl
Out[1]= [image]
```

---

Non-invertible transformations will give a degenerate result:

```wl
In[1]:= ImagePerspectiveTransformation[[image], {{1, 1 / 2}, {2, 1}}, 200]
```

ImagePerspectiveTransformation::imgtrnsinv: The transformation in {{1.,0.5,0.},{2.,1.,0.},{0.,0.,1.}} is not invertible.

```wl
Out[1]= [image]
```

## See Also

* [`TransformationFunction`](https://reference.wolfram.com/language/ref/TransformationFunction.en.md)
* [`FindGeometricTransform`](https://reference.wolfram.com/language/ref/FindGeometricTransform.en.md)
* [`ImageForwardTransformation`](https://reference.wolfram.com/language/ref/ImageForwardTransformation.en.md)
* [`ImageTransformation`](https://reference.wolfram.com/language/ref/ImageTransformation.en.md)
* [`AffineTransform`](https://reference.wolfram.com/language/ref/AffineTransform.en.md)

## Related Guides

* [Geometric Operations](https://reference.wolfram.com/language/guide/ImageGeometry.en.md)
* [Video Computation: Update History](https://reference.wolfram.com/language/guide/VideoComputation-UpdateHistory.en.md)
* [Video Editing](https://reference.wolfram.com/language/guide/VideoEditing.en.md)
* [Computational Photography](https://reference.wolfram.com/language/guide/ComputationalPhotography.en.md)

## History

* [Introduced in 2010 (8.0)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn80.en.md) \| [Updated in 2014 (10.0)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn100.en.md) ▪ [2021 (13.0)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn130.en.md)