Main Content

Create a Gallery of Transformed Images

This example shows many properties of geometric transformations by applying different transformations to a checkerboard image.

Overview

二维几何变换是一个映射,其将欧几里德平面中的每个点与欧几里德平面中的另一个点相关联。在这些示例中,几何变换由一个规则定义,该规则讲述了如何将带有笛卡尔坐标(x,y)与笛卡尔坐标(u,v)的另一点映射的点。棋盘模式有助于可视化输入图像平面中的坐标网格和每个变换引入的失真类型。

Image 1: Create Checkerboard

checkerboardproduces an image that has rectangular tiles and four unique corners, which makes it easy to see how the checkerboard image gets distorted by geometric transformations.

After you have run this example once, try changing the imageIto your favorite image.

sqsize = 60; I = checkerboard(sqsize,4,4); nrows = size(I,1); ncols = size(I,2); fill = 0.3; imshow(I) title('Original')

Image 2: Apply Nonreflective Similarity to Checkerboard

Nonreflective similarity transformations may include a rotation, a scaling, and a translation. Shapes and angles are preserved. Parallel lines remain parallel. Straight lines remain straight.

For a nonreflective similarity,

[ u v ] = [ x y 1 ] T

Tis a 3-by-3 matrix that depends on 4 parameters.

% Try varying these 4 parameters.scale = 1.2;% scale factorangle = 40*pi/180;% rotation angletx = 0;% x translationty = 0;% y translationsc = scale * cos(角);ss = scale * sin(角);t = [SC -SS 0;ss sc 0;TX TY 1];

Since nonreflective similarities are a subset of affine transformations, create anaffine2dobject using:

