[Insight-users] How to resample a 3D RGB image ?

Gavin Baker gavinb+xtk at cs.mu.OZ.AU
Thu Oct 14 01:16:25 EDT 2004


Hello Yu,

On Wed, Oct 13, 2004 at 11:47:33AM +0800, Yu Long wrote:

>    Now I explain the problem I encountered in detail.

>    In short, my problem is how to resample a 3D RGB image.  I have made an
>    3D RGB image( about 600 * 400 * 1000) from 2D RGB image slices.But the
>    3D RGB image is too large to process.So I want to resample it to reduce
>    the image size.

As well as using the ResampleImageFilter to reduce the resolution of the
input volume, you may also want to consider using volumes of interest,
ExtractImageFilter, and even the MultiResolutionPyramidImageFilter
(depending on your data and the rest of your pipeline, of course).

There is a full example that shows how to use the resampling filter with RGB
data:

    Examples/Filtering/ResampleImageFilter6.cxx 

Luis: please note that on line 38 of this example, the comparison for argc
should probably be "< 3".

>    As I have known, ResampleImageFilter needs an interpolator. I tried
>    VectorInterpolateImageFunction and
>    VectorLinearInterpolateImageFunction,but got compilation errors. The
>    first error is shown below:
> 
> [...]
> 
> Now I have no idea about how to correct this compilition error.
> My code is attached,which is modified from an itk example.

There were a couple of tricky little problems, mainly to do with types and
templating.  I have attached the diffs to your code I made to get it
working.

The diffs are:

- remove the numeric traits include, which isn't really needed

- include and use the _Vector_ResampleImageFilter, not the regular one

- it's preferrable to typedef the pixel type

- the interpolator needs the coords type specified as double (as it defaults
  to float, which does not mix with the filter's internal type)

- zero is a "magic number" to the compiler, and it gets a little confused
  when you try to use it (as opposed to say 42) as the default value (which
  is actually passed to a pixel ctor).  Cast it to the pixel's component
  type and all's well (this is only really required for 0, which is a bit
  odd but hey, it works).

- add try/catch blocks around the writer update, otherwise we don't get to
  see the exception text if anything goes wrong

Regards,

  :: Gavin

-- 
Gavin Baker                                      Complex Systems Group
http://www.cs.mu.oz.au/~gavinb             The University of Melbourne
-------------- next part --------------
--- new02.cxx 2004-10-14 15:02:15.000000000 +1000
+++ resampleRGB.cxx      2004-10-14 15:00:19.000000000 +1000
@@ -38,7 +38,6 @@
 #include "itkImage.h"
 #include "itkImageFileReader.h"
 #include "itkImageFileWriter.h"
-#include "itkNumericTraitsRGBPixel.h"
 
 //  Software Guide : BeginLatex
 //
@@ -50,7 +49,7 @@
 
 
 // Software Guide : BeginCodeSnippet
-#include "itkResampleImageFilter.h"
+#include "itkVectorResampleImageFilter.h"
 // Software Guide : EndCodeSnippet
 
 
@@ -97,9 +96,11 @@
   //  Software Guide : EndLatex 
 
   // Software Guide : BeginCodeSnippet
-  const     unsigned int   Dimension = 3;
-  typedef    itk::RGBPixel < unsigned char >  InputPixelType;
-  typedef   itk::RGBPixel <unsigned char >  OutputPixelType;
+  const     unsigned int   Dimension = 2;
+  typedef unsigned char PixelComponentType;
+
+  typedef    itk::RGBPixel < PixelComponentType >  InputPixelType;
+  typedef   itk::RGBPixel < PixelComponentType >  OutputPixelType;
   typedef itk::Image< InputPixelType,  Dimension >   InputImageType;
   typedef itk::Image< OutputPixelType, Dimension >   OutputImageType;
   // Software Guide : EndCodeSnippet
@@ -127,7 +128,7 @@
   //  Software Guide : EndLatex 
 
   // Software Guide : BeginCodeSnippet
-  typedef itk::ResampleImageFilter<InputImageType,OutputImageType> FilterType;
+  typedef itk::VectorResampleImageFilter<InputImageType,OutputImageType> FilterType;
   FilterType::Pointer filter = FilterType::New();
   // Software Guide : EndCodeSnippet
 
@@ -175,7 +176,7 @@
   
   // Software Guide : BeginCodeSnippet
   typedef itk::VectorLinearInterpolateImageFunction< 
-                       InputImageType>  InterpolatorType;
+      InputImageType, double >  InterpolatorType;
   // Software Guide : EndCodeSnippet
 
 
@@ -208,8 +209,7 @@
   //  Software Guide : EndLatex 
 
   // Software Guide : BeginCodeSnippet
-  //filter->SetDefaultPixelValue( itk::NumericTraits<RGBPixel<unsigned char> >::Zero() );
-  filter->SetDefaultPixelValue( 0);
+  filter->SetDefaultPixelValue(static_cast<PixelComponentType>(0));
   // Software Guide : EndCodeSnippet
 
 
@@ -337,7 +337,15 @@
 
   if( exampleAction == 1 )
     {
-    writer->Update();
+        try
+        {
+            writer->Update();
+        }
+        catch( itk::ExceptionObject & excp )
+        {
+            std::cerr << "Exception thrown " << std::endl;
+            std::cerr << excp << std::endl;
+        }
     }
 
 


More information about the Insight-users mailing list