Padding and Shearing an Image Simultaneously

此示例显示了如何构建一个tform.表示简单剪切变换的结构,然后将其应用于图像。我们探讨转换如何影响直线和圆圈,然后将其用作探索可用于图像填充的各种选项的车辆imtransformtform.array

Step 1: Transform an Image Using Simple Shear

在两个方面,一个简单的剪切变换,即映射一对输入坐标[u v]to a pair of output coordinates[x y]有形式

x = u + a * v

y = v

wherea是一个常数。

Any simple shear is a special case of an affine transformation. You can easily verify that

[ x y 1 ] = [ u v 1 ] * [ 1 0 0 a 1 0 0 0 1 ]

yields the values forxythat you received from the first two equations.

设置a= 0.45,我们构建一个仿射tform.struct usingmaketform

一个= 0.45;T = maketform ('affine', [1 0 0; a 1 0; 0 0 1] );

我们选择,读取和查看和视图和图像转换。

A = imread('足球.jpg');h1 = figure; imshow(A); title('Original Image');

我们选择a shade of orange as our fill value.

orange = [255 127 0]';

We are ready to useT改变A。We could callimtransform如下:

B = imtransform(A,T,'cubic','FillValues',orange);

but this is wasteful since we would apply cubic interpolation along both columns and rows. (With our pure shear transform, we really only need to interpolate along each row.) Instead, we create and use a resampler that applies cubic interpolation along the rows but simply uses nearest neighbor interpolation along the columns, then callimtransform和display the result.

R = makeresampler({'立方体','nearest'},'fill');B = imtransform(A,T,R,'FillValues',橙子);h2 =数字;imshow(b);标题('Sheared Image');

Step 2: Explore the Transformation

转换直线网格或圆圈数组tform.fwd是理解转型的好方法(只要它具有前向和逆函数)。

Define a grid of lines covering the original image, and display it over the image Then usetform.fwd将纯剪切应用于网格中的每一行,并在剪切图像上显示结果。

[u,v] = meshgrid(0:64:320,0:64:256);[x,y] = tformfwd(t,u,v);灰色= 0.65 * [1 1];图(H1);保持;线(U,V,'颜色',灰色);line(U',V','颜色',灰色);

figure(h2); hold;line(X, Y,'颜色',灰色);line(X',Y','颜色',灰色);

You can do the same thing with an array of circles.

灰色= 0.65 * [1 1];foru = 0:64:320forv = 0:64:256 theta = (0 : 32)' * (2 * pi / 32); uc = u + 20*cos(theta); vc = v + 20*sin(theta); [xc,yc] = tformfwd(T,uc,vc); figure(h1); line(uc,vc,'颜色',灰色);figure(h2); line(xc,yc,'颜色',灰色);endend

第3步:比较“填充”,“复制”和“绑定”焊盘方法

When we applied the shear transformation,imtransform填充在左侧和右侧的橙色三角形,没有数据。那是因为我们指定了一种垫方法'fill'when callingmakeresampler。共有五种不同的焊盘方法选择('fill','复制','界','circular',和'symmetric'). Here we compare the first three.

First, to get a better look at how the'fill'option worked, use the'XData''YData'options inimtransformto force some additional space around the output image.

R = makeresampler({'立方体','nearest'},'fill');Bf = imtransform(A,T,R,'XData',[-49 500],'YData',[-49 400],。。。'FillValues',橙子);图,imshow(bf);标题('垫方法=“填满”);

Now, try the'复制'method (no need to specify fill values in this case).

R = makeresampler({'立方体','nearest'},'复制');br = imtransform(a,t,r,'XData',[-49 500],'YData', [-49 400]); figure, imshow(Br); title('Pad Method = ''replicate''');

And try the'界'方法。

R = makeresampler({'立方体','nearest'},'界');Bb = imtransform(A,T,R,'XData',[-49 500],'YData',[-49 400],。。。'FillValues',橙子);figure, imshow(Bb); title('Pad Method = ''bound''');

Results with'fill''界'look very similar, but look closely and you'll see that the edges are smoother with'fill'。这是因为输入图像用填充值填充,然后在边缘,混合填充和图像值上施加立方插值。相反,'界'识别输入图像的内部和外部之间的严格边界。落在外面的积分是填补。当它们靠近边缘时,落在内部的点是内插的。近距离看起来可以更清楚地显示这一点。我们选择XDataYDatato bracket a point near the lower right corner of the image, in the output image space, the resize with'nearest'to preserve the appearance of the individual pixels.

R = makeresampler({'立方体','nearest'},'fill');cf = imtransform(a,t,r,'XData',[423 439],'YData',[245 260],。。。'FillValues',橙子);R = makeresampler({'立方体','nearest'},'界');Cb = imtransform(A,T,R,'XData',[423 439],'YData',[245 260],。。。'FillValues',橙子);cf = imresize(CF,12,'nearest');Cb = imresize(Cb,12,'nearest');figure; subplot(1,2,1); imshow(Cf); title('垫方法=“填满”);subplot(1,2,2); imshow(Cb); title('Pad Method = ''bound''');

Step 4: Exercise the 'circular' and 'symmetric' Pad Methods

剩下的两种垫方法是'circular'(circular repetition in each dimension) and'symmetric'(具有附加镜像的图像的循环重复)。要显示更多的模式,我们将转换重新定义为减少尺度。

Thalf = maketform('affine',[1 0; a 1; 0 0]/2); R = makeresampler({'立方体','nearest'},'circular');Bc = imtransform(A,Thalf,R,'XData',[-49 500],'YData',[-49 400],。。。'FillValues',橙子);figure, imshow(Bc); title('pad方法=''圆形''');

R = makeresampler({'立方体','nearest'},'symmetric');Bs = imtransform(A,Thalf,R,'XData',[-49 500],'YData',[-49 400],。。。'FillValues',橙子);figure, imshow(Bs); title('Pad Method = ''symmetric''');