[Insight-users] SegError writing Spatial Object

Manuel Ricardo Galindo Moreno mrgalindo at puj.edu.co
Thu Jun 1 19:09:07 EDT 2006


Hello

I am new using itk, i am doing a program that extracts a mesh from a image file and stores it into a file. My code is based on the examples MeshSpatialObject.cxx and SurfaceExtraction.cxx from the toolkit.

The problem is that i get segmentation error an i don't really know why, y suspect that the problem is handling the spatial object but i am not sure.

I hope you could help me. My code is:


//***Extracts a mesh from a binary image and saves it into an archive***

#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif

#ifdef __BORLANDC__
#define ITK_LEAN_AND_MEAN
#endif
#include "itkImageFileReader.h"
#include "itkBinaryMask3DMeshSource.h"
#include "itkImage.h"
#include "itkMesh.h"
#include <itkMeshSpatialObject.h>
#include <itkSpatialObjectWriter.h>
#include <itkDefaultDynamicMeshTraits.h>


int main(int argc, char * argv[] ) 
{

	if( argc < 3 )
	{
		std::cerr << "Usage: IsoSurfaceExtraction  inputImageFile   objectValue " << std::endl;
		return EXIT_FAILURE;
	}

//Declares the parameters for the image reader
	const unsigned int Dimension = 3;
	typedef unsigned char  PixelType;

	typedef itk::Image< PixelType, Dimension >   ImageType;

//Reads the Image
	typedef itk::ImageFileReader< ImageType >    ReaderType;
	ReaderType::Pointer reader = ReaderType::New();
	reader->SetFileName( argv[1] );

	try
	{
		reader->Update();
	}
	catch( itk::ExceptionObject & exp )
	{
		std::cerr << "Exception thrown while reading the input file " << std::endl;
		std::cerr << exp << std::endl;
		return EXIT_FAILURE;
	}

//Declares the Mesh
	typedef itk::Mesh<double>                         MeshType;

//Declares a filter to extract the mesh from the image
	typedef itk::BinaryMask3DMeshSource< ImageType, MeshType >   MeshSourceType;

	MeshSourceType::Pointer meshSource = MeshSourceType::New();

	const PixelType objectValue = static_cast<PixelType>( atof( argv[2] ) );

	meshSource->SetObjectValue( objectValue );
	
	meshSource->SetInput( reader->GetOutput() );

	try
	{
		meshSource->Update();
	}
	catch( itk::ExceptionObject & exp )
	{
		std::cerr << "Exception thrown during Update() " << std::endl;
		std::cerr << exp << std::endl;
		return EXIT_FAILURE;
	}

	
	std::cout << "Nodes = " << meshSource->GetNumberOfNodes() << std::endl;
	std::cout << "Cells = " << meshSource->GetNumberOfCells() << std::endl;

		
//Declares an Spatial object that contains the mesh
	typedef itk::MeshSpatialObject<MeshType>     MeshSpatialObjectType;
	MeshSpatialObjectType::Pointer myMeshSpatialObject = MeshSpatialObjectType::New();
//Stores the mesh
	myMeshSpatialObject->SetMesh(meshSource->GetOutput());
	myMeshSpatialObject->GetMesh();
	
	typedef itk::DefaultDynamicMeshTraits< double , 3, 3 > MeshTrait;

//Declares and Spatial Object writer to a file
	typedef itk::SpatialObjectWriter<3,double,MeshTrait> WriterType;
	WriterType::Pointer writer = WriterType::New();
	
	writer->SetInput(myMeshSpatialObject);
	writer->SetFileName("myMesh.meta");
	
	
	try
	{
		writer->Update();
	}
	catch( itk::ExceptionObject & exp )
	{
		std::cerr << "Exception thrown during writer->Update() " << std::endl;
		std::cerr << exp << std::endl;
		return EXIT_FAILURE;
	}


	return EXIT_SUCCESS;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://public.kitware.com/pipermail/insight-users/attachments/20060601/75c34c74/attachment.htm


More information about the Insight-users mailing list