[ITK] Using itk::TriangleMeshToBinaryImageFilter on a cube

Silvain Bériault silvain.beriault at zimmercas.com
Tue Feb 9 16:43:36 EST 2016


Dear all,

I am having difficulties with the use of itk::TriangleMeshToBinaryImageFilter on a cube mesh. There are many mislabled voxel at the surface of the cube (see attached images). The source code follows (Usage MeshToImageTest cube1.vtk cube1.nii 0.175).

Thank you in advance for your help,

Silvain Bériault

#include <itkImage.h>
#include <itkMesh.h>
#include <itkMeshFileReader.h>
#include <itkTriangleMeshToBinaryImageFilter.h>
#include <itkImageFileWriter.h>

#include <string>

typedef itk::Image<short, 3> VolumeType;
typedef itk::Mesh<double> MeshType;

int main(int argc, char* argv[])
{
   if (argc != 4)
   {
      std::cerr << "Usage: MeshToImageTest inputMeshPath outputVolumePath spacing" << std::endl;
   }

   std::string inputFile = argv[1];
   std::string outputFile = argv[2];
   VolumeType::SpacingType spacing;
   spacing.Fill(atof(argv[3]));

   // read input mesh
   typedef itk::MeshFileReader<MeshType> MeshReaderType;
   MeshReaderType::Pointer reader = MeshReaderType::New();
   reader->SetFileName(inputFile);
   reader->Update();
   MeshType::Pointer mesh = reader->GetOutput();

   // Compute OutputVolume's origin and size based on the mesh's bounding box + padding
   auto& bounds = mesh->GetBoundingBox()->GetBounds();
   const int PADDING = 10;
   VolumeType::PointType origin;
   origin[0] = bounds[0] - PADDING * spacing[0];
   origin[1] = bounds[2] - PADDING * spacing[1];
   origin[2] = bounds[4] - PADDING * spacing[2];
   VolumeType::SizeType size;
   size[0] = (bounds[1] - bounds[0]) / spacing[0] + 2*PADDING;
   size[1] = (bounds[3] - bounds[2]) / spacing[1] + 2*PADDING;
   size[2] = (bounds[5] - bounds[4]) / spacing[2] + 2*PADDING;

   // perform conversion between mesh to volume
   typedef itk::TriangleMeshToBinaryImageFilter<MeshType, VolumeType> MeshToImageFilter;
   MeshToImageFilter::Pointer meshToVol = MeshToImageFilter::New();
   meshToVol->SetInput(mesh);
   meshToVol->SetOrigin(origin);
   meshToVol->SetSpacing(spacing);
   meshToVol->SetSize(size);
   meshToVol->SetInsideValue(1);
   meshToVol->SetOutsideValue(0);
   meshToVol->Update();

   // write output volume
   typedef itk::ImageFileWriter<VolumeType> ImageWriterType;
   ImageWriterType::Pointer writer = ImageWriterType::New();
   writer->SetInput(meshToVol->GetOutput());
   writer->SetFileName(outputFile);
   writer->Update();
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/community/attachments/20160209/4f2ef971/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: cube1.vtk
Type: application/octet-stream
Size: 494 bytes
Desc: cube1.vtk
URL: <http://public.kitware.com/pipermail/community/attachments/20160209/4f2ef971/attachment-0001.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: screenshot.png
Type: image/png
Size: 75676 bytes
Desc: screenshot.png
URL: <http://public.kitware.com/pipermail/community/attachments/20160209/4f2ef971/attachment-0001.png>


More information about the Community mailing list