[vtkusers] save slice from vtk image volume

David Gobbi david.gobbi at gmail.com
Wed Dec 4 08:44:56 EST 2013


Hi Jessie,

The vtkImageResliceMapper just draws the slice to the screen, it does
not produce it as a output on its output port.  So the following line
of code will not work:

writer->SetInputConnection(im->GetOutputPort());

The only way to get the image from vtkImageResliceMapper is to capture
the contents of the render window, with vtkWindowToImageFilter.

  David


On Wed, Dec 4, 2013 at 1:43 AM, Jesse Ross-Jones <jesse.rj at gmail.com> wrote:
> Dear VTKrs
>
> I am trying to extract a single slice from an image volume and write the
> image to a file.
>
> I have found and example using vtkImageResliceMapper and I have modified it
> below. However I am unable to properly save the image.
>
> Suggestions for how to save the single image and the volume with only the
> single image slice much appreciated!
>
> Many thanks,
> Jesse
>
>
> int main(int argc, char *argv[])
> {
>         if (argc < 3) {
>                 cout << "Usage: " << argv[0] << " Input_Image " <<
> "Output_Image"<< endl;
>                 return EXIT_FAILURE;
>         }
>
>         vtkSmartPointer<vtkRenderer> aRenderer =
> vtkSmartPointer<vtkRenderer>::New();
>         vtkSmartPointer<vtkRenderWindow> renWin =
> vtkSmartPointer<vtkRenderWindow>::New();
>         renWin->AddRenderer(aRenderer);
>         vtkSmartPointer<vtkRenderWindowInteractor> iren =
> vtkSmartPointer<vtkRenderWindowInteractor>::New();
>         iren->SetRenderWindow(renWin);
>
>         vtkSmartPointer<vtkXMLImageDataReader> reader =
> vtkSmartPointer<vtkXMLImageDataReader>::New();
>     reader->SetFileName(argv[1]);
>     reader->Update();
>
>         aRenderer->SetBackground(.2, .3, .4);
>         renWin->SetSize(640, 480);
>
>
>         // outline provides context around the data.
>         vtkSmartPointer<vtkOutlineFilter> outlineData =
> vtkSmartPointer<vtkOutlineFilter>::New();
>         outlineData->SetInputConnection(reader->GetOutputPort());
>         outlineData->Update();
>
>         vtkSmartPointer<vtkPolyDataMapper> mapOutline =
> vtkSmartPointer<vtkPolyDataMapper>::New();
>         mapOutline->SetInputConnection(outlineData->GetOutputPort());
>
>         vtkSmartPointer<vtkActor> outline =
> vtkSmartPointer<vtkActor>::New();
>         outline->SetMapper(mapOutline);
>         outline->GetProperty()->SetColor(0,0,0);
>
>         // Setting of vtkImageResliceMapper
>         vtkSmartPointer<vtkImageResliceMapper> im =
> vtkSmartPointer<vtkImageResliceMapper>::New();
>         im->SetInputConnection(reader->GetOutputPort());
>         //im->SliceFacesCameraOn();
>         //im->SliceAtFocalPointOn();
>         im->BorderOff();
>
>         vtkSmartPointer<vtkPlane> slicePlane =
> vtkSmartPointer<vtkPlane>::New();
>
>         double testpoint[3];
>         testpoint[0] = 49.72023;
>         testpoint[1] = 94.96884;
>         testpoint[2] = 24.4359779;
>
>         double normal[3];
>         normal[0] = -0.118968;
>         normal[1] = -0.9534519;
>         normal[2] = 0.27708499;
>
>         double testpoint2[3];
>         testpoint2[0] = 47.7378;
>         testpoint2[1] = 88.439;
>         testpoint2[2] = 26.1231;
>
>         double normal2[3];
>         normal2[0] = -0.4399;
>         normal2[1] = -0.8165;
>         normal2[2] = 0.3738968;
>
>         slicePlane->SetNormal(normal2);
>         slicePlane->SetOrigin(testpoint2);
>
>         im->SetSlicePlane(slicePlane);
>
>
>
>         // Setting of vtkImageProperty
>         vtkSmartPointer<vtkImageProperty> ip =
> vtkSmartPointer<vtkImageProperty>::New();
>         ip->SetColorWindow(2000);
>         ip->SetColorLevel(1000);
>         ip->SetAmbient(0.0);
>         ip->SetDiffuse(1.0);
>         ip->SetOpacity(1.0);
>         ip->SetInterpolationTypeToLinear();
>
>         // Try of vtkImageSlice
>         vtkSmartPointer<vtkImageSlice> imageSlice =
> vtkSmartPointer<vtkImageSlice>::New();
>         vtkSmartPointer<vtkInteractorStyleImage > styleImage =
> vtkSmartPointer<vtkInteractorStyleImage >::New();
>
>         imageSlice->SetMapper(im);
>         imageSlice->SetProperty(ip);
>
>
>         vtkSmartPointer<vtkXMLImageDataWriter> writer =
>         vtkSmartPointer<vtkXMLImageDataWriter>::New();
>         writer->SetFileName(argv[2]);
>         writer->SetInputConnection(im->GetOutputPort());
>         writer->Write();
>
>         styleImage->SetInteractionModeToImage3D();
>         iren->SetInteractorStyle(styleImage);
>         renWin->SetInteractor(iren);
>
>         // Actors are added to the renderer.
>         aRenderer->AddActor(outline);
>         aRenderer->AddViewProp(imageSlice);
>
>         // camera Setting
>         vtkSmartPointer<vtkCamera> aCamera =
> vtkSmartPointer<vtkCamera>::New();
>         aCamera->SetViewUp (0, 0, -1);
>         aCamera->SetPosition (0, 1, 0);
>         aCamera->SetFocalPoint (0, 0, 0);
>         aCamera->ComputeViewPlaneNormal();
>         aCamera->Azimuth(30.0);
>         aCamera->Elevation(30.0);
>         aCamera->ParallelProjectionOn();
>         aRenderer->SetActiveCamera(aCamera);
>
>
>         renWin->Render();
>         aRenderer->ResetCamera();
>         aRenderer->ResetCameraClippingRange();
>
>         // interact with data
>         iren->Initialize();
>         iren->Start();
>
>         return EXIT_SUCCESS;
> }


More information about the vtkusers mailing list