# Spatial transformations: Forward mapping11

Posted by Steve Eddins,

I've written previously about defining a spatial transform as a function, (x,y) = T{(u,v)}, that maps points from one space (input space) to another (output space). Given such a function, how do you go about spatially transforming an image?

The most obvious procedure is called forward mapping. It works like this:

Consider each input image pixel in turn. For each input image pixel:

• Determine its location in input space, (uk,vk).
• Map that location to output space using (xk,yk) = T{(uk,vk)}.
• Figure out which output pixel contains the location (xk,yk).
• Copy the input pixel value to that output pixel.
This diagram illustrates the procedure:

Forward mapping has two main disadvantages as a computational procedure: gaps and overlaps. Depending on specific spatial transform function, you may have some output pixels that did not receive any input image pixels; these are the gaps. You may also have some output pixels that received more than one input image pixel; these are the overlaps. In both cases, it is challenging to figure out a reasonable way to set those output pixels.

One way to overcome these problems is to map pixel rectangles in input space to output space quadrilaterals, as shown here:

With this procedure, the input pixel value is allocated to different output pixels depending on the relative fractional coverage of the quadrilateral. Although this procedure can produce good results, it is complicated to implement and it takes a long time to compute.

Because of the problems with forward mapping, many (most?) image spatial transform implementations use a different technique, called inverse mapping. I'll describe that in a future post.

Leonardo replied on : 1 of 11

Dear Steve,
I have an input image from which I digitized grid points.
I know the real distance between the grid points, so I can create an output array. I would like to specify a transformation using these points, so I can assign the real location to each point from the input.
Is it possible?
Best
Leonardo

Steve replied on : 2 of 11

Leonardo—Sounds like you could use cp2tform to estimate a spatial transform fitting your points.

Rudolph replied on : 3 of 11

Hi Steve,

I need to perform lense calibration on an image taken with specific camera. The lense coefficients are available.

The distortions on the lense are radial distortion, decentering of the principal point and some affinity. According to me I’ll have to calculate the (subpixel units) distance which I have to move each pixel in the x and y direction. So each pixel will have it’s own resulting transformation. I went through most of your blogs and I want to create a custom tform struct which I can implement on bulk images.

Now I can’t see how I will be able to do this with inverse mapping, true? Will imtransform deal with the overlapping and gaps for me automatically with chosen interpolation? How do I deal with a pixel that moves 0.166 pixels up for instance? How will matlab save the info on output space?

Any guidence would be appreciated.

Rudolph replied on : 4 of 11

I managed to overcome the idea of linear indexing which imtransform use to determine the custom spatial transform. Thanks

douglas replied on : 5 of 11

Could you tell how to apply the grid points (data points from the transformation grid) in the transformation ?

[Points edited out for space.]

Steve replied on : 6 of 11

Douglas—Use cp2tform to infer a spatial transformation from your point set. One of the spatially varying ones might be best. Then use imtransform to warp your image.

Jie Shi replied on : 7 of 11

Hello, Steve. In my recent research, I have to spatially warp an image using a vector field. After the forward mapping, some of the image coordinates will be mapped to floating-point values. I think this problem can be addressed by the rectangle-to-quadrilateral method you mentioned here. But I am wondering whether there is a function in Matlab to do so, or if I need to implement the algorithm myself? In the latter case, do you have some references for this algorithm’s details? Thanks.

Steve replied on : 8 of 11

Jie—No, there is no forward-mapping spatial image transformation function. I’m sorry, but I don’t have any references.

Shre replied on : 9 of 11

Hi Steve,

I’m doing a facial aging project at the undergraduate level.I wanted the user to click on certain control points on the 20 year old image and then wanted the program to to warp certain features downward to show geometrical facial changes.The displacement will be embedded into the code already. Which transformation should I use?..Can I find MATLAB code for it which I can modify(add displacement values, etc) to fit my purpose. Most of the codes available warp/morph one photo into another…in this case i dont have my resultant image(its to be predicted)…so i cant use those codes and i dont know how to modify them….It’s very very urgent! I’ll appreciate if you reply to this ASAP

heres the link of the paper that Im trying to implement

http://www.dca.fee.unicamp.br/~leopini/private/pubs/Greyce-2007WesternNYImageProcessingWorksop.pdf

Thanks!

Shre replied on : 10 of 11

In the above mentioned comment I would like to add that only those control points selected by the user by clicking with the mouse need to be warped to target pixel position

Steve replied on : 11 of 11

Shre—I can’t offer consulting on individual projects or on reading and interpreting journal papers. It sounds like you should use on of the locally varying transform types offered by the function cpselect.

These postings are the author's and don't necessarily represent the opinions of MathWorks.