Warp
Apply projective or affine transformation
 Library:
Computer Vision Toolbox / Geometric Transformations
Description
The Warp block transforms an image by applying projective or affine transformation. You can transform the entire image or a region of the image by defining a rectangular region of interest (ROI).
Ports
Input
Image
— Input image
matrix  3D numeric array
Input image, specified as one of these values:
matrix — For intensity images of size MbyN.
3D numeric array — For true color images of size MbyNby3.
Data Types: single
 double
 int8
 int16
 int32
 int64
 uint8
 uint16
 uint32
 uint64
TForm
— Transformation matrix
3by2 matrix  3by3 matrix
Transformation matrix, specified as any one of these values:
3by2 matrix — For affine transformation.
3by3 matrix — For projective transformation.
For more information about the transformations, see Algorithms.
Dependencies
To enable this input port, set the Transformation matrix
source parameter value to Input port
.
Data Types: double
 single
ROI
— Region of interest
4element vector
Region of interest, specified as a 4element vector of form [x_{s} y_{s} width height]. x_{s} and y_{s} are the x and y coordinates of the top left corner of the ROI, respectively.
If you specify the ROI
input, the
Warp
block applies transformation only to the specified
region and returns the transformed region at the output.
Dependencies
To enable this input port, select the Enable ROI input port parameter.
Output
Image
— Transformed image
matrix  3D numeric array
Transformed image, returned as one of these values:
matrix — If input is an intensity image of size PbyQ1.
3D numeric array — If input is a true color image of size PbyQby3.
The data type of the output transformed image is same as that of the input image. The size of the output transformed image is either same as the input image or equal to the value set for the Output image position vector [x y width height] parameter.
Data Types: single
 double
 int8
 int16
 int32
 int64
 uint8
 uint16
 uint32
 uint64
Err_roi
— Indicator for transformed ROI outside image region
0
 1
Indicator for transformed ROI outside image region, returned as
0
or 1
.
Data Types: Boolean
Parameters
Transformation matrix for source
— Source for passing transformation matrix
Input port
(default)  Custom
Source for passing transformation matrix, specified as either Input
port
or Custom
. If you select
Custom
, you can enter the transformation matrix
coefficients by using the Transformation matrix parameter that
appears with this selection.
Transformation matrix
— Value for transformation matrix
3by2 matrix  3by3 matrix
Value for transformation matrix, specified as a 3by2 matrix for affine transformation or 3by3 matrix for projective transformation.
Dependencies
To enable this parameter, set the Transformation matrix for
source parameter value to Custom
.
Interpolation method
— Method for interpolating transformed pixel values
Bilinear
(default)  Nearest neighbor
 Bicubic
Method for interpolating transformed pixel values, specified as Nearest
neighbor
, Bilinear
, or
Bicubic
.
If you select Nearest neighbor
, the block uses the value
of an nearby pixel for the new pixel value. If you select
Bilinear
, the new pixel value is the weighted average of
the four nearest pixel values. If you select Bicubic
, the new
pixel value is the weighted average of the sixteen nearest pixel values.
The number of pixels the block considers affects the complexity of the computation.
Therefore, the Nearest neighbor
interpolation is the most
computationally efficient. However, because the accuracy of the method is proportional
to the number of pixels considered, the Bicubic
method is the
most accurate. For information about the interpolation methods, see the More About
section.
Background fill value
— Intensity value for background pixels in the transformed image
0
(default)  scalar  3element vector
Intensity value for background pixels in the transformed image, specified as one of these values:
scalar — If input image is a gray scale image.
3element vector — If input image is a true color image. The vector is of the form [r g b] specifying the red (r), green (g), and blue (b) color channel values for the background pixels.
The default fill value is 0
and sets the background
color to black.
Output image position source
— Source for passing output image size
Same as input image
(default)  Custom
Source for passing a value for the output image size, specified as either
Same as input image
or Custom
.
If you select
Same as input image
, the output transformed image is of same size as that of the input image.If you select
Custom
, you must specify a bounding box to output only the image region that lies within the bounding box. This selection enables the Output image position vector [x y width height] parameter that you can use for specifying the bounding box value.
Output image position vector [x y width height]
— Size of the output image
[1
1
512
512
] (default)  4element vector
Size of the output image, specified as a four element vector of form
[x
y
width
height]. When you specify this parameter, the
Warp
block creates a bounding box of specified width and
height values. The size of the output image is set to the size of the bounding box and
will contain the transformed image region that lies within the bounding box. If the size
of the output image is greater than the size of the transformed image region within the
bounding box, the intensity value of the extraneous pixels in the output image are set
to the value specified for Background fill value parameter.
x and y are the spatial coordinates that define topleft corner position of the bounding box with respect to the input image.
Dependencies
To enable this parameter, set the Output image position
source parameter value to Custom
.
Enable ROI input port
— Input ROI
off (default)  on
Select this parameter to enable the ROI
input port and specify
the ROI to be transformed.
Enable output port indicating if any part of ROI is outside input image
— Output if transformed ROI is outside the specified image size
off (default)  on
Select this parameter to enable the Err_roi
output port.
Dependencies
To enable this parameter, select the Enable ROI input port parameter.
Simulate using
— Block simulation method
Interpreted Execution
(default)  Code Generation
Block simulation method, specified as Interpreted Execution
or
Code Generation
. If you want your block to use the MATLAB^{®} interpreter, choose Interpreted Execution
. If you want
your block to run as compiled code, choose Code Generation
. For more
information, see Choosing a Simulation Mode (Simulink).
Model Examples
Block Characteristics
Data Types 

