From 840dbf3410bcca5934b36990e740ad2211b239f2 Mon Sep 17 00:00:00 2001
From: Thomas Feuvrier <thomas.feuvrier@c-s.fr>
Date: Thu, 2 Feb 2006 07:55:12 +0000
Subject: [PATCH] Finalisation des classes d'extraction de ROI et de canaux.

---
 Code/Common/otbExtractROI.h                  |  3 +-
 Code/Common/otbExtractROIBase.h              | 25 +-----
 Code/Common/otbVectorToRGBImageFilter.h      | 83 ++++++++++++++++++++
 Code/Common/otbVectorToRGBImageFilter.txx    | 67 ++++++++++++++++
 Code/Common/otbVectorToScalarImageFilter.h   | 69 ++++++++++++++++
 Code/Common/otbVectorToScalarImageFilter.txx | 63 +++++++++++++++
 6 files changed, 285 insertions(+), 25 deletions(-)
 create mode 100755 Code/Common/otbVectorToRGBImageFilter.h
 create mode 100755 Code/Common/otbVectorToRGBImageFilter.txx
 create mode 100755 Code/Common/otbVectorToScalarImageFilter.h
 create mode 100755 Code/Common/otbVectorToScalarImageFilter.txx

diff --git a/Code/Common/otbExtractROI.h b/Code/Common/otbExtractROI.h
index d7ddbd8555..8640e4609e 100755
--- a/Code/Common/otbExtractROI.h
+++ b/Code/Common/otbExtractROI.h
@@ -20,7 +20,8 @@ namespace otb
 {
 
 /** \class ExtractROI
- * \brief Extrait une partie d'une image mono-canal.
+ * \brief Extrait une partie d'une image (mono-canal ou multi-canal).
+ * Le Pixel peut etre de type simple ou alors un itk::RGBPixel, etc.
  * Cette classe s'appuie sur la classe "otb::ExtractROIBase"
  *
  */
diff --git a/Code/Common/otbExtractROIBase.h b/Code/Common/otbExtractROIBase.h
index b8dbefb46e..1731ba0c4c 100755
--- a/Code/Common/otbExtractROIBase.h
+++ b/Code/Common/otbExtractROIBase.h
@@ -20,31 +20,8 @@ namespace otb
 {
 
 /** \class ExtractROIBase
- * \brief Decrease the image size by cropping the image to the selected 
- * region bounds.
- *
- * ExtractROIBase changes the image boundary of an image by removing  
- * pixels outside the target region.  The target region must be specified.
- *
- * ExtractROIBase also collapses dimensions so that the input image 
- * may have more dimensions than the output image (i.e. 4-D input image
- * to a 3-D output image).  To specify what dimensions to collapse,
- * the ExtractionRegion must be specified.  For any dimension dim where
- * ExtractionRegion.Size[dim] = 0, that dimension is collapsed.  The 
- * index to collapse on is specified by ExtractionRegion.Index[dim].
- * For example, we have a image 4D = a 4x4x4x4 image, and we want 
- * to get a 3D image, 3D = a 4x4x4 image, specified as [x,y,z,2] from 4D 
- * (i.e. the 3rd "time" slice from 4D).  The ExtractionRegion.Size = 
- * [4,4,4,0] and ExtractionRegion.Index = [0,0,0,2].  
- *
- * The number of dimension in ExtractionRegion.Size and Index must = 
- * InputImageDimension.  The number of non-zero dimensions in 
- * ExtractionRegion.Size must = OutputImageDimension.
- *
- * This filter is implemented as a multithreaded filter.  It provides a 
- * ThreadedGenerateData() method for its implementation.
+ * \brief Classe de base, permettant d'extraire une partie d'une image (mono-canal ou multi-canal).
  * 
- * \ingroup GeometricTransforms
  */
 template <class TInputImage, class TOutputImage>
 class ITK_EXPORT ExtractROIBase:
diff --git a/Code/Common/otbVectorToRGBImageFilter.h b/Code/Common/otbVectorToRGBImageFilter.h
new file mode 100755
index 0000000000..74f1979b25
--- /dev/null
+++ b/Code/Common/otbVectorToRGBImageFilter.h
@@ -0,0 +1,83 @@
+
+#ifndef __otbVectorToRGBImageFilter_h
+#define __otbVectorToRGBImageFilter_h
+
+#include "itkImageToImageFilter.h"
+#include "itkImage.h"
+#include "itkVectorImage.h"
+#include "itkRGBPixel.h"
+
+namespace otb
+{
+  
+/** \class VectorToRGBImageFilter
+ *
+ * \brief Creer une image RGB a partir d'une image multi-canal de type VectorImage
+ * Il est possible de sélectionner les trois canaux RGB de l'image source que l'on souhaite insérer dans l'image destination.
+ *
+ * \ingroup Multithreaded
+ */
+
+
+template <class TInputPixel, class TOutputPixel, unsigned int VImageDimension=2>
+class ITK_EXPORT VectorToRGBImageFilter :
+    public itk::ImageToImageFilter< itk::VectorImage < TInputPixel, VImageDimension> , itk::Image<itk::RGBPixel<TOutputPixel> , VImageDimension> >
+{
+public:
+  /** Standard class typedefs. */
+  typedef VectorToRGBImageFilter  Self;
+  typedef itk::ImageToImageFilter< itk::VectorImage < TInputPixel, VImageDimension> , itk::Image<itk::RGBPixel<TOutputPixel> , VImageDimension> >  Superclass;
+  typedef itk::SmartPointer<Self>   Pointer;
+  typedef itk::SmartPointer<const Self>  ConstPointer;
+
+  /** Standard class macros */
+  itkNewMacro(Self);
+  itkTypeMacro(VectorToRGBImageFilter, itk::ImageToImageFilter) ;
+
+  typedef typename Superclass::OutputImageRegionType OutputImageRegionType ;
+
+  typedef itk::VectorImage<TInputPixel,VImageDimension>                 InputImageType;
+  typedef itk::Image<itk::RGBPixel<TOutputPixel>,VImageDimension>       OutputImageType;
+
+  typedef typename OutputImageType::PixelType OutputImagePixelType ;
+
+  /** Set/Get le numero de canal à traiter pour le canal rouge */
+  itkSetMacro(RedChannel,unsigned int);
+  itkGetConstMacro(RedChannel,unsigned int);
+  /** Set/Get le numero de canal à traiter pour le canal vert */
+  itkSetMacro(GreenChannel,unsigned int);
+  itkGetConstMacro(GreenChannel,unsigned int);
+  /** Set/Get le numero de canal à traiter pour le canal bleu */
+  itkSetMacro(BlueChannel,unsigned int);
+  itkGetConstMacro(BlueChannel,unsigned int);
+
+
+protected:
+  VectorToRGBImageFilter() ;
+  virtual ~VectorToRGBImageFilter() {}
+  
+  void ThreadedGenerateData(const OutputImageRegionType &outputRegionForThread,
+                            int threadId) ;
+ 
+private:
+  VectorToRGBImageFilter(const Self&); //purposely not implemented
+  void operator=(const Self&); //purposely not implemented
+  
+  
+  
+    /** Contient le numero de canal rouge a extraire */
+    unsigned int  m_RedChannel;
+    /** Contient le numero de canal vert a extraire */
+    unsigned int  m_GreenChannel;
+    /** Contient le numero de canal bleu a extraire */
+    unsigned int  m_BlueChannel;
+
+};
+
+} // end namespace otb
+
+#ifndef OTB_MANUAL_INSTANTIATION
+#include "otbVectorToRGBImageFilter.txx"
+#endif
+
+#endif
diff --git a/Code/Common/otbVectorToRGBImageFilter.txx b/Code/Common/otbVectorToRGBImageFilter.txx
new file mode 100755
index 0000000000..5d2e2b8d10
--- /dev/null
+++ b/Code/Common/otbVectorToRGBImageFilter.txx
@@ -0,0 +1,67 @@
+#ifndef _otbVectorToRGBImageFilter_txx
+#define _otbVectorToRGBImageFilter_txx
+
+#include "otbVectorToRGBImageFilter.h"
+
+#include "itkImageRegionIterator.h"
+#include "itkImageRegionConstIterator.h"
+#include "itkProgressReporter.h"
+#include "itkPixelTraits.h"
+
+namespace otb
+{
+
+/**
+ * Constructor
+ */
+template <class TInputPixel, class TOutputPixel, unsigned int VImageDimension>
+VectorToRGBImageFilter< TInputPixel, TOutputPixel, VImageDimension >
+::VectorToRGBImageFilter() :    m_RedChannel(1),
+                                m_GreenChannel(2),
+                                m_BlueChannel(3)
+{
+}
+
+
+/**
+ * ThreadedGenerateData Performs the pixel-wise addition
+ */
+template <class TInputPixel, class TOutputPixel, unsigned int VImageDimension>
+void
+VectorToRGBImageFilter< TInputPixel, TOutputPixel, VImageDimension >
+::ThreadedGenerateData( const OutputImageRegionType &outputRegionForThread,
+                        int threadId)
+{
+
+  typename OutputImageType::Pointer outputPtr = this->GetOutput();
+
+  typename InputImageType::Pointer inputPtr = (InputImageType*)(this->GetInput(0));
+  
+  itk::ImageRegionIterator<OutputImageType> outputIt(outputPtr, outputRegionForThread);
+
+  itk::ProgressReporter progress(this, 
+                            threadId, 
+                            outputRegionForThread.GetNumberOfPixels());
+  outputIt.GoToBegin();
+  typename OutputImageType::PixelType arrayPixel ;
+
+  itk::ImageRegionConstIterator< InputImageType > inputIt(inputPtr, outputRegionForThread);
+  inputIt.GoToBegin();
+  
+  while( !outputIt.IsAtEnd() ) 
+    {
+    typename OutputImageType::PixelType pix;
+
+    pix.SetRed(inputIt.Get().GetElement(m_RedChannel-1));
+    pix.SetGreen(inputIt.Get().GetElement(m_GreenChannel-1));
+    pix.SetBlue(inputIt.Get().GetElement(m_BlueChannel-1));
+    outputIt.Set(pix);
+    ++inputIt;
+    ++outputIt;
+    progress.CompletedPixel();
+    }
+}
+
+} // end namespace otb
+
+#endif
diff --git a/Code/Common/otbVectorToScalarImageFilter.h b/Code/Common/otbVectorToScalarImageFilter.h
new file mode 100755
index 0000000000..b5f19f8bc8
--- /dev/null
+++ b/Code/Common/otbVectorToScalarImageFilter.h
@@ -0,0 +1,69 @@
+
+#ifndef __otbVectorToScalarImageFilter_h
+#define __otbVectorToScalarImageFilter_h
+
+#include "itkImageToImageFilter.h"
+#include "itkImage.h"
+#include "itkVectorImage.h"
+
+namespace otb
+{
+  
+/** \class VectorToScalarImageFilter
+ *
+ * \brief Creer une image mono-canal a partir d'une image multi-canal (type VectorImage)
+ * Il est possible de sélectionner le canal de l'image source que l'on souhaite extraire dans l'image destination.
+ *
+ * \ingroup Multithreaded
+ */
+
+
+template <class TInputPixel, class TOutputPixel, unsigned int VImageDimension=2>
+class ITK_EXPORT VectorToScalarImageFilter :
+    public itk::ImageToImageFilter< itk::VectorImage < TInputPixel, VImageDimension> , itk::Image<TOutputPixel , VImageDimension> >
+{
+public:
+  /** Standard class typedefs. */
+  typedef VectorToScalarImageFilter  Self;
+  typedef itk::ImageToImageFilter< itk::VectorImage < TInputPixel, VImageDimension> , itk::Image<TOutputPixel , VImageDimension> >  Superclass;
+  typedef itk::SmartPointer<Self>   Pointer;
+  typedef itk::SmartPointer<const Self>  ConstPointer;
+
+  /** Standard class macros */
+  itkNewMacro(Self);
+  itkTypeMacro(VectorToScalarImageFilter, itk::ImageToImageFilter) ;
+
+  typedef typename Superclass::OutputImageRegionType OutputImageRegionType ;
+
+  typedef itk::VectorImage<TInputPixel,VImageDimension>                 InputImageType;
+  typedef itk::Image<TOutputPixel,VImageDimension>                      OutputImageType;
+
+  typedef typename OutputImageType::PixelType OutputImagePixelType ;
+
+  /** Set/Get le numero de canal à traiter */
+  itkSetMacro(Channel,unsigned int);
+  itkGetConstMacro(Channel,unsigned int);
+
+protected:
+  VectorToScalarImageFilter() ;
+  virtual ~VectorToScalarImageFilter() {}
+  
+  void ThreadedGenerateData(const OutputImageRegionType &outputRegionForThread,
+                            int threadId) ;
+ 
+private:
+  VectorToScalarImageFilter(const Self&); //purposely not implemented
+  void operator=(const Self&); //purposely not implemented
+  
+    /** Contient le numero de canal a extraire */
+    unsigned int  m_Channel; // [1...]
+
+};
+
+} // end namespace otb
+
+#ifndef OTB_MANUAL_INSTANTIATION
+#include "otbVectorToScalarImageFilter.txx"
+#endif
+
+#endif
diff --git a/Code/Common/otbVectorToScalarImageFilter.txx b/Code/Common/otbVectorToScalarImageFilter.txx
new file mode 100755
index 0000000000..78754b53f8
--- /dev/null
+++ b/Code/Common/otbVectorToScalarImageFilter.txx
@@ -0,0 +1,63 @@
+#ifndef _otbVectorToScalarImageFilter_txx
+#define _otbVectorToScalarImageFilter_txx
+
+#include "otbVectorToScalarImageFilter.h"
+
+#include "itkImageRegionIterator.h"
+#include "itkImageRegionConstIterator.h"
+#include "itkProgressReporter.h"
+#include "itkPixelTraits.h"
+
+namespace otb
+{
+
+/**
+ * Constructor
+ */
+template <class TInputPixel, class TOutputPixel, unsigned int VImageDimension>
+VectorToScalarImageFilter< TInputPixel, TOutputPixel, VImageDimension >
+::VectorToScalarImageFilter() :    m_Channel(1)
+{
+}
+
+
+/**
+ * ThreadedGenerateData Performs the pixel-wise addition
+ */
+template <class TInputPixel, class TOutputPixel, unsigned int VImageDimension>
+void
+VectorToScalarImageFilter< TInputPixel, TOutputPixel, VImageDimension >
+::ThreadedGenerateData( const OutputImageRegionType &outputRegionForThread,
+                        int threadId)
+{
+
+  typename OutputImageType::Pointer outputPtr = this->GetOutput();
+
+  typename InputImageType::Pointer inputPtr = (InputImageType*)(this->GetInput(0));
+  
+  itk::ImageRegionIterator<OutputImageType> outputIt(outputPtr, outputRegionForThread);
+
+  itk::ProgressReporter progress(this, 
+                            threadId, 
+                            outputRegionForThread.GetNumberOfPixels());
+  outputIt.GoToBegin();
+  typename OutputImageType::PixelType arrayPixel ;
+
+  itk::ImageRegionConstIterator< InputImageType > inputIt(inputPtr, outputRegionForThread);
+  inputIt.GoToBegin();
+  
+  while( !outputIt.IsAtEnd() ) 
+    {
+//    typename OutputImageType::PixelType pix;
+
+//    pix.Set();
+    outputIt.Set(inputIt.Get().GetElement(m_Channel-1));
+    ++inputIt;
+    ++outputIt;
+    progress.CompletedPixel();
+    }
+}
+
+} // end namespace otb
+
+#endif
-- 
GitLab