[Insight-users] ITK Rearrange slices in volume

Phillip Ward pgwar1 at student.monash.edu
Sun Apr 7 18:24:37 EDT 2013


Hey Bradley,

Thank's for the advice. This is a rather small part of a larger pipeline
that will get bigger soon. I'd like to avoid re-writing it all in python if
I can.

Currently the pipeline is taking real and imaginary components of a 4D
dataset and combining them to make a complex dataset. Then two nested
SliceBySlice filters are used to iterate over the inner most 2 dimensions,
performing a FFT shift, an IFFT, and another FFT shift. After this the 4D
FFT inverted dataset is reduced over the 4th dimension using a square
filter and an accumulate filter. This all works as it should and
I've verified it. I didn't want to include the source as it is quite long.

After viewing the output of the pipeline I noticed the slices are
interleaved, most likely due to the capture protocol on the device they
originated from. So now I'm attempting to reorder them correctly before
performing any 3D segmentation.

Below is a code segment from my current Paste filter in a loop code.
accumulateFilter->GetOutput() is the end of the pipeline proceeding this
code. If I do 1 iteration of the loop, I can extract one slice and have it
in the correct position in the output image. Any more and I receive a blank
image. The part of the code I'm most unhappy about is the method of reusing
the PasteFilter. I'm fairly certain using the same input
(accumulateFilter->GetOutput()) and trying to reusing the destination
(sortedImage) is where it is failing.

                // Sort image slices
                // Allocate space for sorted image
                ImageType::Pointer sortedImage = ImageType::New();

sortedImage->SetRegions(accumulateFilter->GetOutput()->GetLargestPossibleRegion());
                sortedImage->Allocate();

                // Size of slices to be sorted
                ImageType::SizeType sourceSize;
                sourceSize[0] = Kx;
                sourceSize[1] = Ky;
                sourceSize[2] = 1;
                sourceSize[3] = 1;

                PasteFilterType::Pointer pasteFilter = PasteFilterType::New
();
                ImageType::RegionType sourceRegion;

                for (int i = 0; i < Ns; ++i)
                {
                   ImageType::IndexType sourceIndex;
                   sourceIndex[0] = 0;
                   sourceIndex[1] = 0;
                   sourceIndex[2] = i;
                   sourceIndex[3] = 0;

                   ImageType::IndexType destinationIndex;
                   if (i%2 == 0) {
                      destinationIndex[0] = 0;
                      destinationIndex[1] = 0;
                      destinationIndex[2] = i/2;
                      destinationIndex[3] = 0;
                   } else {
                      destinationIndex[0] = 0;
                      destinationIndex[1] = 0;
                      destinationIndex[2] = i/2 + Ns/2 + Ns%2;
                      destinationIndex[3] = 0;
                   }

                   sourceRegion.SetSize(sourceSize);
                   sourceRegion.SetIndex(sourceIndex);

                   // Paste even slices before odd slices
                   pasteFilter = PasteFilterType::New ();

 pasteFilter->SetSourceImage(accumulateFilter->GetOutput());
                   pasteFilter->SetDestinationImage(sortedImage);
                   pasteFilter->SetSourceRegion(sourceRegion);
                   pasteFilter->SetDestinationIndex(destinationIndex);

                   sortedImage = pasteFilter->GetOutput();
                }

Cheers,
Phil



On 8 April 2013 04:43, Bradley Lowekamp <blowekamp at mail.nih.gov> wrote:

> Hello Phil,
>
> It this is the try of problem that SimpleITK excels out. A lot of the time
> I like to prototype in SimpleITK, then implement it as an ITK pipeline once
> I know how what filters I need, what parameters I need, how I want the
> pipeline to flow for efficiency.
>
> As it sound like you have been struggling for a while with a variety of
> some of ITK pipeline and other complication, I'd like to suggest you play
> with he parameters of the filters you are using in a SimpleITK IPython
> environment, then implement it in the ITK pipeline after you know what the
> filters should do. So that it'd separate the filter issues from the
> pipeline issues.
>
> There are quite a number of ways you could be trying to setup your
> pipeline, and it's not clear what your pipeline goals are. You also did not
> provide code, so I really can't give much of a suggestion
>
> Here is a quick solution in SimpleITK Python:
>
> import SimpleITK as sitk
> img = sitk.ReadImage( "myfile.mha")
> Nz = img.GetSize()[2]
> limg = [ img[:,:,z//2] if z%2 else img[:,:,Nz//2+z/2] for z in range(0,Nz)
> ]
> rimg = sitk.JoinSeries( limg, img.GetSpacing()[2] )
>
> Hopefully, that 4th statement isn't to much pythonic short hand. It uses
> list comprehension combined with sliced based indexing to for a list of 2D
> z-slices that are re-ordered as you specified.
>
> The statement "img[:,:,n]" say ":"- all in x, ":"-all in y,  and just the
> "n-th" slice. Internally it's implemented with an ExtractImageFilter to
> extract a 2D slice from a 3D image.
>
> Then the JoinSeriesImageFilter, just takes that re-oreded lists of slices
> an makes a 3D image from it. And the spacing from the old image is used
> again.
>
> From here you can convert to explicitly using the ExtractImageFilter and
> get closer to the C++ ITK proper code.
>
>
>
> On Apr 7, 2013, at 2:42 AM, Phillip Ward <pgwar1 at student.monash.edu>
> wrote:
>
> > Hey ITK community,
> >
> > I've got a problem I've been struggling with for a few weeks now and
> would like some assistance.
> >
> > I have a volume and I would like to rearrange the slices in it, i.e.,
> remap the third dimension of the volume. The volume is read from a single
> file, so renaming image files or reading in a different order is not
> suitable.
> >
> > Right now, the even slices are first (1,15) and the odd slices are
> second (16,30). So the map f(z) = (z%2 ? z/2 : Nz/2 + z/2), where Nz is the
> length of the third dimension, interleaves the even and odd into their
> correct order (for even Nz).
> >
> > I've attempted using a paste image filter in a loop to paste each slice
> one at a time
> > size=(Nx,Ny,1), destinationIndex=(0,0,f(z));
> > This produced a black image, which I suspect was a pipeline error on my
> behalf, but I haven't been able to work out how to implement this correctly.
> >
> > I've attempted using a resample image filter, but my knowledge of
> transformations was lacking and that failed also.
> >
> > I feel like there is an easier way to do this that I am perhaps
> overlooking, but either way I cannot implement it and would appreciate some
> advice.
> >
> > Cheers,
> > Phil
> > _____________________________________
> > 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://www.itk.org/mailman/listinfo/insight-users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20130408/3367ea17/attachment.htm>


More information about the Insight-users mailing list