[vtkusers] Exporting vtkVolume to STL/PLY

Bill Lorensen bill.lorensen at gmail.com
Fri Jul 31 11:45:38 EDT 2015


stl and ply store triangles and, for ply, polygons. Your example is
rendering a volume directly. You would need to extract polygonal surfaces
from your volume. This means you will need to perform some sort of
segmentation to identify the structures to model with polygons. Marching
Cubes is one technique that extracts surfaces based on a threshold. But
most medical segmentation requires more sophisticated techniques.

What is the modality of the dicom files/ CT, MRI, PET, ...?


On Fri, Jul 31, 2015 at 11:16 AM, Ievgen Petrashchuk <vaper at ukr.net> wrote:

> Hello, I've been playing with VTK examples and got the tool that renders
> 3D mesh, based on DICOM files.
> How can I export the mesh to .ply or .stl file?
>
> My code:
> #include <vtkVersion.h>
> #include <vtkSmartPointer.h>
> #include <vtkSphere.h>
> #include <vtkSampleFunction.h>
> #include <vtkSmartVolumeMapper.h>
> #include <vtkColorTransferFunction.h>
> #include <vtkPiecewiseFunction.h>
> #include <vtkRenderer.h>
> #include <vtkRenderWindow.h>
> #include <vtkRenderWindowInteractor.h>
> #include <vtkVolumeProperty.h>
> #include <vtkCamera.h>
> #include <vtkImageShiftScale.h>
> #include <vtkImageData.h>
> #include <vtkPointData.h>
> #include <vtkDataArray.h>
> #include <vtkXMLImageDataReader.h>
> #include <vtkPLYWriter.h>
> #include <vtkDICOMImageReader.h>
> #include <vtkVolumeOutlineSource.h>
>
> int main(int argc, char* argv[])
> {
>     // Verify input arguments
>     if ( argc != 3 )
>     {
>         std::cout << "Usage: " << argv[0]
>         << " FolderName" << "Export.ply" << std::endl;
>         return EXIT_FAILURE;
>     }
>
>     std::string folder = argv[1];
>     std::string output = argv[2];
>
>     // Read all the DICOM files in the specified directory.
>     vtkSmartPointer<vtkDICOMImageReader> reader =
>             vtkSmartPointer<vtkDICOMImageReader>::New();
>     reader->SetDirectoryName(folder.c_str());
>     reader->Update();
>
>     //Create vtkImageData
>     vtkSmartPointer<vtkImageData> imageData =
>             vtkSmartPointer<vtkImageData>::New();
>
>     imageData->ShallowCopy(reader->GetOutput());
>
>     //renderer
>     vtkSmartPointer<vtkRenderWindow> renWin =
>             vtkSmartPointer<vtkRenderWindow>::New();
>     vtkSmartPointer<vtkRenderer> ren1 =
>             vtkSmartPointer<vtkRenderer>::New();
>     ren1->SetBackground(0,0,0);
>
>     renWin->AddRenderer(ren1);
>
>     renWin->SetSiz e(301,300); // intentional odd and NPOT  width/height
>
>     vtkSmartPointer<vtkRenderWindowInteractor> iren =
>             vtkSmartPointer<vtkRenderWindowInteractor>::New();
>     iren->SetRenderWindow(renWin);
>
>     renWin->Render(); // make sure we have an OpenGL context.
>
>     vtkSmartPointer<vtkSmartVolumeMapper> volumeMapper =
>             vtkSmartPointer<vtkSmartVolumeMapper>::New();
>     volumeMapper->SetBlendModeToComposite(); // composite first
> #if VTK_MAJOR_VERSION <= 5
>     volumeMapper->SetInputConnection(imageData->GetProducerPort());
> #else
>   volumeMapper->SetInputData(imageData);
> #endif
>     vtkSmartPointer<vtkVolumeProper ty> volumeProperty =
>             vtkSmartPointer<vtkVolumeProperty>::New();
>     volumeProperty->ShadeOff();
>     volumeProperty->SetInterpolationType(VTK_LINEAR_INTERPOLATION);
>
>     vtkSmartPointer<vtkPiecewiseFunction> compositeOpacity =
>             vtkSmartPointer<vtkPiecewiseFunction>::New();
>     compositeOpacity->AddPoint(-1000.0,0.0);
>     compositeOpacity->AddPoint(0.0,0.0);
>     compositeOpacity->AddPoint(1000.0,0.89);
>     volumeProperty->SetScalarOpacity(compositeOpacity); // composite first.
>
>     vtkSmartPointer<vtkColorTransferFunction> color =
>             vtkSmartPointer<vtkColorTransferFunction>::New();
>
> < /div>
>     color->AddRGBPoint(0,    0.0, 0.0, 0.0);
>     color->AddRGBPoint(1000,  1.0, 1, 1);
>     color->AddRGBPoint(-1000,    0.0, 0.0, 0.0);
>     volumeProperty->SetColor(color);
>
>     volumeProperty->SetInterpolationTypeToLinear();
>     volumeProperty->ShadeOff();
>
>     vtkSmartPointer<vtkVolume> volume =
>             vtkSmartPointer<vtkVolume>::New();
>     volume->SetMapper(volumeMapper);
>     volume->SetProperty(volumeProperty);
>
>
>     ren1->AddViewProp(volume);
>     ren1->ResetCamera();
>
>     // Render composite. In default mode. For coverage.
>     renWin->R ender();
>
>     // 3D texture mode. For coverage.
>     volumeMapper->SetRequestedRenderModeToDefault();
>     renWin->Render();
>
>     // Software mode, for coverage. It also makes sure we will get the same
>     // regression image on all platforms.
>     volumeMapper->SetRequestedRenderModeToRayCast();
>     renWin->Render();
>
>     iren->Start();
>
>     return EXIT_SUCCESS;
> }
>
> _______________________________________________
> 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
>
> Search the list archives at: http://markmail.org/search/?q=vtkusers
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/vtkusers
>
>


-- 
Unpaid intern in BillsBasement at noware dot com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20150731/b554eca4/attachment.html>


More information about the vtkusers mailing list