【vtk】資料型別轉換vtkpolydata轉vtkImagedata
阿新 • • 發佈:2019-01-29
用到的類為vtkPolyDataToImageStencil
效果:
#include <vtkVersion.h> #include <vtkSmartPointer.h> #include <vtkPolyData.h> #include <vtkImageData.h> #include <vtkSphereSource.h> #include <vtkMetaImageWriter.h> #include <vtkPolyDataToImageStencil.h> #include <vtkImageStencil.h> #include <vtkPointData.h> #include <vtkCutter.h> #include <vtkPlane.h> #include <vtkStripper.h> #include <vtkLinearExtrusionFilter.h> #include <vtkXMLPolyDataWriter.h> #include <vtkImageData.h> #include <vtkMetaImageReader.h> #include <vtkSmartPointer.h> #include <vtkInteractorStyleImage.h> #include <vtkRenderer.h> #include <vtkImageActor.h> #include <vtkImageMapper3D.h> #include <vtkRenderWindow.h> #include <vtkRenderWindowInteractor.h> #include <vtkTesting.h> void convertData2() { vtkSmartPointer<vtkSphereSource> sphereSource = vtkSmartPointer<vtkSphereSource>::New(); sphereSource->SetRadius(20); sphereSource->SetPhiResolution(30); sphereSource->SetThetaResolution(30); vtkSmartPointer<vtkPolyData> pd = sphereSource->GetOutput(); sphereSource->Update(); vtkSmartPointer<vtkImageData> whiteImage = vtkSmartPointer<vtkImageData>::New(); double bounds[6]; pd->GetBounds(bounds); double spacing[3]; // desired volume spacing spacing[0] = 0.5; spacing[1] = 0.5; spacing[2] = 0.5; whiteImage->SetSpacing(spacing); // compute dimensions int dim[3]; for (int i = 0; i < 3; i++) { dim[i] = static_cast<int>(ceil((bounds[i * 2 + 1] - bounds[i * 2]) / spacing[i])); } whiteImage->SetDimensions(dim); whiteImage->SetExtent(0, dim[0] - 1, 0, dim[1] - 1, 0, dim[2] - 1); double origin[3]; origin[0] = bounds[0] + spacing[0] / 2; origin[1] = bounds[2] + spacing[1] / 2; origin[2] = bounds[4] + spacing[2] / 2; whiteImage->SetOrigin(origin); #if VTK_MAJOR_VERSION <= 5 whiteImage->SetScalarTypeToUnsignedChar(); whiteImage->AllocateScalars(); #else whiteImage->AllocateScalars(VTK_UNSIGNED_CHAR,1); #endif // fill the image with foreground voxels: unsigned char inval = 255; unsigned char outval = 0; vtkIdType count = whiteImage->GetNumberOfPoints(); for (vtkIdType i = 0; i < count; ++i) { whiteImage->GetPointData()->GetScalars()->SetTuple1(i, inval); } // polygonal data --> image stencil: vtkSmartPointer<vtkPolyDataToImageStencil> pol2stenc = vtkSmartPointer<vtkPolyDataToImageStencil>::New(); #if VTK_MAJOR_VERSION <= 5 pol2stenc->SetInput(pd); #else pol2stenc->SetInputData(pd); #endif pol2stenc->SetOutputOrigin(origin); pol2stenc->SetOutputSpacing(spacing); pol2stenc->SetOutputWholeExtent(whiteImage->GetExtent()); pol2stenc->Update(); // cut the corresponding white image and set the background: vtkSmartPointer<vtkImageStencil> imgstenc = vtkSmartPointer<vtkImageStencil>::New(); #if VTK_MAJOR_VERSION <= 5 imgstenc->SetInput(whiteImage); imgstenc->SetStencil(pol2stenc->GetOutput()); #else imgstenc->SetInputData(whiteImage); imgstenc->SetStencilConnection(pol2stenc->GetOutputPort()); #endif imgstenc->ReverseStencilOff(); imgstenc->SetBackgroundValue(outval); imgstenc->Update(); // vtkSmartPointer<vtkMetaImageWriter> writer = // vtkSmartPointer<vtkMetaImageWriter>::New(); // writer->SetFileName("SphereVolume.mhd"); // #if VTK_MAJOR_VERSION <= 5 // writer->SetInput(imgstenc->GetOutput()); // #else // writer->SetInputData(imgstenc->GetOutput()); // #endif // writer->Write(); ////////////////////////////////////////////////////////////////////////// //流水線 vtkImageGaussianSmooth *gaosi=vtkImageGaussianSmooth::New(); //高斯平滑處理 gaosi->SetInputData(imgstenc->GetOutput()); gaosi->SetDimensionality(3); gaosi->SetRadiusFactors(2,2,1); gaosi->Update(); vtkImageCast *cast=vtkImageCast::New(); //轉換成vtk資料格式 cast->SetInputConnection(gaosi->GetOutputPort()); cast->SetOutputScalarTypeToUnsignedChar();; cast->ClampOverflowOn(); cast->Update(); vtkOpenGLVolumeTextureMapper3D *glmapper=vtkOpenGLVolumeTextureMapper3D::New(); //3D紋理對映法 glmapper->SetInputConnection(cast->GetOutputPort()); vtkPiecewiseFunction * psfunction=vtkPiecewiseFunction::New(); //透明度傳遞函式 psfunction->AddPoint( 0.0 , 0.0); psfunction->AddPoint( 40.0 , 0.0); psfunction->AddPoint( 50.0 , 0.3); psfunction->AddPoint( 110.0, 0.4); psfunction->AddPoint( 120, 0.5); psfunction->AddPoint( 130, 0.6); psfunction->AddPoint( 190, 0.8); psfunction->AddPoint( 255, 1.0); vtkColorTransferFunction * colfunction=vtkColorTransferFunction::New(); //顏色傳遞函式 colfunction->AddRGBPoint(0.0,0.5, 0.3, 0.2); colfunction->AddRGBPoint(50.0, 0.8, 0.5, 0.5); colfunction->AddRGBPoint(110.0, 0.6, 0.2, 0.3); colfunction->AddRGBPoint(190.0, 0.81, 0.27, 0.1); colfunction->AddRGBPoint(255.0, 0.5, 0.9, 0.9); vtkVolumeProperty * volpro=vtkVolumeProperty::New(); //體資料屬性 volpro->SetColor(colfunction); volpro->SetScalarOpacity(psfunction); volpro->ShadeOn(); volpro->SetInterpolationTypeToLinear(); volpro->SetAmbient(0.2); volpro->SetDiffuse(0.9); volpro->SetSpecular(0.2); volpro->SetSpecularPower(10); vtkVolume * data=vtkVolume::New(); //體資料對映器和屬性 data->SetMapper(glmapper); data->SetProperty(volpro); vtkRenderer *render=vtkRenderer::New(); vtkRenderWindow *renwin=vtkRenderWindow::New(); vtkRenderWindowInteractor *iren=vtkRenderWindowInteractor::New(); renwin->AddRenderer(render); renwin->SetInteractor(iren); //繪製器 render->AddVolume(data); render->SetBackground(0,0,0); render->ResetCameraClippingRange(); //繪製視窗 renwin->AddRenderer(render); renwin->Render(); // vtkMetaImageWriter* writer = vtkMetaImageWriter::New(); // writer->SetInputData(resultImage); //writer->SetFileName("E:\\PolyDataToImageStencil\\bin2\\Debug\\polyDataToImage_liver.mha"); // writer->SetFileName("D:\\liver.mha"); // writer->Write(); }