[Insight-developers] strangeness with cylinder spatial object

Richard Beare richard.beare at gmail.com
Tue Jul 15 21:14:22 EDT 2008


Hi,
I'm experimenting with the cylinder spatial object and getting odd
results. I'm probably missing something basic, because I haven't done
much with spatial objects yet.

I'm after a cylinder oriented along the y axis of the image, centred
on the middle of the image. I set up the output so that the origin is
near the middle of the image. The cylinder radius is less than the
image width/height.

The strangeness I see is that the cylinder isn't centred, although I
think it should be, and there is a wrap around in one direction - i.e
the left side of the cylinder is clipped by the image boundary and
reappears on the right.

My code is as follows - does anything leap out as being wrong?:

    typedef typename itk::Image<PixType, dim> ImageType;

    typename ImageType::SizeType size;
    typename ImageType::SpacingType spacing;
    typename ImageType::PointType origin;

    spacing[0] = 0.1;
    spacing[1] = 0.1;
    spacing[2] = 0.1;

    size[0] = (long unsigned int)round(15.6/spacing[0]);
    size[1] = (long unsigned int)round(39.8 /spacing[1]);
    size[2] = (long unsigned int)round(15.6 /spacing[2]);

    origin[0] =-7.6;
    origin[1] = -20;
    origin[2] = -7.6;

    typename ImageType::RegionType region;
    region.SetSize(size);
    //region.SetIndex(start);
    typename ImageType::Pointer result = ImageType::New();
    //result->SetSpacing(spacing);
//    result->SetOrigin(origin);
//    result->SetRegions(region);
//    result->Allocate();
//    result->FillBuffer(0);
//    std::cout << result;
//    writeIm<ImageType>(result, CmdLineObj.OutputIm);

    // a simple cylider model of a test tube
    typedef typename itk::GroupSpatialObject<dim> SceneSpatialObjectType;
    typename SceneSpatialObjectType::Pointer scene =
SceneSpatialObjectType::New();
    typedef itk::CylinderSpatialObject CylObjectType;

    typedef typename itk::SpatialObjectToImageFilter< SceneSpatialObjectType,
    ImageType >   SpatialObjectToImageFilterType;

//    typename SpatialObjectToImageFilterType::PointType cylorigin;

    // create a testtube using spatial objects
    typename CylObjectType::Pointer testtube = CylObjectType::New();
    testtube->SetRadius(6.7);
    testtube->SetHeight(500);  // pretend it is really long
    testtube->SetId(1);
    testtube->SetDefaultInsideValue(255);


    // transform it to the middle
    typename CylObjectType::TransformType::OffsetType coffset;
    coffset.Fill(0);
//    coffset[0]=region.GetSize()[0] * spacing[0] /2;
//    coffset[2]=region.GetSize()[2] * spacing[2] /2;

//    testtube->GetObjectToParentTransform()->SetOffset( coffset );
//    testtube->ComputeObjectToWorldTransform();

    scene->AddSpatialObject(testtube);

    typename SpatialObjectToImageFilterType::Pointer renderer =
    SpatialObjectToImageFilterType::New();
    renderer->SetInput(  scene  );
    renderer->SetSpacing(spacing);


    renderer->SetOrigin( origin );

    renderer->SetSize(size);
    renderer->SetUseObjectValue(true);
    renderer->SetOutsideValue( 0 );
    renderer->Update();

    writeIm<ImageType>(renderer->GetOutput(), CmdLineObj.OutputIm);

/////////////////
//helper function
/////////////////
    template <class TImage>
void writeIm(typename TImage::Pointer Im, std::string filename)
{
  typedef typename itk::ImageFileWriter<TImage> WriterType;
  typename WriterType::Pointer writer = WriterType::New();
  writer->SetInput(Im);
  writer->SetFileName(filename.c_str());
  writer->Update();
}


More information about the Insight-developers mailing list