[Insight-users] Problems using CastImageFilter with itk::Defo rmableMesh3DFilter::GradientImageType

Miller, James V (Research) millerjv@crd.ge.com
Mon, 30 Sep 2002 09:53:39 -0400


You'll need to define a cast operator that converts a pixel from a
GradientImagePixelType to your unsigned short pixel type.

Now, I don't this is what you really intended.  The GradientImagePixelType
is a CovariantVector with three components which are doubles.  You are trying
to cast the image to a single component unsigned short image.

If you wanted to convert the gradient to a scalar, then use one of the 
gradient magnitude filters.  If you intended to convert the covariant vector
of doubles to a covariant vector of unsigned shorts or a vector of unsigned
shorts, then your original image type should be

typedef itk::Image< itk::CovariantVector<unsigned short, 3>,3> OriginalImageType

In this case case, you will need to define a cast operator that converts
a CovariantVector<double, 3> to a CovariantVector<unsigned short, 3>.  These
conversions are not done by default, we force these conversions to be explicit.

Note that CovariantVector provides a CastFrom() method that you could wrap into
a cast operator to convert from CovariantVector of doubles to CovariantVector
of unsigned short.

The cast image filter is trivial subclass of the UnaryFunctorImageFilter.
You could use a UnaryFunctorImageFilter and provide your own Functor for
doing this conversion. Again the CastFrom() method could be used for the conversion

The code may look something like this...

namespace Functor {  
  
  template< class TInput, class TOutput>
  class CovariantCast
  {
  public:
    CovariantCast() {};
    ~CovariantCast() {};
    inline TOutput operator()( const TInput & A )
    {
	TOutput B;
	B.CastFrom(A);
      return B;
    }
  };
}

template <class TInputImage, class TOutputImage>
class ITK_EXPORT CovariantCastImageFilter :
    public
    UnaryFunctorImageFilter<TInputImage,TOutputImage, 
    Functor::CovariantCast< 
              typename TInputImage::PixelType, 
              typename TOutputImage::PixelType> >
{
public:
  /** Standard class typedefs. */
  typedef CovariantCastImageFilter  Self;
  typedef UnaryFunctorImageFilter<TInputImage,TOutputImage, 
    Functor::CovariantCast< 
              typename TInputImage::PixelType, 
              typename TOutputImage::PixelType>   
                >  Superclass;
  typedef SmartPointer<Self>   Pointer;
  typedef SmartPointer<const Self>  ConstPointer;

  /** Method for creation through the object factory. */
  itkNewMacro(Self);
  
protected:
  CovariantCastImageFilter() {}
  virtual ~CovariantCastImageFilter() {}

private:
  CovariantCastImageFilter(const Self&); //purposely not implemented
  void operator=(const Self&); //purposely not implemented

};




