<div dir="ltr"><div>Hi Yosbanis,</div><div><br></div><div>Here's a little thought experiment.  What is the result of the</div><div>following code?</div><div><br></div><div>  short x = -1;</div><div>  unsigned short y = static_cast<unsigned short>(x);</div><div><br></div><div>If you understand the code above, then you'll understand why</div><div>performing a cast-to-unsigned is only a good thing to do if all</div><div>of your data values are positive.</div><div><br></div><div>CT data, for example, usually has negative values and a simple</div><div>"cast" operation will do Bad Things to the data.</div><div><br></div><div>I recommend using vtkImageShiftScale to do the conversion</div><div>instead, with appropriate "shift" (and a "scale" if necessary)</div><div>so that all the resulting data values fit into an unsigned short.</div><div>For example, if the smallest pixel value in the CT is -1000,</div><div>then a Shift of +1000 will make all the values positive.  Ideally,</div><div>you should measure the pixel value for "air" and shift so that</div><div>air has a value of zero.</div><div><br></div><div>The ClampOverflowOn() method can be used as an extra</div><div>precaution.</div><div><br></div><div> - David</div><div><br></div><br><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Dec 4, 2014 at 12:34 PM, Yosbanis Vicente Gonzalez <span dir="ltr"><<a href="mailto:90yobi90@gmail.com" target="_blank">90yobi90@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><span lang="en"><span>hi i</span> <span>am new to</span> <span>working with</span> <span>vtk</span><span>.</span> <span>I'm trying to</span> <span>render a</span> <span>volume of a</span> <span>DICOM</span> <span>image.</span><br> <span>I was able to</span> <span>open a</span> <span>single</span> <span>file with</span> <span>vtkGDCMImageReader</span> <span>(using</span> <span>SetFileName</span><span>), but when</span> <span>I render</span> <span>with</span> <span>vtkVolumeRayCastIsosurfaceFunction</span> <span>and</span> <span>vtkVolumeRayCastMapper</span> <span>gives me the following</span> <span>error:</span><br><br> <span>ERROR:</span> <span>In</span> <span>/../vtk/VTK5.10.1/VolumeRendering/vtkVolumeRayCastMapper.cxx</span><span>, line</span> <span>326</span><br> <span>vtkVolumeRayCastMapper</span> <span>(</span><span>0x247bbe0</span><span>)</span><span>: Can not</span> <span>render</span> <span>volume</span> <span>data</span> <span>of type</span> <span>short</span><span>,</span> <span>only</span> <span>unsigned</span> <span>char</span> <span>or</span> <span>unsigned short</span><span>.</span><br><br> <span>Do some research and</span> <span>use the</span> <span>vtkImageCast</span> <span>filter to convert</span> <span>from</span> <span>short</span> <span>to</span> <span>unsigned short</span> <span>type, but not</span> <span>solved the</span> <span>problem.</span> <span>This is the code</span> <span>for what I did</span> <span>to see if anyone</span> <span>can tell me what</span> <span>I did wrong</span><span>.</span><br> <span>Thank you<br><br>------------------------------------------------------<br>reader = vtkgdcm.vtkGDCMImageReader()<br>reader.FileLowerLeftOn()<br>reader.SetFileName('/home/yosbanis/images/d_david/cd1/dicom/14040312/31200000/24479448')<br>reader.Update()<br><br><br><br># orientaciones<br>#----------------------------------------------------------------<br><br>orientations = {<br>    gdcm.Orientation.SAGITTAL: 0,<br>    gdcm.Orientation.CORONAL: 1,<br>    gdcm.Orientation.AXIAL: 2<br>}<br>orientacion_gdcm = gdcm.Orientation()<br>orientation = orientations[orientacion_gdcm.GetType(reader.GetImageOrientationPatient())]<br><br>image = reader.GetOutput()<br>origin = list(reader.GetOutput().GetOrigin())<br>extent = list(reader.GetOutput().GetExtent())<br>spacing = list(reader.GetOutput().GetSpacing())<br><br>axial = vtk.vtkMatrix4x4()<br>axial.DeepCopy((<br>    1,  0,  0, 0,<br>    0,  1,  0, 0,<br>    0,  0,  1, 0,<br>    0,  0,  0, 1<br>))<br><br>coronal = vtk.vtkMatrix4x4()<br>coronal.DeepCopy((<br>    1,  0,  0, 0,<br>    0,  0,  1, 0,<br>    0,  1,  0, 0,<br>    0,  0,  0, 1<br>))<br><br>sagittal = vtk.vtkMatrix4x4()<br>sagittal.DeepCopy((<br>    0,  0, -1, 0,<br>    1,  0,  0, 0,<br>    0, -1,  0, 0,<br>    0,  0,  0, 1<br>))<br><br>axes = {<br>    0: sagittal, 1: coronal, 2: axial<br>}<br><br>dc = reader.GetDirectionCosines()<br><br># obtener las coordenadas del cursor<br>#-------------------------------------------<br>def get_coordinate_cursor():<br>    # Find position<br>    x, y, z = pick.GetPickPosition()<br>    bounds = [0, 0, 0, 0, 0, 0]<br>    volume.GetBounds(bounds)<br>    if bounds[0] == bounds[1]:<br>        print 'X'<br>        x = bounds[0]<br>    elif bounds[2] == bounds[3]:<br>        print 'Y'<br>        y = bounds[2]<br>    elif bounds[4] == bounds[5]:<br>        print 'Z'<br>        z = bounds[4]<br>    return x, y, z<br><br>def center_image():<br>    image = reader.GetOutput()<br>    origin = image.GetOrigin()<br>    extent = image.GetExtent()<br>    spacing = image.GetSpacing()<br><br>    xc = origin[0] + 0.5 * (extent[0] + extent[1]) * spacing[0]<br>    yc = origin[1] + 0.5 * (extent[2] + extent[3]) * spacing[1]<br>    zc = origin[2] + 0.5 * (extent[4] + extent[5]) * spacing[2]<br><br>    camera = renderer.GetActiveCamera()<br>    focal_point = camera.GetFocalPoint()<br>    dist = camera.GetDistance()<br>    <br>    if orientation == 0:<br><br>        camera.SetFocalPoint(focal_point[0], yc, zc)<br>        camera.SetPosition(dist, yc, zc)<br><br>    elif orientation == 1:<br><br>        camera.SetFocalPoint(xc, focal_point[1], zc)<br>        camera.SetPosition(xc, -dist, zc)<br><br>    else:<br><br>        camera.SetFocalPoint(xc, yc, focal_point[2])<br>        camera.SetPosition(xc, yc, -dist)<br><br>    renderer.ResetCamera()<br>    renderer.ResetCameraClippingRange()<br><br>    render_window.Render()<br><br><br>def fit_image():<br>    image = reader.GetOutput()<br>    extent = image.GetExtent()<br>    spacing = image.GetSpacing()<br><br>    xd = (extent[1] - extent[0] + 1) * spacing[0]<br>    yd = (extent[3] - extent[2] + 1) * spacing[1]<br>    zd = (extent[5] - extent[4] + 1) * spacing[2]<br><br>    center_image()<br>    camera = renderer.GetActiveCamera()<br>    if orientation == 2:<br>        camera.SetParallelScale(0.5 * yd)<br>    elif orientation == 1:<br>        camera.SetParallelScale(0.5 * zd)<br>    elif orientation == 0:<br>        camera.SetParallelScale(0.5 * xd)<br>    render_window.Render()<br><br>def set_view_plane(view_plane):<br>    if view_plane == 0:<br>        left_to_right = [0, 1, 0, 0]<br>        view_up = [0, 0, 1, 0]<br>    elif view_plane == 1:<br>        left_to_right = [1, 0, 0, 0]<br>        view_up = [0, 0, 1, 0]<br>    else:<br>        left_to_right = [1, 0, 0, 0]<br>        view_up = [0, -1, 0, 0]<br>        m = axial<br><br>    center_image()<br>    style.SetImageOrientation(left_to_right[:3], view_up[:3])<br>    bounds = [0, 0, 0, 0, 0, 0]<br>    volume.GetBounds(bounds)<br>    c.SetModelBounds(bounds)<br><br>    render_window.Render()<br><br>    global orientation<br>    orientation = view_plane<br><br>def flip_horizontal():<br>    cam = renderer.GetActiveCamera()<br>    position = cam.GetPosition()<br>    factor = [1, 1, 1]<br>    factor[orientation] = -1;<br>    cam.SetPosition(factor[0]*position[0],factor[1]*position[1],factor[2]*position[2])<br>    renderer.ResetCameraClippingRange()<br>    render_window.Render()<br><br><br>def key_press(sender, event):<br>    global focal_point, orientation<br><br>    key = interactor.GetKeySym()<br>    style.SetCurrentRenderer(renderer)<br><br>    if key == 'a':<br>        set_view_plane(2)<br>    elif key == 'c':<br>        set_view_plane(1)<br>    elif key == 's':<br>        set_view_plane(0)<br>    elif key == 'f':<br>        fit_image()<br>    elif key == 'h':<br>        flip_horizontal()<br>    elif key == 'v':<br>        cam = renderer.GetActiveCamera()<br>        # flip horizontal<br>        position = cam.GetPosition()<br>        factor = [1, 1, 1]<br>        factor[orientation] = -1;<br>        cam.SetPosition(factor[0]*position[0],factor[1]*position[1],factor[2]*position[2])<br><br>        # now vertical<br>        viewup = cam.GetViewUp();<br>        cam.SetViewUp(-viewup[0], -viewup[1], -viewup[2])<br>        renderer.ResetCameraClippingRange()<br>        render_window.Render()<br><br>    elif key == 'w':<br>       <br>        print volumeProperty.GetColorWindow()<br><br>#----------- trabajo del cursor y eventos -----------------------<br>state = 0<br>def start_cursor(orientation, e):<br>    global state<br>    state = True<br>    _last_position = orientation.GetInteractor().GetEventPosition()<br><br>def move_cursor(orientation, e):<br>    if not state:<br>        return<br><br>    # pick the point<br>    interactor = orientation.GetInteractor()<br>    x, y = interactor.GetEventPosition()<br>    interactor.GetPicker().Pick(x, y, 0, renderer)<br>    x, y, z = interactor.GetPicker().GetPickPosition()<br><br>    bounds = [0, 0, 0, 0, 0, 0]<br>    volumeMapper.GetInput().GetBounds(bounds)<br>    if bounds[0] == bounds[1]:<br>        x = bounds[0]<br>    elif bounds[2] == bounds[3]:<br>        y = bounds[2]<br>    elif bounds[4] == bounds[5]:<br>        z = bounds[4]<br><br>    cursor_coordinates = (x, y, z)<br>    c.SetFocalPoint(cursor_coordinates)<br>    render_window.Render()<br><br>def stop_cursor(orientation, e):<br>    global state<br>    state = False<br><br>interactor = vtk.vtkRenderWindowInteractor()<br>render_window = vtk.vtkRenderWindow()<br>render_window.SetNumberOfLayers(2)<br>render_window.SetSize(800, 600)<br>interactor.SetRenderWindow(render_window)<br><br>style = vtk.vtkInteractorStyleImage()<br>style.SetInteractionModeToImageSlicing()<br>interactor.SetInteractorStyle(style)<br>style.AddObserver('CharEvent', key_press)<br>style.AddObserver('LeftButtonPressEvent', start_cursor)<br>style.AddObserver('LeftButtonReleaseEvent', stop_cursor)<br>style.AddObserver('MouseMoveEvent', move_cursor)<br><br>c = vtk.vtkCursor3D()<br>c.AllOff()<br>c.AxesOn()<br><br>volumeColor = vtk.vtkColorTransferFunction()<br>volumeColor.AddRGBPoint(0,0.0,0.0,0.0)<br>volumeColor.AddRGBPoint(180,0.3,0.1,0.2)<br>volumeColor.AddRGBPoint(1000,1.0,0.7,0.6)<br>volumeColor.AddRGBPoint(2000,1.0,1.0,0.9)<br><br>volumeScalarOpacity = vtk.vtkPiecewiseFunction()<br>volumeScalarOpacity.AddPoint(0,0.0)<br>volumeScalarOpacity.AddPoint(180,0.0)<br>volumeScalarOpacity.AddPoint(1000,0.2)<br>volumeScalarOpacity.AddPoint(2000,0.8)<br><br>volumeProperty = vtk.vtkVolumeProperty()<br>volumeProperty.SetColor(volumeColor)<br>volumeProperty.SetScalarOpacity(volumeScalarOpacity)<br>volumeProperty.SetInterpolationTypeToLinear()<br>volumeProperty.ShadeOn()<br>volumeProperty.SetAmbient(0.6)<br>volumeProperty.SetDiffuse(0.6)<br>volumeProperty.SetSpecular(0.8)<br><br></span></span></div><span lang="en"><span># use vtkImageCast!!!!!<br></span></span><div><span lang="en"><span><br>imageCast = vtk.vtkImageCast()<br>imageCast.SetInput( reader.GetOutput() )<br>imageCast.SetOutputScalarTypeToUnsignedShort()<br><br>isosurfaceFunction = vtk.vtkVolumeRayCastIsosurfaceFunction()<br>isosurfaceFunction.SetIsoValue(600)<br>volumeMapper =  vtk.vtkVolumeRayCastMapper()<br>volumeMapper.SetInput(imageCast.GetOutput())<br>volumeMapper.SetVolumeRayCastFunction(isosurfaceFunction)<br>volumeMapper.SetInputConnection(reader.GetOutputPort())<br><br><br>volume = vtk.vtkVolume()<br>volume.SetMapper(volumeMapper)<br>volume.SetProperty(volumeProperty)<br>volume.SetUserMatrix(axes[orientation])<br><br>m = vtk.vtkPolyDataMapper()<br>m.SetInputConnection(c.GetOutputPort())<br><br>a = vtk.vtkActor()<br>a.SetMapper(m)<br>a.GetProperty().SetColor(1.0, 0.46, 0)<br>a.SetVisibility(1)<br>a.PickableOff()<br><br><br>renderer = vtk.vtkRenderer()<br>renderer.SetLayer(0)<br>renderer.AddViewProp(volume)<br><br>renderer.GetActiveCamera().ParallelProjectionOn()<br>renderer.ResetCamera()<br><br>renderer1 = vtk.vtkRenderer()<br>renderer1.SetLayer(1)<br>renderer1.AddViewProp(a)<br>renderer1.SetInteractive(0)<br>renderer1.SetActiveCamera(renderer.GetActiveCamera())<br><br>render_window.AddRenderer(renderer1)<br>render_window.AddRenderer(renderer)<br><br>style.SetCurrentRenderer(renderer)<br>set_view_plane(orientation)<br><br>c.SetFocalPoint(renderer.GetActiveCamera().GetFocalPoint())<br><br><br>render_window.Render()<br>interactor.Initialize()<br>interactor.Start()<br><br></span></span><span lang="en"><span>Please</span> <span>I need an answer</span></span></div></div></blockquote></div></div></div>