[vtkusers] vtkImageReslicing from every major axis problem

David Gobbi david.gobbi at gmail.com
Sat Jun 17 00:47:14 EDT 2017

Hi Andaharoo,

It's possible to simplify things by using one transformation instead of
two: When using SetResliceAxesOrigin(), it's best to use
SetResliceAxesDirectionCosines() to set the orientation instead of

I'm going to define maxpos[] instead of extent[]:

maxpos[i] = (dim[i] - 1) * spacing[i]

The "- 1" is very important: the idea here is that the first slice is at
position 0, and the last slice is at position n-1 if "n" is the number of
slices.  Then we can set up vtkImageReslice as follows:

reslicer->SetResliceAxesOrigin(slice * spacing[0], 0.0, maxpos[2]);
reslicer->SetResliceAxesDirectionCosines(0.0, 1.0, 0.0,  0.0, 0.0, -1.0,
 -1.0, 0.0, 0.0);
reslicer->SetOutputOrigin(0.0, 0.0, 0.0);

The "direction cosines" specify the orientation of the extracted slices: in
the above example, (0, 1, 0) is in the "y" direction and (0, 0, -1) is in
the "-z" direction so it will show a slice from the YZ plane.  The third
direction (-1, 0, 0) is the slicing direction, and it must be set to the
cross product of the other two directions.

The reason that the ResliceAxesOrigin must be set to (slicepos, 0.0,
maxpos[2]) in this case is that the "z" component of one of the direction
cosines is negative.

 - David

On Wed, Jun 14, 2017 at 11:35 PM, Andaharoo <Andx_roo at live.com> wrote:

> I have a program that shows the three major axii of a volume using
> vtkImageViewer2 and I want to output a single image given the x, y, or z
> slice number. I've been using vtkImageReslice to accomplish this task.
> Outputting the XY plane is easy, I just do:
> reslicer->setResliceAxesOrigin(0, 0, slice * spacing[2]);
> I don't have to transform the plane as XY is default. Then I just set the z
> of the plane, which in physical space would be the slice number * spacing.
> Now to do the same thing but with the YZ plane I do:
> reslicer->SetResliceAxesOrigin(0, 0, slice * spacing[0]);
> transform->Translate(extent[0], extent[1], extent[2]);
> transform->RotateY(90);
> transform->Translate(-extent[0], -extent[1], -extent[2]);
> reslicer->SetResliceTransform(transform);
> Note: extent[i] = dim[i] * spacing[i], not vtk's image data extent.
> So I transform to the origin, then rotate the plane on the Y axis. I've
> noticed that the axes origin gets applied after transform so the slice
> number that would have been on the x axis should be specified as z in
> SetResliceAxesOrigin. Also to get the correct depth of the slice I need the
> spacing from the corresponding axis in the image which would be x axis
> spacing or spacing[0].
> Now the problem is that while the transform works perfectly the slice
> number
> isn't the correct one. In fact it's a suspicious number.
> With this input image:
> Dim = 240, 240, 163
> Spacing = 1.0444, 1.0444, 1.0999
> Dim with Spacing (size) ~ 249, 249, 179
> When I try to export the middle slice, 120, I get the wrong slice. Now I
> noticed if I swap the spacing with the spacing of the z axis (spacing[2])
> then I get the middle slice of the z axis. Slice 81 (163/2 floored). Which
> makes me think the slice is somehow in z axis scale. So I tried manually
> rescaling the axesorigin by normalizing the slice. frac = slice / maxslice
> or 0.5 = 120 / 240. Then frac * size[2] or 0.5 * 163 * 1.0999. But the
> scaling is still off. While I get the correct slice from 120 all the others
> are still off.
> --
> View this message in context: http://vtk.1045678.n5.nabble.
> com/vtkImageReslicing-from-every-major-axis-problem-tp5743646.html
> Sent from the VTK - Users mailing list archive at Nabble.com.
> _______________________________________________
> Powered by www.kitware.com
> Visit other Kitware open-source projects at http://www.kitware.com/
> opensource/opensource.html
> Please keep messages on-topic and check the VTK FAQ at:
> http://www.vtk.org/Wiki/VTK_FAQ
> Search the list archives at: http://markmail.org/search/?q=vtkusers
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/vtkusers
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20170616/7e49ab04/attachment.html>

More information about the vtkusers mailing list