<div dir="ltr">Hi,<br>I'm trying to convert a 3d surface (got as some segmentation result) to a binary image, for comparison between different segmentations.<br>I build a mesh from points and triangles which I got and save it as a vtk polygonal data. It performs well and I can visualize it properly using paraview.<br>
However, when I'm trying to convert it to a 3D binary volume using itkTriangleMeshToBinaryImageFilter I got the following exception:<br> <br>------------------------------------------------------ start of output
-------------------------------------------------------------------------<br>ExceptionObject caught !<br><br>itk::ExceptionObject (012CFB38)<br>Location: "void __thiscall itk::TriangleMeshToBinaryImageFilter<class itk::Mesh<unsigned char,3,class itk::DefaultStaticMeshTraits<unsigned char,3,3,float,float,unsigned char> >,class itk::Image<unsigned char,3> >::GenerateData(void)"<br>
File: e:\libs\insighttoolkit-3.10.0\code\basicfilters\itkTriangleMeshToBinaryImageFilter.txx<br>Line: 224<br>Description: itk::ERROR: TriangleMeshToBinaryImageFilter(038FD670): No Image Indices Found.<br><br><br>Press any key to continue<br>
<br><br><br>------------------------------------------------------- end of output -------------------------------------------------------------------------<br><br>My code is below,<br><br>------------------------------------------------------ start of code-------------------------------------------------------------------------<br>
#include "itkVTKPolyDataWriter.h"<br>#include "itkVTKPolyDataReader.h"<br>#include "itkImageFileReader.h"<br>#include "itkImageFileWriter.h"<br>#include "itkCastImageFilter.h"<br>
<br><br>#include "itkMesh.h"<br>#include "itkTriangleCell.h"<br>#include <fstream><br><br><br>#include "itkTriangleMeshToBinaryImageFilter.h"<br><br><br><br>int main( int argc, char **argv )<br>
{ <br> char *points_name = NULL, *faces_name = NULL, *output_mesh = NULL, *input_image = NULL, *output_name = NULL;<br> <br><br> <br><br> points_name = argv[1];<br> faces_name = argv[2];<br> output_mesh = argv[3];<br>
input_image = argv[4];<br> output_name = argv[5];<br> <br><br> typedef short PixelType;<br> typedef unsigned char OutputPixelType;<br> const unsigned int Dimension = 3;<br><br> <br> typedef itk::Mesh <OutputPixelType, Dimension> itkMeshType;<br>
<br> itkMeshType::Pointer mesh = itkMeshType::New();<br> <br> <br> <br> std::fstream pointsReader;<br> pointsReader.open (points_name, std::ios::in);<br> int i=0;<br> while (pointsReader.peek () != EOF)<br>
{<br> itkMeshType::PointType p;<br> pointsReader >> p[0] >> p[1] >> p[2];<br> mesh->SetPoint (i,p);<br> ++i;<br> }<br> pointsReader.close();<br> <br> typedef itkMeshType::CellType CellType;<br>
typedef itk::TriangleCell < CellType > FaceType;<br><br> typedef CellType::CellAutoPointer CellAutoPointer;<br><br><br><br> std::fstream facesReader;<br> facesReader.open (faces_name, std::ios::in);<br>
<br> i=0;<br> while (facesReader.peek () != EOF)<br> {<br> int p[3];<br> facesReader >> p[0] >> p[1] >> p[2];<br> <br> CellAutoPointer face;<br> face.TakeOwnership( new FaceType );<br>
<br> face->SetPointId (0, p[0]);<br> face->SetPointId (1, p[1]);<br> face->SetPointId (2, p[2]);<br><br> mesh->SetCell (i,face);<br> ++i;<br> }<br> facesReader.close();<br>
mesh->Update();<br><br> <br><br> <br><br> <br> typedef itk::VTKPolyDataWriter<itkMeshType> itkVTKPolyDataWriterType;<br><br> itkVTKPolyDataWriterType::Pointer meshWriter = itkVTKPolyDataWriterType::New();<br>
meshWriter->SetFileName( output_mesh );<br> meshWriter->SetInput (mesh);<br> try<br> {<br> meshWriter->Update();<br> }<br> catch ( itk::ExceptionObject &err)<br> {<br> std::cout << "ExceptionObject caught !" << std::endl; <br>
std::cout << err << std::endl; <br> return -1;<br> }<br><br> <br><br><br> typedef itk::Image< PixelType, Dimension > ImageType;<br> typedef itk::Image< OutputPixelType, Dimension > OutputImageType;<br>
<br> typedef itk::ImageFileReader< ImageType > ImageReaderType;<br><br><br><br><br><br> // Read the input files<br> ImageReaderType::Pointer reader = ImageReaderType::New();<br> reader->SetFileName( input_image );<br>
try<br> {<br> reader->Update();<br> }<br> catch ( itk::ExceptionObject &err)<br> {<br> std::cout << "ExceptionObject caught !" << std::endl; <br> std::cout << err << std::endl; <br>
return -1;<br> }<br><br> <br><br> ImageType::Pointer orgImage = reader->GetOutput();<br> <br> const ImageType::PointType & orgOrigin = orgImage->GetOrigin();<br> const ImageType::SizeType & orgSize = orgImage->GetLargestPossibleRegion().GetSize();<br>
const ImageType::SpacingType & orgSpacing = orgImage->GetSpacing();<br> const ImageType::IndexType &orgIndex = orgImage->GetLargestPossibleRegion().GetIndex();<br> <br> <br> <br><br><br> typedef itk::TriangleMeshToBinaryImageFilter <itkMeshType, OutputImageType> itkTriangleMeshToBinaryImageFilterType;<br>
itkTriangleMeshToBinaryImageFilterType::Pointer meshFilter = itkTriangleMeshToBinaryImageFilterType::New();<br> meshFilter->SetTolerance (1.0);<br> meshFilter->SetSpacing (orgSpacing);<br> meshFilter->SetOrigin(orgOrigin);<br>
meshFilter->SetSize (orgSize);<br> meshFilter->SetIndex (orgIndex);<br> meshFilter->SetInput(mesh);<br> <br> <br> try<br> {<br> meshFilter->Update();<br> }<br> catch ( itk::ExceptionObject &err )<br>
{<br> std::cout << "ExceptionObject caught !" << std::endl; <br> std::cout << err << std::endl; <br> return -1;<br> }<br> <br> <br> <br> <br> // Write the output image containing the binary volume.<br>
typedef itk::ImageFileWriter< OutputImageType > WriterType;<br> WriterType::Pointer writer = WriterType::New();<br> writer->SetInput( meshFilter->GetOutput() );<br> writer->SetFileName( output_name );<br>
try<br> {<br> writer->Update();<br> }<br> catch ( itk::ExceptionObject &err )<br> {<br> std::cout << "ExceptionObject caught !" << std::endl; <br> std::cout << err << std::endl; <br>
return -1;<br> }<br><br> return 0; <br>}<br><br>------------------------------------------------------ end of code-------------------------------------------------------------------------<br><br clear="all">
Thanks for any help,<br>Moti<br>-- <br>__<br>Moti Freiman, Ph.D Student.<br>Medical Image Processing and Computer-Assisted Surgery Laboratory.<br>School of Computer Science and Engineering.<br>The Hebrew University of Jerusalem Givat Ram, Jerusalem 91904, Israel<br>
Phone: +(972)-2-658-5371 (laboratory)<br>WWW site: <a href="http://www.cs.huji.ac.il/~freiman">http://www.cs.huji.ac.il/~freiman</a><br>
</div>