20060801-Spatial transformations: Controlling the input and output grids with imtransform

原文:http://blogs.mathworks.com/steve/2006/08/01/spatial-transformations-controlling-the-input-and-output-grids-with-imtransform/

The function imtransform has several optional parameters that allow you to fine-tune its behavior. Today's topic is about the parameters that let you control where the input image lives in input space, as well as the location and spacing of the output space pixel grid.

Input image location

imtransform assumes that an M-by-N input image lives in input space inside a rectangle spanning from 0.5 to M+0.5 horizontally, and from 0.5 to N+0.5 vertically.

I = checkerboard(1,4);
imshow(I, 'initialmag', 'fit')
axis on
box on
set(gca, 'XTick', [1 8], 'YTick', [1 8])
xlabel('u axis')
ylabel('v axis')
title('8-by-8 image')
What if you want the input image to be in some other location? Sometimes, for example, it's convenient to place the center of the image at the input-space origin.

% imshow doesn't know about u-v space, just x-y space, so its parameters
% are called XData and YData.
imshow(I, 'XData', [-3.5 3.5], 'YData', [-3.5 3.5], 'initialmag', 'fit')
axis on
box on
set(gca, 'XTick', [-3.5 0 3.5], 'YTick', [-3.5 0 3.5])
xlabel('u axis')
ylabel('v axis')
You can place imtransform's input image where you want in u-v space by specifying the UData and VData parameters. UData is a two-element vector. Its first value is the u-coordinate of the first column of the input image. Its second value is the u-coordinate of the last column. Similarly, VData is a two-element vector containing the v-coordinates of the first and last rows of the input image.

To place the center of an M-by-N image at the origin, specify UData to be [1 N] - (N+1)/2 and VData to be [1 M] - (M+1)/2.

Output space pixel grid

imtransform determines the bounding rectangle of the transformed image in output space and then overlays a pixel grid on that rectangle. The pixels in the output grid have the same size as the input image pixels.

You can modify the grid location and pixel size by specifying the XData, YData, and Size parameters in the call to imtransform.

XData and YData are similar to UData and VData. XData is a two-element vector containing the x-coordinates of the first and last columns of the output image. YData is a two-element vector containing the y-coordinates of the first and last rows of the output image.

The Size parameter is a two-element vector specifying how many rows and columns are in the output space grid.

Example

Here's your task: Position an input image centered at the origin and extending from -1 to 1 in the horizontal and vertical directions. Apply a 45 degree rotation to it. Compute the output image only inside a square extending from -0.8 to 0.8 horizontally and vertically. Also, compute the output image using only one-eighth as many pixels (in each direction) as the image input.

I = imread('pout.tif');
imshow(I)
T = [1 -sin(pi/4) 0; sin(pi/4) 1 0; 0 0 1];
tform = maketform('affine', T);

udata = [-1 1];
vdata = [-1 1];

xdata = [-0.8 0.8];
ydata = [-0.8 0.8];
output_size = round(size(I)/8);

J = imtransform(I, tform, 'UData', udata, 'VData', vdata, ...
    'XData', xdata, 'YData', ydata, 'Size', output_size);

imshow(J)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值