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

4 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.

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


Steve Eddins manages the Image & Geospatial development team at The MathWorks and coauthored Digital Image Processing Using MATLAB. He writes here about image processing concepts, algorithm implementations, and MATLAB.

  • Sana: hi steve, could you explain to me how i would be able to use the dir function, to do a loop through a directory...
  • Nishtha: Sir, I have preprocessed the image in following steps: [1] adaptive histogram equalization [2] thresholding...
  • Kristof: I also strongly support the idea. I have just recently bumped into the problem that im2single was not...
  • Steve: David—I’ m glad you found it useful!
  • David Lalejini: I found your example very useful for finding connected nodes in a large set of input pairs. I start...
  • tommy: Dear Steve, I have a question,please if you are kind to help me regarding the accumulator array dimensions of...
  • Steve: Abc—I don’t know how to distinguish the faces. You might try posting your question in the MATLAB...
  • Manju: well if we have a few ovals within each other like in a cell how do we measure the distance from the center...
  • Steve: Manju—What do you mean? How is each region defined?
  • Manju: if we have 2-3 regions within each other how do we measure the regions of each one?

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