[Insight-users] Location of Origin

Luis Ibanez luis.ibanez at kitware.com
Wed Jul 11 10:31:19 EDT 2007


Hi Dan,


It is perfectly possible to get Index with negatives components
as output of the TransformPhysicalPointToIndex() method.


It all depends on what coordinates you pass in the point as input
to TransformPhysicalPointToIndex().


If the point is outside of the physical extent of the image, you
can get indices with negative components and you can get indices
with components that are larger than the number of pixels of the
image along that direction.


It is *your responsibility* to verify if an index is inside of the
extent of the image before you attempt to use it in an operation
such as GetPixel() or SetPixel().


A typical way to make this check is to use the "IsInside()" method
of the ImageRegion class. Something like:


    ImageType::RegionType region = image->GetBufferedRegion();

    if( region.IsInside( index ) )
      {
      ImageType::PixelType value = image->GetPixel( index );
      }


In your case, since you are using the TransformPhysicalPointToIndex()
method, you could also use the boolean returned by this method as
an indication of whether the point is inside of the
*LargestPossibleRegion*.


Something like:


     bool isInside = image->TransformPhysicalPointToIndex(point,index);
     if( isInside )
       {
       image->SetPixel( index, value );
       }




If too many points from your text file are falling outside of
your image, maybe you need to reconsider your choice of image
Origin, Number of Pixels and Pixel Spacing, so that your image
grid better covers the area where the points from your text
file are located.


You may find useful to read the ITK Software Guide


           http://www.itk.org/ItkSoftwareGuide.pdf


In particular the chapter on "Data Representation", and its
"Image" section.



BTW: There is an ITK filter that provides the functionality
      of mapping points into an image. Which seems to be what
      you are trying to do.


      You may want to look at:
http://www.itk.org/Insight/Doxygen/html/classitk_1_1PointSetToImageFilter.html

      The code is in

        Insight/Code/BasicFilters/itkPointSetToImageFilter.h
        Insight/Code/BasicFilters/itkPointSetToImageFilter.txx




Regards,


     Luis


---------------------
Dan Homerick wrote:
> Hi Luis,
> 
> Thanks for the response!
> 
> In my case I wasn't getting negative physical coordinates (a perfectly 
> reasonable thing), I was getting negative Index values (a perfectly 
> segfault thing)!
> 
> I was building up an image from raw data points in an text file, using 
> image->TransformPhysicalPointToIndex in order to decide where in the 
> image to place each data point.
> 
> The cause of the negative Index values? It's geographic data, and the 
> coordinates are from the Northern/Western hemisphere -- so my "x-axis" 
> (i.e. longitude) was increasing in the opposite direction than expected, 
> sending my data points off in the wrong direction (and off of the image).
> 
> My solution is, for now, to just set my origin corresponding to the 
> South-Eastern corner (lower-right, or "the last pixel that would be 
> reached by an iterator"). The image "looks" upside-down AND horizontally 
> flipped, but the coordinates seem to come out correct when I retrieve 
> them, which is what matters.
> 
> Thanks again, the explanation and clarification did help quite a bit in 
> figuring out why it was behaving the way it was.
> 
>  - Dan
> 
> 
> On 7/9/07, * Luis Ibanez* <luis.ibanez at kitware.com 
> <mailto:luis.ibanez at kitware.com>> wrote:
> 
> 
> 
>     Hi Dan,
> 
>     The origin is expected to be in the lower left corner of an image
>     and it is
>     expected to be expressed in physical coordinates (not pixels),
>     e.g. millimeters or centimeters.
> 
>     Note that the notion of "left / right" and "upper / lower" is
>     a *VISUALIZATION* notion.
> 
>     What appears on your screen up or down, and left or right, depend on
>     what image viewer you are using..
> 
>     We will strongly encourage you to use a viewer designed for medical
>     applications,
>     not a generic viewer such as the default fax viewer that you get on
>     Windows, or
>     the generic display from ImageMagick.
> 
> 
>     You will find a minimalistic viewer in the
>     InsightApplications/ImageViewer directory.
>     You can also download Windows binaries of this application from :
> 
>          http://public.kitware.com/pub/itk/InsightApplicationsBin/
> 
> 
>     In order to make sense of the conversions between indices and
>     physical coordinates
>     you must start by printing out the details of your image using
> 
>                image->Print( std::cout )
> 
>     and gathering the information about:
> 
>         *          image origin
>         *          pixel spacing
> 
> 
>     Note that some images may indeed have negative spacing,
>     and/or negative origin values.
> 
> 
>     Regards,
> 
> 
>         Luis
> 
> 
>     -------------------------------------------------------------------------------------------
>     On 7/9/07, *Dan Homerick * < danhomerick at gmail.com
>     <mailto:danhomerick at gmail.com>> wrote:
> 
>         A hopefully very quick question -- should the origin be set to
>         the physical coordinates of the upper left corner of the image,
>         or the lower left corner?
> 
>         I would assume upper left, but the diagram in the Software Guide
>         seems to indicate lower left. If it specifies in the text
>         anywhere, I must be overlooking it.
> 
>         I keep getting negative numbers when I use
>         image->TransformPhysicalPointToIndex, and while it's probably a
>         bug somewhere upstream in my code, if anyone knows of a common
>         mistake that will cause that, please let me know.
> 
>         Cheers,
>          - Dan
> 
>         _______________________________________________
>         Insight-users mailing list
>         Insight-users at itk.org <mailto:Insight-users at itk.org>
>         http://www.itk.org/mailman/listinfo/insight-users
> 
> 
> 


More information about the Insight-users mailing list