Steve on Image Processing

June 13th, 2006

Spatial transformations: findbounds

I've written previously about how Image Processing Toolbox uses inverse mapping to implement spatial transforms. In this method, you set up a grid in output space. For each pixel in the output space grid, you use the inverse transform to determine the corresponding input space location, and you interpolate to get the input image pixel value at that point.

I've also written about the question of where to establish the grid in output space. If you don't somehow pay attention to the specific transformation, you might choose a grid that's too small:

Or your grid might be too big:

Or your grid might be in the wrong place completely:

To avoid these problems (and to avoid the tech support calls!), the function imtransform uses the function findbounds to determine automatically where to place the output grid and how big it should be.

findbounds first creates a grid of points. These points are located in input space at each corner of the image, as well as in between each corner and in the middle. It looks like this:

I = imread('rice.png');
h = imshow(I);
set(h,'AlphaData',0.3);
axis on, grid on
in_points = [ ...
    0.5000    0.5000
    0.5000  256.5000
  256.5000    0.5000
  256.5000  256.5000
    0.5000  128.5000
  128.5000    0.5000
  128.5000  128.5000
  128.5000  256.5000
  256.5000  128.5000];
hold on
plot(in_points(:,1), in_points(:,2), '.', 'MarkerSize', 18)
hold off

findbounds then calls tformfwd to transform these points into output space. (For this example I'm going to construct an affine tform struct that combines scaling, rotation, shear, and a large translation.)

tform = maketform('affine', ...
    [1.1067 -0.2341 0; 0.5872 1.1769 0; 1000 -300 1]);
out_points = tformfwd(tform, in_points)
out_points =

  1.0e+003 *

    1.0008   -0.2995
    1.1512    0.0018
    1.2842   -0.3595
    1.4345   -0.0582
    1.0760   -0.1489
    1.1425   -0.3295
    1.2177   -0.1789
    1.2928   -0.0282
    1.3593   -0.2088

plot(out_points(:,1), out_points(:,2), '.', 'MarkerSize', 18)
axis ij, axis image
grid on

The bounding box of the points in output space tells us where to put the output space grid.

When you call imtransform, you can use optional output arguments to determine where the image is located in output space.

[J,XData,YData] = imtransform(I, tform);
XData
XData =

  1.0e+003 *

    1.0017    1.4337

YData
YData =

 -358.7527    1.2473

You can use this information together with imshow to place the image in the right place on an axes that contains other X-Y data.

h = imshow(J,'XData',XData,'YData',YData);
set(h,'AlphaData',0.3)
hold on
plot(out_points(:,1), out_points(:,2), '.', 'MarkerSize', 18)
axis on
grid on
hold off
axis ij, axis image

So that's the method imtransform uses to determine where the output image should be in output space.

In a future posting I'll cover two more topics related to findbounds and imtransform:

  • What to do when there's no forward transform in the tform structure
  • The special problem of pure translation


Get the MATLAB code

Published with MATLAB® 7.2

6 Responses to “Spatial transformations: findbounds”

  1. Qi Chen replied on :

    Excellent explanation. I suggest to include this in the help documentation. I have struggled for understanding how imtransform works when reading the help file in matlab. I wasn’t clear until reading this.

  2. Steve replied on :

    Qi - Thanks for the suggestion.

  3. and replied on :

    i have a data set of points (10 points of the image in different places of the image) of an image that i’m interest to and i would like to know if there is a way to know where these points would be after a rotation .

  4. Steve replied on :

    And—The functions tformfwd and tforminv can be used to transform point locations. See my February 10, 2006 post.

  5. Zhenbo Wang replied on :

    Hi,Steve.
    Would you give a specific Example by using findbounds function,PLZ? I still don’t quite get it after reading the “findbounds” demo.

  6. Steve replied on :

    Zhenbo—Can you say more about what you are looking for? I gave a specific example in this post. Also, be aware that users generally don’t need to worry about calling findbounds directly. This post was intended to explain a little bit about what’s going on behind the scene.

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.