[ITK-users] [ITK] Resampling after SetDirection() and SetOrigin()

Matt McCormick matt.mccormick at kitware.com
Thu Jul 3 12:24:47 EDT 2014


Hi Mathew,

The reader and the resampler are connected in a pipeline by default.
The Update() on the writer or the resampler will nullify your
modification on "image". To disconnect the pipeline,

  reader.Update()
  image = reader.GetOutput()
  image.DisconnectPipeline()

See also [1].

Hope this helps,
Matt

[1] http://www.itk.org/Doxygen/html/classitk_1_1DataObject.html#a8a70b06ebb65da85628342f3e992bc4c

On Wed, Jul 2, 2014 at 12:14 PM, Mathew Guilfoyle <mrguilfoyle at gmail.com> wrote:
> Hi Matt
>
> Sorry to labour this point but I'm still confused as to what is going on; my
> misunderstanding may well be to do with how the pipelines work.
>
> As an example:
>
> import itk
>
> fileNameIn = '/route/to/input.nii'
> fileNameOut = '/route/to/output.nii'
> image_type = itk.Image[itk.F, 3]
> reader = itk.ImageFileReader[image_type].New()
> reader.SetFileName(fileNameIn)
> reader.Update()
>
> image = reader.GetOutput()
> #image.GetOrigin() gives [-148, -153,  55]
> #image.GetDirection().GetVnlMatrix().get(0,0)....(2,2)
> #gives matrix:  1.0     0       0
> #               0       0.95    0.3
> #               0       -0.3    0.95
>
> image.GetOrigin().Fill(0)
> image.GetDirection().SetIdentity()
> #image.GetOrigin() gives [0, 0, 0]
> #image.GetDirection().GetVnlMatrix().get(0,0)....(2,2)
> #gives matrix:  1.0     0       0
> #               0       1.0     0
> #               0       0       1.0
>
> writer = itk.ImageFileWriter[image_type].New()
> writer.SetFileName(fileNameOut)
> writer.SetInput(image)
> writer.Update()
> #output image volume is unchanged when viewed in (e.g.) ImageJ
>
> resampler = itk.ResampleImageFilter[image_type, image_type].New()
> resampler.SetInput(image)
> resampler.SetSize(image.GetLargestPossibleRegion().GetSize())
> resampler.SetOutputOrigin(image.GetOrigin())
> resampler.SetOutputSpacing(image.GetSpacing())
> resampler.SetOutputDirection(image.GetDirection())
> resampler.Update()
>
> writer.SetInput(resampler.GetOutput())
> writer.Update()
> #now output is translated and rotated by the (inverse) values of the
> original
> #origin vector and direction matrix
> #
> #However, resampler.GetInput().GetOrigin() gives [0,0,0]
> #and similarly the direction of the input is a 3x3 identity matrix
>
> I don't understand why the resampler changes the image volume since it
> should not be 'aware' of the original origin/direction.
>
> From your previous reply you suggest that the call to resampler.Update()
> might cause the reader to re-import the file from disk.  Is this the case
> even when the reader and resampler are not connected in a pipeline (as
> here).  Further, if the reader does re-import the original image and
> origin/direction parameters why does resampler.GetInput().GetOrigin() report
> [0, 0, 0] and  the input direction matrix is an identity?
>
> I want to understand why this is happening as it is clearly important for
> ensuring I set up more complex pipelines correctly.
>
> Many thanks
> Mathew
>
>
> On 30 June 2014 03:28, Matt McCormick <matt.mccormick at kitware.com> wrote:
>>
>> Hi Mathew,
>>
>> The image does not retain the original parameters somewhere.  However,
>> can must be take, because if the image is generated by a filter in a
>> pipeline, that filter will reset the original value when the pipeline
>> is re-run. ChangeInformationImageFilter will ensure this reset does
>> not happen during a pipeline update.
>>
>> The Set methods doo reorient/reposition the whole image volume in
>> world space.  The resampling filter does not select a subregion or
>> rotates the images -- to get the same out, the  sampling locations of
>> the output image for the ResampleImageFilter must be set as desired.
>> Set SetOutputOrigin [1], SetOutputDirection [2], etc.
>>
>> Hope this helps to clarify.
>>
>> I checked by wrapped install, and I have ChangeInformationImageFilter
>> instantiations for float images.
>>
>> Hope this helps,
>> Matt
>>
>> [1]
>> http://www.itk.org/Doxygen/html/classitk_1_1ResampleImageFilter.html#ac4b197ddefd3b3344bd9c9471acd5b00
>>
>> [2]
>> http://www.itk.org/Doxygen/html/classitk_1_1ResampleImageFilter.html#a9fdd6b862ab6cbaa03702da393bd0474
>>
>>
>> On Sun, Jun 29, 2014 at 10:01 PM, Mathew Guilfoyle
>> <mrguilfoyle at gmail.com> wrote:
>> > Matt, thanks for the reply.
>> >
>> > I would be grateful if you could you explain why this is the desired
>> > behaviour.  If I use the SetDirection() or SetOrigin() methods does the
>> > image object retain the original parameters somewhere?  Subsequently
>> > calling
>> > GetDirection() or GetOrigin() reports the changed parameters.
>> >
>> > Intuitively you would expect both Set methods to reorient/reposition the
>> > whole image volume in world space, and on the face of it that is what
>> > they
>> > seem to do.  However, on passing the image to a resampling filter they
>> > act
>> > to select a subregion or to rotate the image. I would have thought that
>> > rotations and region selections should be options passed to the filter
>> > and
>> > not set in the image itself?
>> >
>> > How does the ChangeInformationImageFilter do it differently?
>> > Incidentally I
>> > have wrapped for float but don't get this as an option for this filter.
>> >
>> > Thanks
>> > Mathew
>> >
>> >
>> >
>> > On 30 June 2014 01:45, Matt McCormick <matt.mccormick at kitware.com>
>> > wrote:
>> >>
>> >> Hi Mathew,
>> >>
>> >> Yes, that is the correct and expected behavior. I do not know if
>> >> ImageJ is using the actual location in world space.
>> >>
>> >> It sounds like ChangeInformationImageFilter is what you are looking
>> >> for.  It will be wrapped for float images if wrapped for float images
>> >> is enabled in you CMake configuration (ITK_WRAP_float).
>> >>
>> >> Hope this helps,
>> >> Matt
>> >>
>> >>
>> >> On Sat, Jun 28, 2014 at 9:55 PM, Mathew Guilfoyle
>> >> <mrguilfoyle at gmail.com>
>> >> wrote:
>> >> > If I change an image's direction and origin using SetDirection() and
>> >> > SetOrigin() methods and then write the image back to a file, the
>> >> > volume
>> >> > stack viewed in (e.g.) ImageJ is unchanged.  This is the behaviour I
>> >> > would
>> >> > expect as changing the direction and origin shouldn't alter the array
>> >> > of
>> >> > image pixels, just its location and orientation in 'world space'?
>> >> >
>> >> > However, if I take the same image, adjust the origin and direction as
>> >> > before, and then input it to ResampleImageFilter with an identity
>> >> > transform
>> >> > (and set spacing, origin, direction, and size to those of the input
>> >> > image)
>> >> > the output is a translated and rotated image.
>> >> >
>> >> > It seems the difference between the original and adjusted origin and
>> >> > direction are applied as a translation and rotation during
>> >> > resampling,
>> >> > even
>> >> > though GetOrigin() and GetDirection() on the input image to the
>> >> > resampler
>> >> > return the adjusted parameters.
>> >> >
>> >> > Is this the expected behaviour? Is there another image parameter that
>> >> > needs
>> >> > to be changed to correctly alter origin and direction?
>> >> >
>> >> > (ChangeInformationImageFilter is not wrapped for float images so I
>> >> > can't
>> >> > use
>> >> > it for my application)
>> >> >
>> >> > Thanks
>> >> >
>> >> > _____________________________________
>> >> > Powered by www.kitware.com
>> >> >
>> >> > Visit other Kitware open-source projects at
>> >> > http://www.kitware.com/opensource/opensource.html
>> >> >
>> >> > Kitware offers ITK Training Courses, for more information visit:
>> >> > http://www.kitware.com/products/protraining.php
>> >> >
>> >> > Please keep messages on-topic and check the ITK FAQ at:
>> >> > http://www.itk.org/Wiki/ITK_FAQ
>> >> >
>> >> > Follow this link to subscribe/unsubscribe:
>> >> > http://public.kitware.com/mailman/listinfo/insight-users
>> >> >
>> >> > _______________________________________________
>> >> > Community mailing list
>> >> > Community at itk.org
>> >> > http://public.kitware.com/mailman/listinfo/community
>> >> >
>> >
>> >
>
>


More information about the Insight-users mailing list