Multidimensional Signals 

VariableSize Signals 

More About
Nearest Neighbor Interpolation Method
For nearest neighbor interpolation, the block uses the value of nearby translated pixel values for the output pixel values.
For example, suppose this matrix,
$$\begin{array}{ccc}1& 2& 3\\ 4& 5& 6\\ 7& 8& 9\end{array}$$
represents your input image. You want to translate this image 1.7 pixels in the positive horizontal direction using nearest neighbor interpolation. The block's nearest neighbor interpolation algorithm is illustrated by the following steps:
Zero pad the input matrix and translate it by 1.7 pixels to the right.
Create the output matrix by replacing each input pixel value with the translated value nearest to it. The result is the following matrix:
$$\begin{array}{ccccc}0& 0& 1& 2& 3\\ 0& 0& 4& 5& 6\\ 0& 0& 7& 8& 9\end{array}$$
Note
You wanted to translate the image by 1.7 pixels, but this method translated the image by 2 pixels. Nearest neighbor interpolation is computationally efficient but not as accurate as bilinear or bicubic interpolation methods.
Bilinear Interpolation
For bilinear interpolation, the block uses the weighted average of two translated pixel values for each output pixel value.
For example, suppose this matrix,
$$\begin{array}{ccc}1& 2& 3\\ 4& 5& 6\\ 7& 8& 9\end{array}$$
represents your input image. You want to translate this image 0.5 pixel in the positive horizontal direction using bilinear interpolation. The block's bilinear interpolation algorithm is illustrated by the following steps:
Zero pad the input matrix and translate it by 0.5 pixel to the right.
Create the output matrix by replacing each input pixel value with the weighted average of the translated values on either side. The result is the following matrix where the output matrix has one more column than the input matrix:
$$\begin{array}{cccc}0.5& 1.5& 2.5& 1.5\\ 2& 4.5& 5.5& 3\\ 3.5& 7.5& 8.5& 4.5\end{array}$$
Bicubic Interpolation
For bicubic interpolation, the block uses the weighted average of four translated pixel values for each output pixel value.
For example, suppose this matrix,
$$\begin{array}{ccc}1& 2& 3\\ 4& 5& 6\\ 7& 8& 9\end{array}$$
represents your input image. You want to translate this image 0.5 pixel in the positive horizontal direction using bicubic interpolation. The block's bicubic interpolation algorithm is illustrated by the following steps:
Zero pad the input matrix and translate it by 0.5 pixel to the right.
Create the output matrix by replacing each input pixel value with the weighted average of the two translated values on either side. The result is the following matrix where the output matrix has one more column than the input matrix:
$$\begin{array}{cccc}0.375& 1.5& 3& 1.625\\ 1.875& 4.875& 6.375& 3.125\\ 3.375& 8.25& 9.75& 4.625\end{array}$$
Algorithms
Affine Transformation
The affine transformation matrix is a 3by2 matrix of form
$$H=\left[\begin{array}{cc}{h}_{1}& {h}_{4}\\ {h}_{2}& {h}_{5}\\ {h}_{3}& {h}_{6}\end{array}\right]$$
where h_{1}, h_{2},...,h_{6} are transformation coefficients.
The value of the pixel located at $$\left[x,y\right]$$ in the input image determines the value of the pixel located at $$\left[\widehat{x},\widehat{y}\right]$$ in the output transformed image. The relationship between the input and output point locations is defined by:
$$\{\begin{array}{c}\widehat{x}=x{h}_{1}+y{h}_{2}+{h}_{3}\\ \widehat{y}=x{h}_{4}+y{h}_{5}+{h}_{6}\end{array}$$
Projective Transformation
The projective transformation matrix is a 3by3 matrix of form
$$H=\left[\begin{array}{ccc}{h}_{1}& {h}_{4}& {h}_{7}\\ {h}_{2}& {h}_{5}& {h}_{8}\\ {h}_{3}& {h}_{6}& {h}_{9}\end{array}\right]$$
where h_{1}, h_{2},...,h_{9} are transformation coefficients.
The value of the pixel located at $$\left[x,y\right]$$ in the input image determines the value of the pixel located at $$\left[\widehat{x},\widehat{y}\right]$$ in the output transformed image.
The relationship between the input and output point locations is defined by:
$$\{\begin{array}{c}\widehat{x}=\frac{x{h}_{1}+y{h}_{2}+{h}_{3}}{x{h}_{7}+y{h}_{8}+{h}_{9}}\\ \widehat{y}=\frac{x{h}_{4}+y{h}_{5}+{h}_{6}}{x{h}_{7}+y{h}_{8}+{h}_{9}}\end{array}$$
The values of pixels in the output point locations are calculated using any of these interpolation methods: Nearest neighbor, bilinear, and bicubic interpolation.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using Simulink® Coder™.
Version History
Introduced in R2015b
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
 América Latina (Español)
 Canada (English)
 United States (English)
Europe
 Belgium (English)
 Denmark (English)
 Deutschland (Deutsch)
 España (Español)
 Finland (English)
 France (Français)
 Ireland (English)
 Italia (Italiano)
 Luxembourg (English)
 Netherlands (English)
 Norway (English)
 Österreich (Deutsch)
 Portugal (English)
 Sweden (English)
 Switzerland
 United Kingdom (English)