Steve on Image Processing

March 31st, 2008

Viewing output-space coordinates for a transformed image

Blog reader Ram asked a question last week that I hear fairly often: When you apply a spatial transformation to an image, how can you see the x-y coordinates when you display the image?

The answer has three steps:

1. Ask imtransform for the spatial coordinates of the output image

2. Give imshow the spatial coordinates when you display the output image

3. Turn on the axis box and tick labels

Let's walk through these steps using an affine transformation.

T = maketform('affine',[.5 0 0; .5 1.5 0; 100 200 1]);
I = imread('cameraman.tif');

The first output from imtransform is the transformed image. There are two more optional arguments, though: xdata and ydata. xdata is a two-element vector specifying the x-coordinates of the first and last image columns. Similarly, ydata specifies the y-coordinates of the first and last image rows.

[I2, xdata, ydata] = imtransform(I, T);
xdata
xdata =

   101   356

ydata
ydata =

  201.5000  584.5000

Step 2 is to pass the coordinate information to imshow. You can do that by using the 'XData' and 'YData' parameters, like this:

imshow(I2, 'XData', xdata, 'YData', ydata)

But we still can't see the coordinates! That's because imshow turns the axis display off. That brings us to step 3: Turn on the axis box and tick labels:

axis on

After you do these steps, you might also want to call impixelinfo, which turns on a display of pixel location and value in the figure window. The display updates as you move the mouse.


Get the MATLAB code

Published with MATLAB® 7.6

14 Responses to “Viewing output-space coordinates for a transformed image”

  1. Vijay replied on :

    Hi Steve,
    I would like to do a delaunay triangulation of an irregular volume and then apply a transformation (via imtranform) to the node points. I was checking out delaunayn, but it seems to work only on regular geometries. I want to apply the triangulation to a 3D image of an organ (ex: the brain) that has been segmented from an MR image. Do you have any suggestions?
    Thank you very much. -Vijay

  2. Steve replied on :

    Vijay—I don’t know what you mean by “seems to work only on regular geometries.” delaunayn doesn’t have such a restriction so far as I know. Also, about using imtransform … it is used for applying 2-D spatial transformations to images, whereas you sound like you want to apply 3-D spatial transformations to points. You would want to use tformfwd and tforminv for that.

  3. Mikr replied on :

    how to write pixel coordinates in file ?

  4. Steve replied on :

    Mikr—As far as I know, the commonly used image file formats such as TIFF, JPEG, PNG, etc., do not support storing spatial pixel coordinates within the file. There are format variations that do, such as GeoTIFF, but MATLAB does not have a function that writes a GeoTIFF file.

  5. Mikr replied on :

    thanks, Steve
    just started to learn matlab
    and to clarify
    matlab saves image files as a matrix of pixel coordinates
    matrix can be seen by imread without semicolon
    is this related to my previous question ? and does it make sence to export matrix from matlab ?

  6. Steve replied on :

    Mikr—An image in MATLAB is simply a matrix of pixel values. It can be saved (exported) to several common image file formats using imwrite.

  7. Mikr replied on :

    thanks
    but is it possible to see and write to file (Excel ?) that matrix of pixel coordinates ?
    instead of exporting one image format to another
    is that matrix which I see after command imread without semicolon ?

  8. Steve replied on :

    Mikr—You might want to take a look at the Getting Started section of the MATLAB documentation in order to become familiar with MATLAB usage and capabilities.

  9. Mikr replied on :

    I look for answers before asking people…

    “But we still can’t see the coordinates! That’s because imshow turns the axis display off. That brings us to step 3: Turn on the axis box and tick labels”

    but actually how to save those coordinates I have seen using your article.
    imwrite makes an image
    save does not support images…
    thanks

  10. swetha replied on :

    Hello Steve,

    Thankyou very much for these blogs,they have been very helpful in my projects. I understand how imtransform work,but i want to know how exactly the transformation takes place,like how we place the input image pixels after transformation in the output space. In short i want to do the transformation without using any matlab function. I just want to know which book to refer.
    Thankyou
    swetha

  11. Markus replied on :

    Hi Steve,

    I am trying to apply “tformfwd” with the
    following T-matrix [sx 0 0 ; sy 0 0 ; 0 0 1]
    in order to shrink a 2D contour.
    I understood that the images gets scaled
    with respect of the image origin (1,1). So
    I need to supply the origion of the contour
    to the “tformfwd” function.
    I calculated the origin of the contour by using “regionprops.centroid” and added the coordinates to each pixel of my contour, afterwards I applied the transformation. By it only gave reasonable results for scaling factor 1/2 and I don´t know why?
    Do you have any suggestions?

    Thank you
    markus

  12. Steve replied on :

    Swetha—How about Paul Heckbert’s Master’s thesis, “Fundamentals of Texture Mapping and Image Warping.” It’s available his web site.

  13. Steve replied on :

    Markus—Your description of what you were trying to accomplish and what you actually did is a bit vague. My only suggestion is that you apply a “sanity check” to your tform struct. Use tformfwd to transform a few points, and make sure that the points transform to the locations you expect. Also, make sure you are using the correct transform matrix for scaling - the scale factors should be on the diagonal. (I’m not sure whether your misplaced sy factor was just a typo.) Finally, do you really need all this machinery, anyway? Why don’t you just scale and shift the contour locations yourself? It might be a lot simpler.

  14. Markus replied on :

    Hi Steve,
    thanks, you are right, I don´t need all this machinery. I solved it like this:
    -shift the contour to the center (1,1) since the centroid of the contour was calculated,
    -scaling
    -shifting back
    T-Matrix: this was just a typo :)

    markus

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.