[Insight-users] converting mesh to binary image
Moti Freiman
freiman at cs.huji.ac.il
Thu Dec 25 18:26:22 EST 2008
Hi,
I'm trying to convert a 3d surface (got as some segmentation result) to a
binary image, for comparison between different segmentations.
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.
However, when I'm trying to convert it to a 3D binary volume using
itkTriangleMeshToBinaryImageFilter I got the following exception:
------------------------------------------------------ start of output
-------------------------------------------------------------------------
ExceptionObject caught !
itk::ExceptionObject (012CFB38)
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)"
File:
e:\libs\insighttoolkit-3.10.0\code\basicfilters\itkTriangleMeshToBinaryImageFilter.txx
Line: 224
Description: itk::ERROR: TriangleMeshToBinaryImageFilter(038FD670): No Image
Indices Found.
Press any key to continue
------------------------------------------------------- end of output
-------------------------------------------------------------------------
My code is below,
------------------------------------------------------ start of
code-------------------------------------------------------------------------
#include "itkVTKPolyDataWriter.h"
#include "itkVTKPolyDataReader.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkCastImageFilter.h"
#include "itkMesh.h"
#include "itkTriangleCell.h"
#include <fstream>
#include "itkTriangleMeshToBinaryImageFilter.h"
int main( int argc, char **argv )
{
char *points_name = NULL, *faces_name = NULL, *output_mesh = NULL,
*input_image = NULL, *output_name = NULL;
points_name = argv[1];
faces_name = argv[2];
output_mesh = argv[3];
input_image = argv[4];
output_name = argv[5];
typedef short PixelType;
typedef unsigned char OutputPixelType;
const unsigned int Dimension = 3;
typedef itk::Mesh <OutputPixelType, Dimension> itkMeshType;
itkMeshType::Pointer mesh = itkMeshType::New();
std::fstream pointsReader;
pointsReader.open (points_name, std::ios::in);
int i=0;
while (pointsReader.peek () != EOF)
{
itkMeshType::PointType p;
pointsReader >> p[0] >> p[1] >> p[2];
mesh->SetPoint (i,p);
++i;
}
pointsReader.close();
typedef itkMeshType::CellType CellType;
typedef itk::TriangleCell < CellType > FaceType;
typedef CellType::CellAutoPointer CellAutoPointer;
std::fstream facesReader;
facesReader.open (faces_name, std::ios::in);
i=0;
while (facesReader.peek () != EOF)
{
int p[3];
facesReader >> p[0] >> p[1] >> p[2];
CellAutoPointer face;
face.TakeOwnership( new FaceType );
face->SetPointId (0, p[0]);
face->SetPointId (1, p[1]);
face->SetPointId (2, p[2]);
mesh->SetCell (i,face);
++i;
}
facesReader.close();
mesh->Update();
typedef itk::VTKPolyDataWriter<itkMeshType> itkVTKPolyDataWriterType;
itkVTKPolyDataWriterType::Pointer meshWriter =
itkVTKPolyDataWriterType::New();
meshWriter->SetFileName( output_mesh );
meshWriter->SetInput (mesh);
try
{
meshWriter->Update();
}
catch ( itk::ExceptionObject &err)
{
std::cout << "ExceptionObject caught !" << std::endl;
std::cout << err << std::endl;
return -1;
}
typedef itk::Image< PixelType, Dimension > ImageType;
typedef itk::Image< OutputPixelType, Dimension > OutputImageType;
typedef itk::ImageFileReader< ImageType > ImageReaderType;
// Read the input files
ImageReaderType::Pointer reader = ImageReaderType::New();
reader->SetFileName( input_image );
try
{
reader->Update();
}
catch ( itk::ExceptionObject &err)
{
std::cout << "ExceptionObject caught !" << std::endl;
std::cout << err << std::endl;
return -1;
}
ImageType::Pointer orgImage = reader->GetOutput();
const ImageType::PointType & orgOrigin = orgImage->GetOrigin();
const ImageType::SizeType & orgSize =
orgImage->GetLargestPossibleRegion().GetSize();
const ImageType::SpacingType & orgSpacing = orgImage->GetSpacing();
const ImageType::IndexType &orgIndex =
orgImage->GetLargestPossibleRegion().GetIndex();
typedef itk::TriangleMeshToBinaryImageFilter <itkMeshType,
OutputImageType> itkTriangleMeshToBinaryImageFilterType;
itkTriangleMeshToBinaryImageFilterType::Pointer meshFilter =
itkTriangleMeshToBinaryImageFilterType::New();
meshFilter->SetTolerance (1.0);
meshFilter->SetSpacing (orgSpacing);
meshFilter->SetOrigin(orgOrigin);
meshFilter->SetSize (orgSize);
meshFilter->SetIndex (orgIndex);
meshFilter->SetInput(mesh);
try
{
meshFilter->Update();
}
catch ( itk::ExceptionObject &err )
{
std::cout << "ExceptionObject caught !" << std::endl;
std::cout << err << std::endl;
return -1;
}
// Write the output image containing the binary volume.
typedef itk::ImageFileWriter< OutputImageType > WriterType;
WriterType::Pointer writer = WriterType::New();
writer->SetInput( meshFilter->GetOutput() );
writer->SetFileName( output_name );
try
{
writer->Update();
}
catch ( itk::ExceptionObject &err )
{
std::cout << "ExceptionObject caught !" << std::endl;
std::cout << err << std::endl;
return -1;
}
return 0;
}
------------------------------------------------------ end of
code-------------------------------------------------------------------------
Thanks for any help,
Moti
--
__
Moti Freiman, Ph.D Student.
Medical Image Processing and Computer-Assisted Surgery Laboratory.
School of Computer Science and Engineering.
The Hebrew University of Jerusalem Givat Ram, Jerusalem 91904, Israel
Phone: +(972)-2-658-5371 (laboratory)
WWW site: http://www.cs.huji.ac.il/~freiman
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20081226/dab134c7/attachment-0001.htm>
More information about the Insight-users
mailing list