[Community] [Insight-users] Resampling: defining the output size at 'runtime' (Update-time) relative to the input size

Vaaksiainen vaaksiainen at gmail.com
Thu Oct 17 02:55:18 EDT 2013


Thanks again for your comments Matt! As you see, I already 'graft' :)

Based on your comments, I modified my code which got me started. Then using
debugger further, I figured out I'd better implementing also
VerifyInputInformation() and GenerateInputRequestedRegion(). In outcome, I
now do have a working copy. So simple I'll just share it here. Not sure
what happens when region-index is nonzero but for time being its ok.

Best,
-Vaaksiainen

***
template < class TInputImage, class TOutputImage >
class ResizeImageFilter :
public itk::ImageToImageFilter<TInputImage, TOutputImage>
{
public:
    typedef ResizeImageFilter Self;
    typedef itk::ImageToImageFilter<TInputImage,TOutputImage>    Superclass;
    typedef itk::SmartPointer<Self>    Pointer;
    typedef typename TInputImage::SpacingType SpacingType;
    typedef typename TOutputImage::SizeType SizeType;

    itkNewMacro( Self );
    itkTypeMacro( ResizeImageFilter, itk::ImageToImageFilter );
    itkSetMacro( Scale, SpacingType );
    itkSetMacro( Size, SizeType );

protected:
    ResizeImageFilter()
    {
        m_Size.Fill( 0 );
        m_Scale.Fill( 1 );
    }
    ~ResizeImageFilter(){}
    void VerifyInputInformation() {}
    void GenerateData();
    void GenerateOutputInformation();
    void GenerateInputRequestedRegion();

private:
    ResizeImageFilter & operator=(const ResizeImageFilter &rhs);
    ResizeImageFilter( const ResizeImageFilter &rhs);

    SpacingType m_Scale;
    SizeType m_Size;
    SpacingType m_SpacingActual;
    SizeType m_SizeActual;
};
template < class TInputImage, class TOutputImage >
void ResizeImageFilter<TInputImage,TOutputImage>
::GenerateData()
{
    typedef itk::ResampleImageFilter<TInputImage,TOutputImage,float>
ResamplerType;
    ResamplerType::Pointer resampler = ResamplerType::New();
    resampler->SetOutputSpacing( m_SpacingActual );
    resampler->SetSize( m_SizeActual );
    resampler->SetInput( this->GetInput() );
    resampler->GraftOutput( this->GetOutput() );
    resampler->Update();
    this->GraftOutput( resampler->GetOutput() );
}
template < class TInputImage, class TOutputImage >
void ResizeImageFilter<TInputImage,TOutputImage>
::GenerateInputRequestedRegion()
{
    this->itk::ProcessObject::GenerateInputRequestedRegion();
}
template < class TInputImage, class TOutputImage >
void ResizeImageFilter<TInputImage,TOutputImage>
::GenerateOutputInformation()
{
    const TInputImage * p = this->GetInput();
    if( p )
    {
        const unsigned int N = TInputImage::ImageDimension;
        double decimation[N];
        m_SizeActual = p->GetRequestedRegion().GetSize();
        m_SpacingActual = p->GetSpacing();

        for ( unsigned int n = 0; n < N; ++n )
        {
            if ( m_Size[n] == 0 )
                decimation[n] = m_Scale[n];
            else
                decimation[n] = (double)m_Size[n]/(double)m_SizeActual[n];
        }
        for ( unsigned int n = 0; n < N; ++n )
        {
            m_SpacingActual[n] /= decimation[n];
            m_SizeActual[n] = (unsigned int)( (double)m_SizeActual[n] *
decimation[n] );
            if ( m_SpacingActual[n] <= 0.0 || m_SizeActual[n] == 0 )
                itkExceptionMacro("spacing[n] <= 0 || size[n] == 0" );
        }
        TOutputImage * pOut = this->GetOutput();
        if ( pOut )
        {
            pOut->CopyInformation( p );
            pOut->SetSpacing( m_SpacingActual );
            typename TOutputImage::RegionType region =
pOut->GetRequestedRegion();
            region.SetSize( m_SizeActual );
            pOut->SetLargestPossibleRegion( region );
            pOut->SetSpacing( m_SpacingActual );
        }
    }
}
***


2013/10/16 Matt McCormick <matt.mccormick at kitware.com>

