[vtkusers] Cut slices out of polydata

Alex Malyushytskyy alexmalvtk at gmail.com
Thu May 9 19:25:57 EDT 2013


Have you tried to use vtkMetaImageWriter
to write data for your code, so you can stop questioning  if problem is
with the way you write jpg?

Alex




On Thu, May 9, 2013 at 11:35 AM, Marc Huber <marchuber.hft at gmx.de> wrote:

>  Hi,
>
> I wanted to cut a polydata object in several slices
>
> When I apply this example it works all fine:
>
> http://www.vtk.org/Wiki/VTK/Examples/Cxx/PolyData/PolyDataContourToImageData
>
> But I have a polydata object and no sphere and I want to produce a *.jpg
> file.
> My result is just a black image and I'm wondering what the problem might
> be.
> Has anyone an idea?
>
> Here is my code (Original code commented out and mine in red):
>
> //ORIGINAL CODE
>     //vtkSmartPointer<vtkSphereSource> sphereSource =
> vtkSmartPointer<vtkSphereSource>::New();
>     //    sphereSource->SetPhiResolution(30);
>     //    sphereSource->SetThetaResolution(30);
>     //    sphereSource->SetCenter(40, 40, 0);
>     //    sphereSource->SetRadius(20);
>
>     //MY CODE
>     vtkSmartPointer<vtkPolyData> polydata =
> vtkSmartPointer<vtkPolyData>::New();
>         polydata->ShallowCopy(stlActor->GetMapper()->GetInputAsDataSet());
>
>     vtkSmartPointer<vtkCutter> circleCutter =
> vtkSmartPointer<vtkCutter>::New();
>
> //circleCutter->SetInputConnection(sphereSource->GetOutputPort());
> //ORIGINAL
>         circleCutter->SetInputConnection(polydata->GetProducerPort());
> //MY CODE
>     vtkSmartPointer<vtkPlane> cutPlane = vtkSmartPointer<vtkPlane>::New();
>         //cutPlane->SetOrigin(sphereSource->GetCenter()); //ORIGINAL
>         cutPlane->SetOrigin(polydata->GetCenter()); //MY CODE
>         cutPlane->SetNormal(0, 0, 1);
>     circleCutter->SetCutFunction(cutPlane);
>     vtkSmartPointer<vtkStripper> stripper =
> vtkSmartPointer<vtkStripper>::New();
>         stripper->SetInputConnection(circleCutter->GetOutputPort()); //
> valid circle
>         stripper->Update();
>     // that's our circle
>     vtkSmartPointer<vtkPolyData> circle = stripper->GetOutput();
>
>     // write circle out
>     vtkSmartPointer<vtkXMLPolyDataWriter> polyDataWriter =
> vtkSmartPointer<vtkXMLPolyDataWriter>::New();
>         polyDataWriter->SetInput(circle);
>         polyDataWriter->SetFileName("circle.vtp");
>         polyDataWriter->SetCompressorTypeToNone();
>         polyDataWriter->SetDataModeToAscii();
>         polyDataWriter->Write();
>
>     // prepare the binary image's voxel grid
>     double bounds[6];
>     circle->GetBounds(bounds);
>     double spacing[3]; // desired volume spacing
>     spacing[0] = 0.5;
>     spacing[1] = 0.5;
>     spacing[2] = 0.5;
>     vtkSmartPointer<vtkImageData> whiteImage =
> vtkSmartPointer<vtkImageData>::New();
>         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])) + 1;
>         if (dim[i] < 1){ dim[i] = 1;}
>     }
>     whiteImage->SetDimensions(dim);
>     whiteImage->SetExtent(0, dim[0] - 1, 0, dim[1] - 1, 0, dim[2] - 1);
>     double origin[3];
>     // NOTE: I am not sure whether or not we had to add some offset!
>     origin[0] = bounds[0];// + spacing[0] / 2;
>     origin[1] = bounds[2];// + spacing[1] / 2;
>     origin[2] = bounds[4];// + spacing[2] / 2;
>     whiteImage->SetOrigin(origin);
>     whiteImage->SetScalarTypeToUnsignedChar();
>     whiteImage->AllocateScalars();
>     // 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);
>     }
>
>     // sweep polygonal data (this is the important thing with contours!)
>     vtkSmartPointer<vtkLinearExtrusionFilter> extruder =
>         vtkSmartPointer<vtkLinearExtrusionFilter>::New();
>         extruder->SetInput(circle);
>         extruder->SetScaleFactor(1.);
>         extruder->SetExtrusionTypeToNormalExtrusion();
>         extruder->SetVector(0, 0, 1);
>         extruder->Update();
>
>     // polygonal data --> image stencil:
>     vtkSmartPointer<vtkPolyDataToImageStencil> pol2stenc =
> vtkSmartPointer<vtkPolyDataToImageStencil>::New();
>         pol2stenc->SetTolerance(0); // important if extruder->SetVector(0,
> 0, 1) !!!
>         pol2stenc->SetInputConnection(extruder->GetOutputPort());
>         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();
>         imgstenc->SetInput(whiteImage);
>         imgstenc->SetStencil(pol2stenc->GetOutput());
>         imgstenc->ReverseStencilOff();
>         imgstenc->SetBackgroundValue(outval);
>         imgstenc->Update();
>
>     //ORIGINAL CODE
>     //vtkSmartPointer<vtkMetaImageWriter> imageWriter =
>     //  vtkSmartPointer<vtkMetaImageWriter>::New();
>     //imageWriter->SetFileName("labelImage.mhd");
>     //imageWriter->SetInputConnection(imgstenc->GetOutputPort());
>     //imageWriter->Write();
>
>     //MY CODE
>       vtkSmartPointer<vtkJPEGWriter> cutWriter =
> vtkSmartPointer<vtkJPEGWriter>::New();
>         cutWriter->SetFileName("cut1.jpg");
>         cutWriter->SetInputConnection(imgstenc->GetOutputPort());
>         cutWriter->Write();
>
> --
> Best regards / Mit freundlichen Grüßen
> Marc Huber
>
>
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the VTK FAQ at:
> http://www.vtk.org/Wiki/VTK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20130509/410417fd/attachment.htm>


More information about the vtkusers mailing list