> Subject: [Insight-users] Problems using CastImageFilter with
> itk::DeformableMesh3DFilter::GradientImageType
> 
> 
> Hallo everybody,
> 
> I just want t convert an Image of Type
> itk::DeformableMesh3DFilter::GradientImageType to another format, for
> instance an image consisting of unsigned short values. I'm 
> trying to use the
> itk::CastImageFilter in the following way:
> 
> 	// mesh definition
> 	typedef	itk::DefaultStaticMeshTraits<double, 3, 3, 
> double,double,double>
> MeshTraits;
> 	typedef itk::Mesh<double,3,MeshTraits> InputMeshType;
> 	typedef itk::Mesh<float, 3, 
> itk::DefaultStaticMeshTraits< float, 3, 3,
> float, float > > MeshType;
> 
> 	// deform filter def
> 	typedef itk::DeformableMesh3DFilter<InputMeshType,InputMeshType>
> Deformable3DFilterType;
> 
> 	// img type def
> 	typedef Deformable3DFilterType::GradientImageType	
> 				GradientImageType;
> 	typedef itk::Image<unsigned short,3>			
> 				OriginalImageType;
> 
> 	// cast filter def
> 	typedef itk::CastImageFilter< GradientImageType, 
> OriginalImageType>
> OriginalCastFilterType;
> 
> 
> 	OriginalCastFilterType::Pointer originalCastFilter =
> OriginalCastFilterType::New();
> 
> 
> The compiler says:
> 
> :\home\boettger\Insight\Code\BasicFilters\itkCastImageFilter.h
> (46) : error
> C2440: 'static_cast' : cannot convert from 'const
> itk::Image<TPixel,VImageDimension>::PixelType' to
> 'itk::Image<TPixel,VImageDimension>::PixelType'
>         with
>         [
> 
> TPixel=itk::DeformableMesh3DFilter<InputMeshType,InputMeshType
> >::GradientTyp
> e,
>             VImageDimension=3
>         ]
>         and
>         [
>             TPixel=unsigned short,
>             VImageDimension=3
>         ]
>         No user-defined-conversion operator available that 
> can perform this
> conversion, or the operator cannot be called
>         
> c:\home\boettger\Insight\Code\BasicFilters\itkCastImageFilter.h(45)
> : while compiling class-template member function
> 'itk::Image<TPixel,VImageDimension>::PixelType
> itk::Functor::Cast<TInput,TOutput>::operator ()(const TInput &)'
>         with
>         [
>             TPixel=unsigned short,
>             VImageDimension=3,
> 
> TInput=itk::Image<itk::DeformableMesh3DFilter<InputMeshType,In
> putMeshType>::
> GradientType,3>::PixelType,
>             TOutput=itk::Image<unsigned short,3>::PixelType
>         ]
> 
> c:\home\boettger\Insight\Code\BasicFilters\itkUnaryFunctorImag
> eFilter.h(102)
> : see reference to class template instantiation
> 'itk::Functor::Cast<TInput,TOutput>' being compiled
>         with
>         [
> 
> TInput=itk::Image<itk::DeformableMesh3DFilter<InputMeshType,In
> putMeshType>::
> GradientType,3>::PixelType,
>             TOutput=itk::Image<unsigned short,3>::PixelType
>         ]
>         
> c:\home\boettger\Insight\Code\BasicFilters\itkCastImageFilter.h(58)
> : see reference to class template instantiation
> 'itk::UnaryFunctorImageFilter<TInputImage,TOutputImage,TFuncti
> on>' being
> compiled
>         with
>         [
>             TInputImage=GradientImageType,
>             TOutputImage=OriginalImageType,
> 
> TFunction=itk::Functor::Cast<itk::Image<itk::DeformableMesh3DF
> ilter<InputMes
> hType,InputMeshType>::GradientType,3>::PixelType,itk::Image<unsigned
> short,3>::PixelType>
>         ]
>         main.cpp(448) : see reference to class template instantiation
> 'itk::CastImageFilter<TInputImage,TOutputImage>' being compiled
>         with
>         [
>             TInputImage=GradientImageType,
>             TOutputImage=OriginalImageType
>         ]
> 
> 
> 
> 
> Could anyone please help me?
> 
> Thanks,
> Thomas
> 
> 
> 
> 
> 
> 
> 
> 
> ---
> Dipl.-Inform. Thomas Boettger
> Deutsches Krebsforschungszentrum         (German Cancer 
> Research Center)
> Div. Medical and Biological Informatics H0100    Tel: (+49) 
> 6221-42 2328
> Im Neuenheimer Feld 280                          Fax: (+49) 
> 6221-42 2345
> D-69120 Heidelberg                            e-mail: 
> t.boettger@dkfz.de
> Germany                      
> http://www.dkfz.de/mbi/people/thomasb.shtml
> 
> 
> _______________________________________________
> Insight-users mailing list
> Insight-users@public.kitware.com
> http://public.kitware.com/mailman/listinfo/insight-users
>