码迷,mamicode.com
首页 > 其他好文 > 详细

vtkPolyData 如何转换 vtkImageData ?

时间:2020-04-13 22:28:07      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:OLE   pre   tst   nal   fill   idt   text   Dimension   ast   

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 ?

标签:OLE   pre   tst   nal   fill   idt   text   Dimension   ast   

原文地址:https://www.cnblogs.com/cheungxiongwei/p/12694284.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!