[Insight-users] Andreas, teach me more!!!

Baoyun Li baoyun_li123 at yahoo.com
Mon Mar 2 14:13:28 EST 2009


Hi, Andreas:

Thanks a lot.

Baoyun




________________________________
From: Andreas Schuh <andreas.schuh.84 at googlemail.com>
To: Baoyun Li <baoyun_li123 at yahoo.com>
Cc: "insight-users at itk.org" <insight-users at itk.org>
Sent: Monday, March 2, 2009 12:23:46 PM
Subject: Re: Andreas, teach me more!!!


:)

Well, I think your problem is a wrong understanding of what happens when you assign an output image returned by GetOutput() to a smart pointer of the output image' type. This is my explanation why you unnecessarily instantiate (too bad I spelled this wrong last time ...) your image types in the main function

image_high = OutputImageType::New();

where later you actually have this statement in your template function:

InternalFilter = resampler->GetOutput();


...

In your first solution you passed the adress of your image object referred to by image_high pointer to IsotropicResample(). However, this function had no chance to change the pointer image_high to refer to the actual image object returned by resampler->GetOutput().

Now to your probable confusion: Each filter allocates its own output image objects of type Image. So, GetOutput() returns a pointer to these created output images. Assigning the returned pointer to a smart pointer just ensures that the reference counting is done properly and that the returned image stays alive even when the filter that produced it is destroyed. The images aren't copied.

In your solution you changed the object the local pointer InternalFilter is pointing to but not the object image_high is pointing to as you didn't change the pointer image_high itself. So, image_high still pointed to the image object you created at the very beginning by OutputImage::New(), which is definitely an empty not even allocated image. This image instance is totally unnecessary as the resample filter creates its own instance. The same applies to the reader.

Change the beginning of your main function as follows:

InputImageType::Pointer inputimage;
OutputImageType::Pointer image_high;

// reader update

inputimage = reader->GetOutput();

IsotropicResample(inputimage, image_high, scale);

// ...

BTW you should not need to template your IsotropicResample function over the dimension as the image types already include this information. Moreover, shouldn't you need to specify the template arguments explicitly. Just call the function as it wouldn't be a template and let the compiler decide which instantiation is needed depending on the types of the passed arguments.

Hope this helps and is explained well enough.

--
regards
Andreas

Am 02.03.2009 um 18:47 schrieb Baoyun Li <baoyun_li123 at yahoo.com>:


Hi, Andreas:

Thanks for your help.

Can you further teach me, what I actuall did in my old code:

TInternalFilter* InternalFilter

to me I transfered the pointer of OutputImageType to the template function, what ever I did in the sub function, I should be able to get the reampled image since I transfter a pointer to the function.

To me, it is same to pass pointer and reference. But obviously, I am wrong. Can you please teach me more.


Baoyun



________________________________
From: Andreas Schuh <andreas.schuh.84 at googlemail.com>
To: Andreas Schuh <andreas.schuh.84 at googlemail.com>
Cc: Baoyun Li <baoyun_li123 at yahoo.com>; "insight-users at itk.org" <insight-users at itk.org>
Sent: Monday, March 2, 2009 10:55:36 AM
Subject: Re: [Insight-users] Image Pointer was passed to the template functin, disirable change happended in the template function, image informatin was lost in the main function


I guess the problem you are talking about is that you want to pass the resampled image as out parameter InternalFilter of your IsotropicResample function, isn't it? But you're actually not doing what you want to. Use TInternalFilter::Pointer& as type for your out parameter InternalFilter and you will be able to use the result of the resampling after calling IsotropicResample in your main function.

--
regards
Andreas

Am 02.03.2009 um 17:48 schrieb Andreas Schuh <andreas.schuh.84 at googlemail.com>:


Hi Baoyun,

your code is somehow confusing.

Why do you instanciate inputimage in your main function at all? This isn't necessary as you later set the pointer to the image object create by the ImageFileReader filter.

Why do you instanciate an output image object in the main function and pass it to your IsotropicResample() template function where the parameter InternalFilter is set to the output of the ResampleFilter instance?

Moreover, there is the part missing where you compute the size that you set as OutputSize of the resample filter.

BTW To make your code more readable, you shouldn't name image types or image parameters using the postfix 'Filter'. Moreover, names of parameters or variables should start with a lower case letter for convenience. Upper case identifiers usually name types or functions/methods.


--
regards
Andreas

