[Insight-users] How to properly use itkTriangleMeshToImageFilter?

Amardeep Singh amar.singh at gmx.de
Thu Jul 2 15:34:36 EDT 2009


Dear ITK Users

I would like to use the "itkTriangleMeshToImageFilter". In a first 
experiment, I just want to create
a sphere using the "itkRegularSphereMeshSource", binarize it and save 
the output. I take the size, origin and spacing
of the resulting image from an image that I read from the harddisk (just 
for convenience).
Unfortunately, I get the following error:

Start Processing!
[-90, 126, -72]          <--- origin
[182, 218, 182]        <--- size of image
[1, 1, 1]                    <--- spacing of image
[0, 0, 0]                    <--- index
Exception caught !

itk::ExceptionObject (0x81a3b00)
Location: "void itk::TriangleMeshToBinaryImageFilter<TInputMesh, 
TOutputImage>::GenerateData() [with TInputMesh = itk::Mesh<float, 3u, 
itk::DefaultStaticMeshTraits<float, 3u, 3u, float, float, float> >, 
TOutputImage = itk::Image<unsigned char, 3u>]"
File: 
/workspace/InsightToolkit-3.14.0/Code/BasicFilters/itkTriangleMeshToBinaryImageFilter.txx
Line: 224
Description: itk::ERROR: TriangleMeshToBinaryImageFilter(0x8141ec8): No 
Image Indices Found.

Above, I have included output, indicating the origin, size, spacing and 
index of the largest region of the image that I read from the harddisk. 
These are the values that I pass to the "itkTriangleMeshToImageFilter", 
so they should be fine, I think.
There has been a similar posting on the mailing list 
("http://www.nabble.com/converting-mesh-to-binary-image-td21170657.html"), 
but the solution to the problem was not made explicit, unfortunately. 
Please, find my source code attached.
Does anyone know how to resolve this problem?
Thank you very much!

Best regards
Amardeep


#include "itkImageRegionIterator.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkTriangleMeshToBinaryImageFilter.h"
#include "itkDefaultStaticMeshTraits.h"
#include "itkMesh.h"
#include "itkRegularSphereMeshSource.h"

#include "iostream"


int main(int argc, char *argv[] )
{
    std::cout << "Start Processing!" << std::endl;

    typedef unsigned char BinaryPixelType;
    typedef float PixelType;
    typedef itk::DefaultStaticMeshTraits<float, 3, 3,float,float> 
TriangleMeshTraits;
    typedef itk::Mesh<float,3, TriangleMeshTraits> TriangleMeshType;
    typedef itk::RegularSphereMeshSource<TriangleMeshType>  
SphereMeshSourceType;
    typedef SphereMeshSourceType::PointType PointType;

    typedef itk::Image<BinaryPixelType, 3>      BinaryImageType;
    typedef itk::Image<PixelType, 3> ImageType;

    typedef itk::TriangleMeshToBinaryImageFilter<TriangleMeshType, 
BinaryImageType> TriangleMeshToBinaryImageFilterType;

   

    TriangleMeshToBinaryImageFilterType::Pointer triangleMeshToImage = 
TriangleMeshToBinaryImageFilterType::New();
    SphereMeshSourceType::Pointer sphere = SphereMeshSourceType::New();
    sphere->SetScale(5);
    sphere->SetResolution(5);

    typedef itk::ImageFileReader<ImageType> ReaderType;
    typedef itk::ImageFileWriter<BinaryImageType> WriterType;

    ReaderType::Pointer reader = ReaderType::New();
    reader->SetFileName("/test_image.nii.gz");
    try
    {
      reader->Update();
    }
    catch( itk::ExceptionObject & excep )
    {
      std::cerr << "Exception caught !" << std::endl;
      std::cerr << excep << std::endl;
    }

    PointType center;
    center[0] = 10;
    center[1] = 10;
    center[2] = 10;
    sphere->SetCenter(center);
    triangleMeshToImage->SetInput(sphere->GetOutput());

    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();

    std::cout << orgOrigin << std::endl;
    std::cout << orgSize << std::endl;
    std::cout << orgSpacing << std::endl;
    std::cout << orgIndex << std::endl;

    triangleMeshToImage->SetTolerance (1.0);
    triangleMeshToImage->SetSpacing (orgSpacing);
    triangleMeshToImage->SetOrigin(orgOrigin);
    triangleMeshToImage->SetSize (orgSize);
    triangleMeshToImage->SetIndex (orgIndex);

    WriterType::Pointer writer = WriterType::New();

    writer->SetFileName("/test_output.nii.gz");
    writer->SetInput(triangleMeshToImage->GetOutput());

    try
    {
        sphere->Update();
        triangleMeshToImage->Update();
        writer->Update();
    }
    catch( itk::ExceptionObject & excep )
    {
      std::cerr << "Exception caught !" << std::endl;
      std::cerr << excep << std::endl;
    }

    return 0;

}







More information about the Insight-users mailing list