Steve on Image Processing

July 27th, 2006

Spatial transformations: Handling noninvertible cases

I've written previously about how imtransform uses inverse mapping to compute the input-space location corresponding to each output pixel. I've also written about how imtransform uses the forward mapping to determine the location of the output image in output space.

But what about noninvertible mappings? For example, the function cp2tform can produce several types of spatial transformations that aren't invertible. It can infer a polynomial transformation from a set of control points:

pairs1 = [1 1; 5 21; 17 40; 28 1; 32 20; 45 40; 72 1; 77 20; 90 40];
pairs2 = [1 1; 1 21; 1 40; 20 1; 20 20; 20 40; 40 1; 40 20; 40 40];
t_poly = cp2tform(pairs1, pairs2, 'polynomial',2);
I = checkerboard(10, 2);
J = imtransform(I,t_poly);

subplot(1,2,1)
imshow(I), title('checkerboard')

subplot(1,2,2)
imshow(J), title('polynomial transformation')

But the polynomial transformation isn't invertible. Since imtransform uses inverse mapping and so must have at least the inverse transformation, cp2tform produces a tform structure where the polynomial transformation is used in the inverse direction, and the structure contains nothing for the forward direction.

t_poly
t_poly = 

       ndims_in: 2
      ndims_out: 2
    forward_fcn: []
    inverse_fcn: @inv_polynomial
          tdata: [6x2 double]

If there's no forward transformation available, then how does findbounds work? That is, how does it find the bounding rectangle of the image in output space?

It does it by using a search. Specifically, for an input-space location u, it uses fminsearch to find the output-space location x that minimizes:

It's possible fminsearch might fail to converge, in which case findbounds issues a warning message and "guesses" that the output image's bounding rectangle is the same as the input image's bounding rectangle.

If you're interested in the details, look at the find_bounds_using_search subfunction inside findbounds.m.


Get the MATLAB code

Published with MATLAB® 7.2

6 Responses to “Spatial transformations: Handling noninvertible cases”

  1. Jens Michaelis replied on :

    Thanks for the example. I have a question though:

    if there is no forward function defined, how can one use the tformfwd command to find out how the process worked?

    thanks for the help,

    jens

  2. Steve replied on :

    Jens – you can’t!

  3. Jens Michaelis replied on :

    It is correct that you can’t, however the workaround is fairly easy, just plug the result in and use tforminv:

    TFORM = cp2tform(input_points, destination_points,’polynomial’,3); % this should make the transform
    predicted_destinations = tforminv(TFORM,destination_points); % check how it worked
    map_residuals = input_points – predicted_destinations

  4. Steve replied on :

    Jens – I’m sorry, I thought you were asking how to verify that findbounds worked.

  5. Ashvini replied on :

    Is there a way to work around the findbound failure? I do a polynomial transformation on an image and findbound fails do to which a small portion of the image is cropped from the left and an extra blank portion is added on the right.

  6. Steve replied on :

    Ashvini—Because you are using a spatial transform that isn’t invertible, findbounds at best can only do an approximation. If you have other information about your problem that tells you where the output image should be, then use that information to explicitly control the output grid instead of relying on the automatic, findbounds-based behavior.


MathWorks
Steve Eddins is a software development manager in the MATLAB and image processing areas at MathWorks. Steve coauthored Digital Image Processing Using MATLAB. He writes here about image processing concepts, algorithm implementations, and MATLAB.

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