Am 02.03.2009 um 17:09 schrieb Baoyun Li <baoyun_li123 at yahoo.com>:




Dear All:
 
I am trying to do some resampling of image throuth template function. Below is example of my main program.
 
In the main program, I read an image and give inputimage=reader->GetOutput().
I decalare another image pointer,OutputImageType::Pointer image_high=OutputImageType::New();
both image pass to the tempalte functin as below
 
 IsotropicResample <3,InputImageType,OutputImageType,double> (inputimage,image_high,scale);

{ my template fucntion delcared as:

 template <unsigned int Dim,class TInputFilter, class TInternalFilter, class TScale> 
void IsotropicResample( TInputFilter* InputFilter, TInternalFilter* InternalFilter, TScale scale)


}

In the template function, I got the reampled image as following :

 resampler->Update();
   InternalFilter=resampler->GetOutput();
   writer->SetFileName("../data/out_test.hdr");
   writer->SetInput(InternalFilter);
       try
       {
      writer->Update();
       }
     catch( itk::ExceptionObject & excep )
      {
       std::cerr << "Exception caught !" << std::endl;
       std::cerr << excep << std::endl;
       }

I checked the result, the resampled image by the above writer is correct.

but I got empty image when I tried to write image_high defined in the main function. 

Can somebody tell me what will be the probolem

Baoyun

////code in the main program
 
  reader->SetFileName(argv[1]);
  InputImageType::Pointer inputimage=InputImageType::New();
  OutputImageType::Pointer image_high=OutputImageType::New();

   InputImageType::Pointer inputimage=InputImageType::New();
  OutputImageType::Pointer image_high=OutputImageType::New();

  try 
    {
    reader->Update();
    }
  catch( itk::ExceptionObject & excep )
    {
    std::cerr << "Exception caught!" << std::endl;
    std::cerr << excep << std::endl;
    }
inputimage=reader->GetOutput();

 IsotropicResample <3,InputImageType,OutputImageType,double> (inputimage,image_high,scale);

////template function

#ifndef _IsotropicResample_H_
#define _IsotropicResample_H_

#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif
#ifdef __BORLANDC__
#define ITK_LEAN_AND_MEAN
#endif

#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
// Software Guide : BeginCodeSnippet
#include "itkResampleImageFilter.h"
#include "itkRecursiveGaussianImageFilter.h"
// Software Guide : EndCodeSnippet



// Software Guide : BeginCodeSnippet
#include "itkIdentityTransform.h"
#include "itkLinearInterpolateImageFunction.h"
#include "itkAffineTransform.h"
// Software Guide : EndCodeSnippet
// Software Guide : BeginCodeSnippet
#include "itkIntensityWindowingImageFilter.h"
#include "itkCastImageFilter.h"
#include <itkWindowedSincInterpolateImageFunction.h> 
#include "itkBSplineInterpolateImageFunction.h"

// Software Guide : EndCodeSnippet


//template <class TInputFilter, class TInternalFilter, class TScale>
//void IsotropicResample( TInputFilter* InputFilter, TInternalFilter* InternalFilter, TScale scale);

template <unsigned int Dim,class TInputFilter, class TInternalFilter, class TScale>
void IsotropicResample( TInputFilter* InputFilter, TInternalFilter* InternalFilter, TScale scale)
{
  
  typedef  TInputFilter InputImageType;
  typedef TInternalFilter OutputImageType;
  typedef   float           InternalPixelType;
  typedef itk::Image< InternalPixelType, Dim >   InternalImageType;

  typename InputImageType::ConstPointer inputImage = InputFilter;

  typename InputImageType::SpacingType inputSpacing= inputImage->GetSpacing();

///// do reampling
  
   resampler->SetSize( size );
   resampler->Update();
   InternalFilter=resampler->GetOutput();
   writer->SetFileName("../data/out_test.hdr");
   writer->SetInput(InternalFilter);
       try
       {
      writer->Update();
       }
     catch( itk::ExceptionObject & excep )
      {
       std::cerr << "Exception caught !" << std::endl;
       std::cerr << excep << std::endl;
       }
 
// 

};

#endif


_____________________________________
Powered by www.kitware.com

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

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

listinfo/insight-users" target=_blank rel=nofollow>http://www.itk.org/mailman/listinfo/insight-users

ml>


      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20090302/8bb34c17/attachment-0001.htm>


More information about the Insight-users mailing list