vtkPolyData 如何转换 vtkImageData ?
Posted cheungxiongwei
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vtkPolyData 如何转换 vtkImageData ?相关的知识,希望对你有一定的参考价值。
vtkSmartPointer<vtkImageData> polyDataToImageData(vtkSmartPointer<vtkPolyData> polydata)
{
vtkSmartPointer<vtkImageData> imageData = vtkSmartPointer<vtkImageData>::New();
double bounds[6];
polydata->GetBounds(bounds);
double spacing[3];// desired volume spacing
spacing[0] = 0.3;
spacing[1] = 0.3;
spacing[2] = 0.3;
imageData->SetSpacing(spacing);
//compute dimensions
int dim[3];
for(int i = 0;i < 3;++i){
dim[i] = static_cast<int>(ceil((bounds[2*i+1]-bounds[2*i])/spacingVal));
}
imageData->SetDimensions(dim);
imageData->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;
imageData->SetOrigin(origin);
imageData->AllocateScalars(VTK_UNSIGNED_CHAR,1);
//fill the image with foreground voxels:
unsigned char inval = 255;
unsigned char outval = 0;
vtkIdType count = imageData->GetNumberOfPoints();
for(vtkIdType i = 0;i < count;++i){
imageData->GetPointData()->GetScalars()->SetTuple1(i,inval);
}
//polygonal data --> image stencil:
vtkSmartPointer<vtkPolyDataToImageStencil> pdtoImageStencil = vtkSmartPointer<vtkPolyDataToImageStencil>::New();
pdtoImageStencil->SetInputData(polydata);
pdtoImageStencil->SetOutputOrigin(origin);
pdtoImageStencil->SetOutputSpacing(spacingVal,spacingVal,spacingVal);
pdtoImageStencil->SetOutputWholeExtent(imageData->GetExtent());
pdtoImageStencil->Update();
//cut the corresponding white image and set the background:
vtkSmartPointer<vtkImageStencil> imageStencil = vtkSmartPointer<vtkImageStencil>::New();
imageStencil->SetInputData(imageData);
imageStencil->SetStencilConnection(pdtoImageStencil->GetOutputPort());
imageStencil->ReverseStencilOff();
imageStencil->SetBackgroundValue(outval);
imageStencil->Update();
imageData->DeepCopy(imageStencil->GetOutput());
return imageData;
}
以上是关于vtkPolyData 如何转换 vtkImageData ?的主要内容,如果未能解决你的问题,请参考以下文章