1
votes

I am trying to change image window width and level.

I understood that I need to create vtkWindowLevelLookupTable. and attach it to mapper. like described here.

I don't understand where this mapper comes from and what is it's input connection. my code:

 _reader2 = vtkDICOMImageReader.New();
            _reader2.SetDirectoryName(path);
            _reader2.Update();

            // Visualize
            _ImageViewer2 = vtkImageViewer2.New();
            _ImageViewer2.SetInputConnection(_reader2.GetOutputPort());


            var image = _ImageViewer2.GetInput();

            // get range of slices (min is the first index, max is the last index)

            _ImageViewer2.GetSliceRange(ref _MinSlice2, ref _MaxSlice2);

            this.vScrollBar2.Maximum = _MaxSlice2 + 1 + vScrollBar2.LargeChange - 1;
            Debug.WriteLine("slices range from : " + _MinSlice2.ToString() + " to " + _MaxSlice2.ToString());

            vtkRenderWindow renderWindow = renderWindowControl2.RenderWindow;

            _interactorStyle2 = vtkInteractorStyleImage.New();
            _interactorStyle2.MouseWheelForwardEvt += new vtkObject.vtkObjectEventHandler(interactor_MouseWheelForwardEvt2);
            _interactorStyle2.MouseWheelBackwardEvt += new vtkObject.vtkObjectEventHandler(interactor_MouseWheelBackwardEvt2);

            renderWindow.GetInteractor().LeftButtonPressEvt += new vtkObject.vtkObjectEventHandler(renderWindowControl2_Click);



            renderWindow.GetInteractor().SetInteractorStyle(_interactorStyle2);
            renderWindow.GetRenderers().InitTraversal();
            vtkRenderer ren;
            while ((ren = renderWindow.GetRenderers().GetNextItem()) != null)
                ren.SetBackground(0.0, 0.0, 0.0);

            _ImageViewer2.SetRenderWindow(renderWindow);
            _ImageViewer2.GetRenderer().AddActor2D(sliceStatusActor);
            _ImageViewer2.GetRenderer().AddActor2D(usageTextActor);
            _ImageViewer2.SetSlice(_MinSlice2);

            // Compute a simple window/level based on scalar range
            vtkWindowLevelLookupTable wlLut = new vtkWindowLevelLookupTable();
            double range = _reader2.GetOutput().GetScalarRange()[1] - _reader2.GetOutput().GetScalarRange()[0];
            double level = (_reader2.GetOutput().GetScalarRange()[1] +
                            _reader2.GetOutput().GetScalarRange()[0]) / 2.0;
            wlLut.SetWindow(1000);
            wlLut.SetLevel(-1000);

            // Create a mapper and actor.
            vtkDataSetMapper mapper = new vtkDataSetMapper();
            mapper.SetInputConnection(_reader2.GetOutputPort());
            mapper.SetLookupTable(wlLut);
           // mapper.SetScalarRange(0, 255);

            vtkActor actor = new vtkActor();

            var actors = _ImageViewer2.GetRenderer().GetActors();
            actor.SetMapper(mapper);

            // Add the actors to the scene
            _ImageViewer2.GetRenderer().AddActor(actor);
            _ImageViewer2.Render();
1

1 Answers

4
votes

The vtkImageViewer2 has the Window/Level functionality already implemented.

You can access it like:


myImageViewer2.GetWindowLevel().SetWindow(1000);
myImageViewer2.GetWindowLevel().SetLevel(-1000);