Steve on Image Processing

February 7th, 2006

Spatial transformations: Affine

To explore spatial transformations of images, we need a simple, nontrivial, and useful transformation. The affine transformation fits the bill.

Here's the basic affine equation:

or:

Already we've encountered another common notational confusion. Sometimes a user will contact us and say "imtransform shrank my image in the horizontal direction, when it should have shrunk it in the vertical direction. Is this a bug in imtransform?" No, it's not a bug. The cause is that the user tried an A matrix from a source that defines the affine equation this way:

The A matrices in these two formulas are not the same; one is the transpose of the other.

Several common spatial transformations applied to images can be expressed in terms of an affine equation, including scaling:

Rotation:

And translation:

OK, enough background notation and equations. Next time we'll jump into MATLAB, construct some affine transformations, and apply them to points.


Get the MATLAB code

Published with MATLAB® 7.1

19 Responses to “Spatial transformations: Affine”

  1. copi replied on :

    Hello again,
    This is a very interesting subjet (geometric trasformations I mean) but I always wonder how MATLAB implements this transformations over an image.
    Is there any way to “vectorize” the operation? The problem even will be worst with a projective transformation due to the fact that the third output coordinate will not be one, so you have to make aditional operations to come back to cartesian coordinates from the homogenious coords.
    In adittion there must be a resampling too.
    What about a little insight on imtransform function? Thanks

  2. Steve replied on :

    copi - Don’t worry, I’ll get there. It’s a big subject area, and I’m taking it a step at a time.

  3. Josh replied on :

    Steve, is it possible to transform an image given only a forward transform? I have a transform in mind but it is rather complicated to compute and its inverse cannot be solved for.

  4. Steve replied on :

    Josh - Not with Image Processing Toolbox functions. imtransform and tformarray use inverse mapping, for reasons I’ll get into in future blog posts. I suggest that you reverse the input and output images in your computation, so that what you’ve been calling the forward transform becomes the inverse transform. Then you can use imtransform or tformarray.

  5. Axel replied on :

    Hi Steve,

    I’m glad I found this blog. I have some calibration data and would like to find the optimal transformation (affine or other) to a set of reference points. With the ‘maketform’ I can only use three or four corresponding points. Is there an algorithm (hopefully implemented in Matlab) that can use a larger set of corresponding points to find a transformation?
    Thanks

  6. Steve replied on :

    Axel - Yes. See the function cp2tform.

  7. Mehdi replied on :

    Hi Steve,
    Thanks for your information. I have a set of points on the image. I want to know which groups of points are polygons and which ones are not? Is there a solution in Matlab for it?
    Thanks,Mehdi

  8. Steve replied on :

    Mehdi - You’ll have to be more specific about your problem. Any group of points can be vertices of a polygon if you allow degenerate and nonsimple polygons.

  9. Mehdi replied on :

    Hi Steve,

    Thanks for your reply. I applied a segmentation procedure and extract some features in the image. Some of the segmented features look like closed curves or polygon and some of features look like linear feauture or open contours. I don’t know in Matlan there is such a command to classify closed curve or contours from open ones?

    Thank you very much,
    Mehdi

  10. Steve replied on :

    Mehdi - I don’t have any particular suggestions for you.

  11. Gleb replied on :

    Steve,
    I’m not sure whether my question is related to this topic.
    I have two images, which I need to combine as follows: one image should be represented in pseudocolors (in “hsv” colormap) but the other image should be represented as a value. In other words, the brightness of pixels needs to be modified. Of course, both images are 2D matrices of the same size. I’m quite confused on how to do it…
    Thank you very much!!!
    Gleb

  12. Steve replied on :

    Gleb - Your question is a little too vague to answer specifically. Usually people will take three 2-D matrices, treat them as different components of some color space, modify them as desired, concatenate them to form an M-by-N-by-3 array, and then use one of the color space conversion functions to make an RGB image for display. Something like this:

    hsv_image = cat(3, H, S, V);
    rgb_image = hsv2rgb(hsv_image);
    
  13. Gleb replied on :

    Steve,
    thank you very much! I will try. :)

  14. Gleb replied on :

    Steve,
    it’s exactly what I needed! Thank you again!!!

  15. Steve replied on :

    Gleb - I’m glad it worked!

  16. Eric Monse replied on :

    Can’t wait to see Matlab.

  17. Steve replied on :

    Eric—What do you mean?

  18. Maher replied on :

    Dear Steve,
    I am trying to find the similarity degree between historic and modern maps (statistically). I need to convert the coordinates of the modern image to fit that of the old one using Matlab.For example: through affine transformation.
    Please, advise so.
    Great Thanks for you.

  19. Steve replied on :

    Maher—You might try using cpselect to select pairs of corresponding points, followed by cp2tform to infer an affine transformation.

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.