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.

  • Steve: Kezia—Try imrotate.
  • kezia: steve, how to perform rotation of structuring element by 15 degrees. kindly answer my question. thank u kezia...
  • Steve: Tasha—I only accept comments that are relevant to the particular blog post or are questions or comments...
  • Tasha: Steve,I send you a comment here but still didn’t get any reply yet.I did not see my comment posted here...
  • Steve: Carsten—Thanks for your input.
  • Carsten: Another vote for either imtranslate.m, or at least a blurb in the imtransform help why pure translation...
  • Loren Shure: If you look towards the end of the fftfilt program, you will see that there’s a check to see if...
  • Steve: Sonja—My imwritesize submission on the MATLAB Central File Exchange might be helpful. It was posted...
  • Steve: Grant—Sorry, but it won’t be for R2010a. That development deadline has already passed.
  • Sonja: My publisher is wanting images for a new book to be 300 dpi. Only 5 of the 19 images are 300, the rest are...

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