<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Generator" CONTENT="MS Exchange Server version 6.5.6944.0">
<TITLE>SegError writing Spatial Object</TITLE>
</HEAD>
<BODY>
<!-- Converted from text/plain format -->
<P><FONT SIZE=2>Hello<BR>
<BR>
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.<BR>
<BR>
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.<BR>
<BR>
I hope you could help me. My code is:<BR>
<BR>
<BR>
//***Extracts a mesh from a binary image and saves it into an archive***<BR>
<BR>
#if defined(_MSC_VER)<BR>
#pragma warning ( disable : 4786 )<BR>
#endif<BR>
<BR>
#ifdef __BORLANDC__<BR>
#define ITK_LEAN_AND_MEAN<BR>
#endif<BR>
#include "itkImageFileReader.h"<BR>
#include "itkBinaryMask3DMeshSource.h"<BR>
#include "itkImage.h"<BR>
#include "itkMesh.h"<BR>
#include <itkMeshSpatialObject.h><BR>
#include <itkSpatialObjectWriter.h><BR>
#include <itkDefaultDynamicMeshTraits.h><BR>
<BR>
<BR>
int main(int argc, char * argv[] )<BR>
{<BR>
<BR>
if( argc < 3 )<BR>
{<BR>
std::cerr << "Usage: IsoSurfaceExtraction inputImageFile objectValue " << std::endl;<BR>
return EXIT_FAILURE;<BR>
}<BR>
<BR>
//Declares the parameters for the image reader<BR>
const unsigned int Dimension = 3;<BR>
typedef unsigned char PixelType;<BR>
<BR>
typedef itk::Image< PixelType, Dimension > ImageType;<BR>
<BR>
//Reads the Image<BR>
typedef itk::ImageFileReader< ImageType > ReaderType;<BR>
ReaderType::Pointer reader = ReaderType::New();<BR>
reader->SetFileName( argv[1] );<BR>
<BR>
try<BR>
{<BR>
reader->Update();<BR>
}<BR>
catch( itk::ExceptionObject & exp )<BR>
{<BR>
std::cerr << "Exception thrown while reading the input file " << std::endl;<BR>
std::cerr << exp << std::endl;<BR>
return EXIT_FAILURE;<BR>
}<BR>
<BR>
//Declares the Mesh<BR>
typedef itk::Mesh<double> MeshType;<BR>
<BR>
//Declares a filter to extract the mesh from the image<BR>
typedef itk::BinaryMask3DMeshSource< ImageType, MeshType > MeshSourceType;<BR>
<BR>
MeshSourceType::Pointer meshSource = MeshSourceType::New();<BR>
<BR>
const PixelType objectValue = static_cast<PixelType>( atof( argv[2] ) );<BR>
<BR>
meshSource->SetObjectValue( objectValue );<BR>
<BR>
meshSource->SetInput( reader->GetOutput() );<BR>
<BR>
try<BR>
{<BR>
meshSource->Update();<BR>
}<BR>
catch( itk::ExceptionObject & exp )<BR>
{<BR>
std::cerr << "Exception thrown during Update() " << std::endl;<BR>
std::cerr << exp << std::endl;<BR>
return EXIT_FAILURE;<BR>
}<BR>
<BR>
<BR>
std::cout << "Nodes = " << meshSource->GetNumberOfNodes() << std::endl;<BR>
std::cout << "Cells = " << meshSource->GetNumberOfCells() << std::endl;<BR>
<BR>
<BR>
//Declares an Spatial object that contains the mesh<BR>
typedef itk::MeshSpatialObject<MeshType> MeshSpatialObjectType;<BR>
MeshSpatialObjectType::Pointer myMeshSpatialObject = MeshSpatialObjectType::New();<BR>
//Stores the mesh<BR>
myMeshSpatialObject->SetMesh(meshSource->GetOutput());<BR>
myMeshSpatialObject->GetMesh();<BR>
<BR>
typedef itk::DefaultDynamicMeshTraits< double , 3, 3 > MeshTrait;<BR>
<BR>
//Declares and Spatial Object writer to a file<BR>
typedef itk::SpatialObjectWriter<3,double,MeshTrait> WriterType;<BR>
WriterType::Pointer writer = WriterType::New();<BR>
<BR>
writer->SetInput(myMeshSpatialObject);<BR>
writer->SetFileName("myMesh.meta");<BR>
<BR>
<BR>
try<BR>
{<BR>
writer->Update();<BR>
}<BR>
catch( itk::ExceptionObject & exp )<BR>
{<BR>
std::cerr << "Exception thrown during writer->Update() " << std::endl;<BR>
std::cerr << exp << std::endl;<BR>
return EXIT_FAILURE;<BR>
}<BR>
<BR>
<BR>
return EXIT_SUCCESS;<BR>
}</FONT>
</P>
</BODY>
</HTML>