> Hi Vaaksiainen,
>
> The filter should implement a GenerateOutputInformation() [1] method
> that does the size calculation there and assigns it to the output
> ImageRegion's.
>
> Hope this helps,
> Matt
>
> [1]
> http://www.itk.org/Doxygen/html/classitk_1_1ProcessObject.html#abe61fb6b7de8c443e7af1561bd722736
>
> On Wed, Oct 16, 2013 at 2:42 PM, Vaaksiainen <vaaksiainen at gmail.com>
> wrote:
> > Thanks for your quick reply, really appreciate.
> >
> > I could well be using itk::ScaleTransform but I use simply resampling
> over
> > hand written spacing. I think my problem is in fact in pipelining:
> >
> > void ResampleImageFilterEx<TInputImage,TOutputImage>::GenerateData()
> > {
> >     //....
> >     typename TInputImage::RegionType roi =
> > this->GetOutput()->GetRequestedRegion();
> >     typename TInputImage::SpacingType spacing =
> > this->GetInput()->GetSpacing();
> >     typename TInputImage::SizeType size = roi.GetSize();
> >
> >     for ( unsigned int i = 0; i < TInputImage::ImageDimension; ++i)
> >     {
> >         spacing[i] /= m_Decimation;
> >         size[i] = (unsigned int)( (double)size[i] * m_Decimation );
> >     }
> >     itk::ResampleImageFilter<TInputImage,TOutputImage>::Pointer
> resampler =
> >         itk::ResampleImageFilter<TInputImage,TOutputImage>::New();
> >
> >     resampler->SetOutputSpacing( inputSpacing );
> >     resampler->SetSize( inputSize );
> >     resampler->SetInput( this->GetInput() );
> >
> >     resampler->GraftOutput( this->GetOutput() );
> >     resampler->Update();
> >     resampler->GraftOutput( pyrup->GetOutput() );
> > }
> >
> > but if m_Decimation (or scale parameter) is given as parameter and input
> yet
> > unknown, I can't get regions propagating correctly. Because eventually I
> > want to call Update only for the tail filter.
> >
> > Best,
> > -Vaaksiainen
> >
> >
> >
> > 2013/10/16 Mike Chinander <chinander at gmail.com>
> >>
> >> Check out the following example:
> >> http://www.itk.org/Wiki/ITK/Examples/ImageProcessing/ScaleTransform
> >>
> >>
> >> On Wed, Oct 16, 2013 at 9:10 AM, Vaaksiainen <vaaksiainen at gmail.com>
> >> wrote:
> >>>
> >>> Hi folks,
> >>>
> >>> itk::ResampleImageFilter requires size being set before Update() so far
> >>> that I've understood it.
> >>>
> >>> That being said, I'd wish to implement resize image filter (derivative
> >>> for before mentioned) which samples the image based on scaling
> parameter
> >>> relative to its input size e.g.
> >>>
> >>> newWidth = scaling * oldWidth
> >>>
> >>> which I figure out at GenerateData() and yet, I'd wish to be able to
> put
> >>> this filter in the pipeline not knowing the size of the input image,
> e.g.
> >>>
> >>> Reader -> Filter#1 -> ResampleImageFilterEx -> Filter#2 -> Writer
> >>>
> >>> How PropagateRequestedRegion() works and how its implemented in
> >>> itk::ResampleImageFilter I'm not sure if I can do this at all.
> >>>
> >>> Please, any advice?
> >>>
> >>> Best,
> >>> -Vaaksiainen
> >>>
> >>>
> >>>
> >>>
> >>>
> >>> _____________________________________
> >>> Powered by www.kitware.com
> >>>
> >>> Visit other Kitware open-source projects at
> >>> http://www.kitware.com/opensource/opensource.html
> >>>
> >>> Kitware offers ITK Training Courses, for more information visit:
> >>> http://www.kitware.com/products/protraining.php
> >>>
> >>> Please keep messages on-topic and check the ITK FAQ at:
> >>> http://www.itk.org/Wiki/ITK_FAQ
> >>>
> >>> Follow this link to subscribe/unsubscribe:
> >>> http://www.itk.org/mailman/listinfo/insight-users
> >>>
> >>
> >>
> >> _____________________________________
> >> Powered by www.kitware.com
> >>
> >> Visit other Kitware open-source projects at
> >> http://www.kitware.com/opensource/opensource.html
> >>
> >> Kitware offers ITK Training Courses, for more information visit:
> >> http://www.kitware.com/products/protraining.php
> >>
> >> Please keep messages on-topic and check the ITK FAQ at:
> >> http://www.itk.org/Wiki/ITK_FAQ
> >>
> >> Follow this link to subscribe/unsubscribe:
> >> http://www.itk.org/mailman/listinfo/insight-users
> >>
> >
> >
> > _____________________________________
> > Powered by www.kitware.com
> >
> > Visit other Kitware open-source projects at
> > http://www.kitware.com/opensource/opensource.html
> >
> > Kitware offers ITK Training Courses, for more information visit:
> > http://www.kitware.com/products/protraining.php
> >
> > Please keep messages on-topic and check the ITK FAQ at:
> > http://www.itk.org/Wiki/ITK_FAQ
> >
> > Follow this link to subscribe/unsubscribe:
> > http://www.itk.org/mailman/listinfo/insight-users
> >
> > _______________________________________________
> > Community mailing list
> > Community at itk.org
> > http://public.kitware.com/cgi-bin/mailman/listinfo/community
> >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/community/attachments/20131017/a75ed492/attachment.html>
-------------- next part --------------
_____________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at
http://www.kitware.com/opensource/opensource.html

Kitware offers ITK Training Courses, for more information visit:
http://www.kitware.com/products/protraining.php

Please keep messages on-topic and check the ITK FAQ at:
http://www.itk.org/Wiki/ITK_FAQ

Follow this link to subscribe/unsubscribe:
http://www.itk.org/mailman/listinfo/insight-users


More information about the Community mailing list