[vtkusers] Topology problem in converting polydata to image

donyyo donyyo at gmail.com
Mon Aug 19 10:09:45 EDT 2013


Hi,
I want to convert polydata contour to image following the example (
http://http://www.vtk.org/Wiki/VTK/Examples/PolyData/PolyDataToImageData
<http://http://www.vtk.org/Wiki/VTK/Examples/PolyData/PolyDataToImageData>  
and 
http://http://www.vtk.org/Wiki/VTK/Examples/Cxx/PolyData/PolyDataContourToImageData
<http://http://www.vtk.org/Wiki/VTK/Examples/Cxx/PolyData/PolyDataContourToImageData> 
). There are many parts in the contour (left image). However, the topology
in the image I got was wrong (right image). What can I do?

<http://vtk.1045678.n5.nabble.com/file/n5722850/image1.png> 

modelPolyData->GetCenter(cutPlaneOrigin);
cutPlaneOrigin[2] = firstImageNum+ii;
vtkPlane *cutPlane = vtkPlane::New();
cutPlane->SetOrigin(cutPlaneOrigin);
cutPlane->SetNormal(0, 0, 1);

vtkCutter *cutter = vtkCutter::New();
cutter->SetInput(modelPolyData);
cutter->SetCutFunction(cutPlane);

vtkStripper *stripper = vtkStripper::New();
stripper->SetInputConnection(cutter->GetOutputPort());
stripper->Update();

cutContourPolyData = stripper->GetOutput();

double cutContourBounds[6];
cutContourPolyData->GetBounds(cutContourBounds);

// Prepare the binary image's voxel grid.
double imageResolution[3] = {1.0, 1.0, 1.0};
imageData->SetSpacing(imageResolution);

// Compute dimensions.
vtkImageData *imageData = vtkImageData::New();
imageData->SetDimensions(imageDimension);
imageData->SetExtent(0, imageDimension[0]-1, 0, imageDimension

[1]-1, 0, imageDimension[2]-1);

imageOrigin[2] = cutContourBounds[4];
imageData->SetOrigin(imageOrigin);
imageData->SetScalarTypeToUnsignedChar();
imageData->AllocateScalars();

// Fill the image with foreground voxels.
for (vtkIdType i = 0; i < imageData->GetNumberOfPoints(); i++)
    imageData->GetPointData()->GetScalars()->SetTuple1(i, 255);

// polygonal data --> image stencil:
vtkPolyDataToImageStencil *pol2stenc = 

vtkPolyDataToImageStencil::New();
pol2stenc->SetTolerance(0);
pol2stenc->SetInput(cutContourPolyData);
pol2stenc->SetOutputOrigin(imageOrigin);
pol2stenc->SetOutputSpacing(imageResolution);
pol2stenc->SetOutputWholeExtent(imageData->GetExtent());
pol2stenc->Update();

// Cut the corresponding white image and set the background.
vtkImageStencil *imgstenc = vtkImageStencil::New();
imgstenc->SetInput(imageData);
imgstenc->SetStencil(pol2stenc->GetOutput());
imgstenc->ReverseStencilOff();
imgstenc->SetBackgroundValue(0);
imgstenc->Update();

// Output image.
imageNum = 10000+firstImageNum+ii;
cout << "imageNum = " << imageNum << endl;
logFile << "imageNum = " << imageNum << endl;
ostringstream outputImageStringStream;
outputImageStringStream << imageNum;
outputImageNumStr = outputImageStringStream.str();
cout << "outputImageNumStr = " << outputImageNumStr.c_str() << 

endl;
logFile << "outputImageNumStr = " << outputImageNumStr.c_str() 

<< endl;
outputImageFileName = modelName

+"-"+outputImageNumStr.substr(1, outputImageNumStr.size()-

1)+"."+outputImageFormat;
outputImageFilePathName = outputImageFilePath

+outputImageFileName;
if (outputImageFormat == "tif")
{
    tiffWriter->SetFileName(outputImageFilePathName.c_str());
    tiffWriter->SetInputConnection(imgstenc->GetOutputPort());
    tiffWriter->Write();
    outputImageIndexFile << outputImageFileName.c_str() << endl;
}
if (outputImageFormat == "png")
{
    pngWriter->SetFileName(outputImageFilePathName.c_str());
    pngWriter->SetInputConnection(imgstenc->GetOutputPort());
    pngWriter->Write();
    outputImageIndexFile << outputImageFileName.c_str() << endl;
}




--
View this message in context: http://vtk.1045678.n5.nabble.com/Topology-problem-in-converting-polydata-to-image-tp5722850.html
Sent from the VTK - Users mailing list archive at Nabble.com.



More information about the vtkusers mailing list