[vtkusers] save slice from vtk image volume

Jesse Ross-Jones jesse.rj at gmail.com
Wed Dec 4 03:43:51 EST 2013


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;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20131204/e73beb75/attachment.htm>


More information about the vtkusers mailing list