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.

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