t_nonsim = affine2d(T); I_nonreflective_similarity = imwarp(I,t_nonsim,“FillValues',填);imshow(I_nonreflective_similarity); title('非反射相似之处')

If you change eithertx要么tyto a non-zero value, you will notice that it has no effect on the output image. If you want to see the coordinates that correspond to your transformation, including the translation, include spatial referencing information:

[I_nonreflective_similarity,RI] = imwarp(I,t_nonsim,“FillValues',填);imshow(I_nonreflective_similarity,RI) axistitle('Nonreflective Similarity (Spatially Referenced)')

Notice that passing the output spatial referencing objectRIfromimwarpreveals the translation. To specify what part of the output image you want to see, use the 'OutputView' name-value pair in theimwarpfunction.

图片3:将相似性与棋盘应用

In a similarity transformation, similar triangles map to similar triangles. Nonreflective similarity transformations are a subset of similarity transformations.

对于相似性,等式与非反射相似度相同:

[ u v ] = [ x y 1 ] T

Tis a 3-by-3 matrix that depends on 4 parameters plus an optional reflection.

% Try varying these parameters.scale = 1.5;% scale factor角度= 10 * pi / 180;% rotation angletx = 0;% x translationty = 0;% y translationa = -1;% -1 -> reflection, 1 -> no reflectionsc = scale * cos(角);ss = scale * sin(角);t = [SC -SS 0;a*ss a*sc 0; tx ty 1];

由于相似性是仿射变换的子集,因此创建一个affine2dobject using:

t_sim = affine2d(T);

As in the translation example above, retrieve the output spatial referencing objectRIfrom theimwarp功能和通过RItoimshowto reveal the reflection.

[I_similarity,RI] = imwarp(I,t_sim,“FillValues',填);imshow(I_similarity,RI) axistitle('Similarity')

Image 4: Apply Affine Transformation to Checkerboard

In an affine transformation, the x and y dimensions can be scaled or sheared independently and there may be a translation, a reflection, and/or a rotation. Parallel lines remain parallel. Straight lines remain straight. Similarities are a subset of affine transformations.

For an affine transformation, the equation is the same as for a similarity and nonreflective similarity:

[ u v ] = [ x y 1 ] T

Tis 3-by-3 matrix, where all six elements of the first and second columns can be different. The third column must be [0;0;1].

% Try varying the definition of T.t = [1 0.3 0;1 1 0;0 0 1];t_aff = actifine2d(t);i_affine = imwarp(i,t_aff,“FillValues',填);imshow(i_affine)标题('Affine')

Image 5: Apply Projective Transformation to Checkerboard

In a projective transformation, quadrilaterals map to quadrilaterals. Straight lines remain straight but parallel lines do not necessarily remain parallel. Affine transformations are a subset of projective transformations.

For a projective transformation:

[ u p v p w p ] = [ x y w ] T

u = u p w p

v = v p w p

T is a 3-by-3 matrix, where all nine elements can be different.

T = [ A D G B E H C F I ]

The above matrix equation is equivalent to these two expressions:

u = A x + B y + C G x + H y + I

v = D x + E y + F G x + H y + I

Try varying any of the nine elements ofT.

t = [1 0 0.002;1 1 0.0002;0 0 1];t_proj = projective2d(t);i_projective = imwarp(i,t_proj,“FillValues',填);imshow(I_projective) title('Projective')

Image 6: Apply Piecewise Linear Transformation to Checkerboard

In a piecewise linear transformation, affine transformations are applied separately to regions of the image. In this example, the top-left, top-right, and bottom-left points of the checkerboard remain unchanged, but the triangular region at the lower-right of the image is stretched so that the bottom-right corner of the transformed image is 50% further to the right and 20% lower than the original coordinate.

movingPoints = [0 0; 0 nrows; ncols 0; ncols nrows;]; fixedPoints = [0 0; 0 nrows; ncols 0; ncols*1.5 nrows*1.2]; t_piecewise_linear = fitgeotrans(movingPoints,fixedPoints,'pwl'); I_piecewise_linear = imwarp(I,t_piecewise_linear,“FillValues',填);imshow(i_piecewise_linear)标题('分段线性')

Image 7: Apply Sinusoidal Transformation to Checkerboard

此示例和以下两个示例示出了如何创建一个显式映射,以将常规网格(xi,yi)中的每个点关联,其中包含不同的点(UI,VI)。此映射存储在a中geometricTranform2dobject, which used byimwarpto transform the image.

在这种正弦变换中,每个像素的X坐标不变。每行像素的Y坐标在正弦图案之后向上或向下移动。

a = ncols/12;% Try varying the amplitude of the sinusoidifcn = @(xy) [xy(:,1), xy(:,2) + a*sin(2*pi*xy(:,1)/nrows)]; tform = geometricTransform2d(ifcn); I_sinusoid = imwarp(I,tform,“FillValues',填);imshow(I_sinusoid); title('Sinusoid')

Image 8: Apply Barrel Transformation to Checkerboard

Barrel distortion perturbs an image radially outward from its center. Distortion is greater farther from the center, resulting in convex sides.

First, define a function that maps pixel indices to distance from the center. Use themeshgridfunction to create arrays of the x-coordinate and y-coordinate of each pixel, with the origin in the upper-left corner of the image.

[xi,yi] = meshgrid(1:ncols,1:nrows);

Shift the origin to the center of the image. Then, convert the Cartesian x- and y-coordinates to cylindrical angle (theta)和半径(r) coordinates using thecart2polfunction.rchanges linearly as distance from the center pixel increases.

xt = xi - ncols/2; yt = yi - nrows/2; [theta,r] = cart2pol(xt,yt);

Define the amplitude,a, of the cubic term. This parameter is adjustable. Then, add a cubic term torso thatr随着距中心像素的距离而变化为非线性。

a = 1;% Try varying the amplitude of the cubic term.rmax = max(r(:)); s1 = r + r.^3*(a/rmax.^2);

Convert back to the Cartesian coordinate system. Shift the origin back to the upper-right corner of the image.

[ut,vt] = pol2cart(theta,s1); ui = ut + ncols/2; vi = vt + nrows/2;

Store the mapping between (xi,yi) and (ui,vi) in ageometricTranform2dobject. Useimwarpto transform the image according to the pixel mapping.

ifcn = @(c) [ui(:) vi(:)]; tform = geometricTransform2d(ifcn); I_barrel = imwarp(I,tform,“FillValues',填);imshow(I_barrel) title('Barrel')

Image 9: Apply Pin Cushion Transformation to Checkerboard

针垫失真是桶变形的逆,因为立方术语具有负幅度。失真仍然远远进一步远离中心,但失真看起来像凹面。

You can begin with the samethetarvalues as for the barrel transformation. Define a different amplitude, b, of the cubic term. This parameter is adjustable. Then, subtract a cubic term torso thatr随着距中心像素的距离而变化为非线性。

b = 0.4;% Try varying the amplitude of the cubic term.s = r - r.^3*(b/rmax.^2);

Convert back to the Cartesian coordinate system. Shift the origin back to the upper-right corner of the image.

[ut,vt] = pol2cart(theta,s); ui = ut + ncols/2; vi = vt + nrows/2;

Store the mapping between (xi,yi) and (ui,vi) in ageometricTranform2dobject. Useimwarpto transform the image according to the pixel mapping.

ifcn = @(c) [ui(:) vi(:)]; tform = geometricTransform2d(ifcn); I_pin = imwarp(I,tform,“FillValues',填);imshow(I_pin) title('Pin Cushion')

Summary: Display All Geometric Transformations of Checkerboard

figure subplot(3,3,1),imshow(I),title('Original')子图(3,3,2),imshow(i_nonreflective_similarity),标题('非反射相似之处') subplot(3,3,3),imshow(I_similarity),title('Similarity') subplot(3,3,4),imshow(I_affine),title('Affine') subplot(3,3,5),imshow(I_projective),title('Projective') subplot(3,3,6),imshow(I_piecewise_linear),title('分段线性')子图(3,3,7),imshow(i_sinusoid),标题('Sinusoid') subplot(3,3,8),imshow(I_barrel),title('Barrel') subplot(3,3,9),imshow(I_pin),title('Pin Cushion')

Note thatsubplotchanges the scale of the images being displayed.

See Also

Functions

Objects

相关话题