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