[vtkusers] problem to convert type signed to unsigned

David Gobbi david.gobbi at gmail.com
Thu Dec 4 17:12:44 EST 2014


Hi Yosbanis,

Here's a little thought experiment.  What is the result of the
following code?

  short x = -1;
  unsigned short y = static_cast<unsigned short>(x);

If you understand the code above, then you'll understand why
performing a cast-to-unsigned is only a good thing to do if all
of your data values are positive.

CT data, for example, usually has negative values and a simple
"cast" operation will do Bad Things to the data.

I recommend using vtkImageShiftScale to do the conversion
instead, with appropriate "shift" (and a "scale" if necessary)
so that all the resulting data values fit into an unsigned short.
For example, if the smallest pixel value in the CT is -1000,
then a Shift of +1000 will make all the values positive.  Ideally,
you should measure the pixel value for "air" and shift so that
air has a value of zero.

The ClampOverflowOn() method can be used as an extra
precaution.

 - David



On Thu, Dec 4, 2014 at 12:34 PM, Yosbanis Vicente Gonzalez <
90yobi90 at gmail.com> wrote:

> hi i am new to working with vtk. I'm trying to render a volume of a DICOM
> image.
> I was able to open a single file with vtkGDCMImageReader (using
> SetFileName), but when I render with vtkVolumeRayCastIsosurfaceFunction
> and vtkVolumeRayCastMapper gives me the following error:
>
> ERROR: In /../vtk/VTK5.10.1/VolumeRendering/vtkVolumeRayCastMapper.cxx,
> line 326
> vtkVolumeRayCastMapper (0x247bbe0): Can not render volume data of type
> short, only unsigned char or unsigned short.
>
> Do some research and use the vtkImageCast filter to convert from short to unsigned
> short type, but not solved the problem. This is the code for what I did to
> see if anyone can tell me what I did wrong.
> Thank you
>
> ------------------------------------------------------
> reader = vtkgdcm.vtkGDCMImageReader()
> reader.FileLowerLeftOn()
>
> reader.SetFileName('/home/yosbanis/images/d_david/cd1/dicom/14040312/31200000/24479448')
> reader.Update()
>
>
>
> # orientaciones
> #----------------------------------------------------------------
>
> orientations = {
>     gdcm.Orientation.SAGITTAL: 0,
>     gdcm.Orientation.CORONAL: 1,
>     gdcm.Orientation.AXIAL: 2
> }
> orientacion_gdcm = gdcm.Orientation()
> orientation =
> orientations[orientacion_gdcm.GetType(reader.GetImageOrientationPatient())]
>
> image = reader.GetOutput()
> origin = list(reader.GetOutput().GetOrigin())
> extent = list(reader.GetOutput().GetExtent())
> spacing = list(reader.GetOutput().GetSpacing())
>
> axial = vtk.vtkMatrix4x4()
> axial.DeepCopy((
>     1,  0,  0, 0,
>     0,  1,  0, 0,
>     0,  0,  1, 0,
>     0,  0,  0, 1
> ))
>
> coronal = vtk.vtkMatrix4x4()
> coronal.DeepCopy((
>     1,  0,  0, 0,
>     0,  0,  1, 0,
>     0,  1,  0, 0,
>     0,  0,  0, 1
> ))
>
> sagittal = vtk.vtkMatrix4x4()
> sagittal.DeepCopy((
>     0,  0, -1, 0,
>     1,  0,  0, 0,
>     0, -1,  0, 0,
>     0,  0,  0, 1
> ))
>
> axes = {
>     0: sagittal, 1: coronal, 2: axial
> }
>
> dc = reader.GetDirectionCosines()
>
> # obtener las coordenadas del cursor
> #-------------------------------------------
> def get_coordinate_cursor():
>     # Find position
>     x, y, z = pick.GetPickPosition()
>     bounds = [0, 0, 0, 0, 0, 0]
>     volume.GetBounds(bounds)
>     if bounds[0] == bounds[1]:
>         print 'X'
>         x = bounds[0]
>     elif bounds[2] == bounds[3]:
>         print 'Y'
>         y = bounds[2]
>     elif bounds[4] == bounds[5]:
>         print 'Z'
>         z = bounds[4]
>     return x, y, z
>
> def center_image():
>     image = reader.GetOutput()
>     origin = image.GetOrigin()
>     extent = image.GetExtent()
>     spacing = image.GetSpacing()
>
>     xc = origin[0] + 0.5 * (extent[0] + extent[1]) * spacing[0]
>     yc = origin[1] + 0.5 * (extent[2] + extent[3]) * spacing[1]
>     zc = origin[2] + 0.5 * (extent[4] + extent[5]) * spacing[2]
>
>     camera = renderer.GetActiveCamera()
>     focal_point = camera.GetFocalPoint()
>     dist = camera.GetDistance()
>
>     if orientation == 0:
>
>         camera.SetFocalPoint(focal_point[0], yc, zc)
>         camera.SetPosition(dist, yc, zc)
>
>     elif orientation == 1:
>
>         camera.SetFocalPoint(xc, focal_point[1], zc)
>         camera.SetPosition(xc, -dist, zc)
>
>     else:
>
>         camera.SetFocalPoint(xc, yc, focal_point[2])
>         camera.SetPosition(xc, yc, -dist)
>
>     renderer.ResetCamera()
>     renderer.ResetCameraClippingRange()
>
>     render_window.Render()
>
>
> def fit_image():
>     image = reader.GetOutput()
>     extent = image.GetExtent()
>     spacing = image.GetSpacing()
>
>     xd = (extent[1] - extent[0] + 1) * spacing[0]
>     yd = (extent[3] - extent[2] + 1) * spacing[1]
>     zd = (extent[5] - extent[4] + 1) * spacing[2]
>
>     center_image()
>     camera = renderer.GetActiveCamera()
>     if orientation == 2:
>         camera.SetParallelScale(0.5 * yd)
>     elif orientation == 1:
>         camera.SetParallelScale(0.5 * zd)
>     elif orientation == 0:
>         camera.SetParallelScale(0.5 * xd)
>     render_window.Render()
>
> def set_view_plane(view_plane):
>     if view_plane == 0:
>         left_to_right = [0, 1, 0, 0]
>         view_up = [0, 0, 1, 0]
>     elif view_plane == 1:
>         left_to_right = [1, 0, 0, 0]
>         view_up = [0, 0, 1, 0]
>     else:
>         left_to_right = [1, 0, 0, 0]
>         view_up = [0, -1, 0, 0]
>         m = axial
>
>     center_image()
>     style.SetImageOrientation(left_to_right[:3], view_up[:3])
>     bounds = [0, 0, 0, 0, 0, 0]
>     volume.GetBounds(bounds)
>     c.SetModelBounds(bounds)
>
>     render_window.Render()
>
>     global orientation
>     orientation = view_plane
>
> def flip_horizontal():
>     cam = renderer.GetActiveCamera()
>     position = cam.GetPosition()
>     factor = [1, 1, 1]
>     factor[orientation] = -1;
>
> cam.SetPosition(factor[0]*position[0],factor[1]*position[1],factor[2]*position[2])
>     renderer.ResetCameraClippingRange()
>     render_window.Render()
>
>
> def key_press(sender, event):
>     global focal_point, orientation
>
>     key = interactor.GetKeySym()
>     style.SetCurrentRenderer(renderer)
>
>     if key == 'a':
>         set_view_plane(2)
>     elif key == 'c':
>         set_view_plane(1)
>     elif key == 's':
>         set_view_plane(0)
>     elif key == 'f':
>         fit_image()
>     elif key == 'h':
>         flip_horizontal()
>     elif key == 'v':
>         cam = renderer.GetActiveCamera()
>         # flip horizontal
>         position = cam.GetPosition()
>         factor = [1, 1, 1]
>         factor[orientation] = -1;
>
> cam.SetPosition(factor[0]*position[0],factor[1]*position[1],factor[2]*position[2])
>
>         # now vertical
>         viewup = cam.GetViewUp();
>         cam.SetViewUp(-viewup[0], -viewup[1], -viewup[2])
>         renderer.ResetCameraClippingRange()
>         render_window.Render()
>
>     elif key == 'w':
>
>         print volumeProperty.GetColorWindow()
>
> #----------- trabajo del cursor y eventos -----------------------
> state = 0
> def start_cursor(orientation, e):
>     global state
>     state = True
>     _last_position = orientation.GetInteractor().GetEventPosition()
>
> def move_cursor(orientation, e):
>     if not state:
>         return
>
>     # pick the point
>     interactor = orientation.GetInteractor()
>     x, y = interactor.GetEventPosition()
>     interactor.GetPicker().Pick(x, y, 0, renderer)
>     x, y, z = interactor.GetPicker().GetPickPosition()
>
>     bounds = [0, 0, 0, 0, 0, 0]
>     volumeMapper.GetInput().GetBounds(bounds)
>     if bounds[0] == bounds[1]:
>         x = bounds[0]
>     elif bounds[2] == bounds[3]:
>         y = bounds[2]
>     elif bounds[4] == bounds[5]:
>         z = bounds[4]
>
>     cursor_coordinates = (x, y, z)
>     c.SetFocalPoint(cursor_coordinates)
>     render_window.Render()
>
> def stop_cursor(orientation, e):
>     global state
>     state = False
>
> interactor = vtk.vtkRenderWindowInteractor()
> render_window = vtk.vtkRenderWindow()
> render_window.SetNumberOfLayers(2)
> render_window.SetSize(800, 600)
> interactor.SetRenderWindow(render_window)
>
> style = vtk.vtkInteractorStyleImage()
> style.SetInteractionModeToImageSlicing()
> interactor.SetInteractorStyle(style)
> style.AddObserver('CharEvent', key_press)
> style.AddObserver('LeftButtonPressEvent', start_cursor)
> style.AddObserver('LeftButtonReleaseEvent', stop_cursor)
> style.AddObserver('MouseMoveEvent', move_cursor)
>
> c = vtk.vtkCursor3D()
> c.AllOff()
> c.AxesOn()
>
> volumeColor = vtk.vtkColorTransferFunction()
> volumeColor.AddRGBPoint(0,0.0,0.0,0.0)
> volumeColor.AddRGBPoint(180,0.3,0.1,0.2)
> volumeColor.AddRGBPoint(1000,1.0,0.7,0.6)
> volumeColor.AddRGBPoint(2000,1.0,1.0,0.9)
>
> volumeScalarOpacity = vtk.vtkPiecewiseFunction()
> volumeScalarOpacity.AddPoint(0,0.0)
> volumeScalarOpacity.AddPoint(180,0.0)
> volumeScalarOpacity.AddPoint(1000,0.2)
> volumeScalarOpacity.AddPoint(2000,0.8)
>
> volumeProperty = vtk.vtkVolumeProperty()
> volumeProperty.SetColor(volumeColor)
> volumeProperty.SetScalarOpacity(volumeScalarOpacity)
> volumeProperty.SetInterpolationTypeToLinear()
> volumeProperty.ShadeOn()
> volumeProperty.SetAmbient(0.6)
> volumeProperty.SetDiffuse(0.6)
> volumeProperty.SetSpecular(0.8)
>
> # use vtkImageCast!!!!!
>
> imageCast = vtk.vtkImageCast()
> imageCast.SetInput( reader.GetOutput() )
> imageCast.SetOutputScalarTypeToUnsignedShort()
>
> isosurfaceFunction = vtk.vtkVolumeRayCastIsosurfaceFunction()
> isosurfaceFunction.SetIsoValue(600)
> volumeMapper =  vtk.vtkVolumeRayCastMapper()
> volumeMapper.SetInput(imageCast.GetOutput())
> volumeMapper.SetVolumeRayCastFunction(isosurfaceFunction)
> volumeMapper.SetInputConnection(reader.GetOutputPort())
>
>
> volume = vtk.vtkVolume()
> volume.SetMapper(volumeMapper)
> volume.SetProperty(volumeProperty)
> volume.SetUserMatrix(axes[orientation])
>
> m = vtk.vtkPolyDataMapper()
> m.SetInputConnection(c.GetOutputPort())
>
> a = vtk.vtkActor()
> a.SetMapper(m)
> a.GetProperty().SetColor(1.0, 0.46, 0)
> a.SetVisibility(1)
> a.PickableOff()
>
>
> renderer = vtk.vtkRenderer()
> renderer.SetLayer(0)
> renderer.AddViewProp(volume)
>
> renderer.GetActiveCamera().ParallelProjectionOn()
> renderer.ResetCamera()
>
> renderer1 = vtk.vtkRenderer()
> renderer1.SetLayer(1)
> renderer1.AddViewProp(a)
> renderer1.SetInteractive(0)
> renderer1.SetActiveCamera(renderer.GetActiveCamera())
>
> render_window.AddRenderer(renderer1)
> render_window.AddRenderer(renderer)
>
> style.SetCurrentRenderer(renderer)
> set_view_plane(orientation)
>
> c.SetFocalPoint(renderer.GetActiveCamera().GetFocalPoint())
>
>
> render_window.Render()
> interactor.Initialize()
> interactor.Start()
>
> Please I need an answer
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20141204/c7cc028c/attachment.html>


More information about the vtkusers mailing list