[Insight-developers] Getting the Gradient as a VectorImage

Bradley Lowekamp blowekamp at mail.nih.gov
Tue May 1 09:08:49 EDT 2012


Hello,

As I mentioned at the TCON, I need to get the GradientImageFilter into SimpleITK. The problem is that the output of this filter is strongly typed to be an Image of CovariantVectors and SimpleITK deal will all multi-component images as the more flexible VectorImages. Not the buffer image buffer is really exactly the same for the two type, just a bunch of floats, but the levels abstraction and C++ I don't believe really guarantees this as one is a pointer to floats for the VectorImages, while the other is an array of CovariantVector object. The reason that we can't just make the filter work with VectorImages is because how it's templated:

template< class TInputImage, class TOperatorValueType = float, class TOutputValueType = float >
class ITK_EXPORT GradientImageFilter:
  public ImageToImageFilter< TInputImage,
                             Image< CovariantVector< TOutputValueType,
                                                     ::itk::GetImageDimension< TInputImage >::ImageDimension >,
                                    ::itk::GetImageDimension< TInputImage >::ImageDimension > >

There is just no way to specify the output image type, I suppose I could add a 4th template parameter.

I am looking for other suggestions on how to adapt this class's output for SimpleITK, or other wise safely convert the output. 

I was able to come up with the following efficient, and horrendous hack, but I don't think I can bare to actually commit it:


template< typename TPixelType, unsigned int ImageDimension >
typename itk::VectorImage< TPixelType, ImageDimension >::Pointer
GetVectorImage( itk::Image< itk::CovariantVector< TPixelType, ImageDimension >, ImageDimension> *img )
{
  typedef itk::Image< itk::CovariantVector< TPixelType, ImageDimension >, ImageDimension> ImageType;
  typedef itk::VectorImage< TPixelType, ImageDimension > VectorImageType;


  SizeValueType numberOfElements = img->GetBufferedRegion().GetNumberOfPixels();
  img->GetPixelContainer()->SetImportPointer( img->GetPixelContainer()->GetBufferPointer(), numberOfElements, false );


  // It appears that the Covararient vector neither is POD nor does it
  // have a trivial copy constructor. So this really is a hack and
  // guaranteed to work.
  //sitkStaticAssert( std::tr1::has_trivial_copy<typename ImageType::PixelType>::value, "Assumption on CovarientVector violated!" ;)
  //sitkStaticAssert( std::tr1::is_pod<typename ImageType::PixelType>::value, "Assumption on CovarientVector violated!" ;)

  typename VectorImageType::InternalPixelType* buffer
    = reinterpret_cast<typename VectorImageType::InternalPixelType*>( img->GetPixelContainer()->GetBufferPointer() );

  // Unlike an image of CovarientVector a VectorImage's container is a
  // container of TPixelType, whos size is the image's number of
  // pixels * number of pixels per component
  numberOfElements *= ImageDimension;

  typename VectorImageType::Pointer out = VectorImageType::New();

  // Set the image's pixel container to import the pointer provided.
  out->GetPixelContainer()->SetImportPointer(buffer, numberOfElements, true );
  out->CopyInformation( img );
  out->SetRegions( img->GetBufferedRegion() );

  return out;
}

Thanks for your suggestions

Brad

========================================================
Bradley Lowekamp  
Medical Science and Computing for
Office of High Performance Computing and Communications
National Library of Medicine 
blowekamp at mail.nih.gov



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-developers/attachments/20120501/1e4382a6/attachment.htm>


More information about the Insight-developers mailing list