[Insight-users] ITK Rearrange slices in volume
Bradley Lowekamp
blowekamp at mail.nih.gov
Sun Apr 7 14:43:28 EDT 2013
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
More information about the Insight-users
mailing list