Commits

Rashad M committed a7c1866

ENH: Adding GammaCorrectionFilter

  • Participants
  • Parent commits fe62c94

Comments (0)

Files changed (6)

File Applications/CMakeLists.txt

 add_subdirectory(Segmentation)
 add_subdirectory(Test)
 add_subdirectory(Utils)
-#add_subdirectory(ContrastEnhancement)
+add_subdirectory(ContrastEnhancement)

File Applications/ContrastEnhancement/otbGammaCorrection.cxx

+
+#include <iostream>
+#include <sstream>
+
+
+#include "otbImage.h"
+#include "otbVectorImage.h"
+#include "otbGammaFunctor.h"
+#include "otbImageFileWriter.h"
+#include "otbImageFileReader.h"
+#include "otbGammaCorrectionFilter.h"
+
+#include "otbWrapperApplication.h"
+#include "otbWrapperApplicationFactory.h"
+
+namespace otb
+{
+namespace Wrapper
+{
+class GammaCorrection : public Application
+{
+public:
+
+  typedef GammaCorrection                Self;
+  typedef Application                    Superclass;
+  typedef itk::SmartPointer<Self>        Pointer;
+  typedef itk::SmartPointer<const Self>  ConstPointer;
+
+  itkNewMacro(Self);
+
+  itkTypeMacro(GammaCorrection, otb::Application);
+
+
+private:
+
+  void DoInit()
+  {
+    this->SetHaveInXML(false);
+    this->SetHaveOutXML(false);
+    SetName("GammaCorrection");
+    SetDescription("This application encode and decode gamma of the input image");
+
+    SetDocName("GammaCorrection");
+    SetDocLongDescription("This application encode and decode gamma of the input image. If value gamma is < 1 it does gamma encoding and if > 1 it does gamma decoding.");
+    SetDocLimitations("None");
+    SetDocAuthors("OTB-Team");
+    SetDocSeeAlso(" ");
+
+    AddDocTag("color");
+    AddDocTag("histogram");
+
+    AddParameter(ParameterType_InputImage, "in", "Input Image");
+    MandatoryOn("in");
+
+    AddParameter(ParameterType_Float, "gamma", "Input gamma value");
+    MandatoryOn("gamma");
+    SetDefaultParameterFloat("gamma", 1.0);
+
+    AddParameter(ParameterType_OutputImage, "out", "Output Image");
+    MandatoryOn("out");
+
+    SetDocExampleParameterValue("in", "rgbInput.tif");
+    SetDocExampleParameterValue("gamma", "2.2");
+    SetDocExampleParameterValue("out", "gammaCorrected.tif");
+
+  }
+
+
+  void DoUpdateParameters()
+  {
+
+  }
+
+  void DoExecute()
+  {
+    FloatVectorImageType::Pointer inImage = GetParameterImage("in");
+    double gamma = GetParameterFloat("gamma");
+    otbAppLogDEBUG( << "Gamma = " << gamma <<std::endl );
+
+    GammaFilterType::Pointer m_GammaFilter = GammaFilterType::New();
+
+    m_GammaFilter->SetInput(inImage);
+    m_GammaFilter->SetGamma(gamma);
+    m_GammaFilter->Update();
+
+    SetParameterOutputImage("out", m_GammaFilter->GetOutput());
+  }
+
+  typedef typename FloatVectorImageType::PixelType PixelType;
+  typedef otb::Functor::GammaFunctor<PixelType, PixelType> GammaFunctorType;
+  typedef otb::GammaCorrectionFilter<FloatVectorImageType,FloatVectorImageType,GammaFunctorType> GammaFilterType;
+
+};
+
+} //end namespace Wrapper
+} //end namespace otb
+
+OTB_APPLICATION_EXPORT(otb::Wrapper::GammaCorrection)

File Code/CMakeLists.txt

   ADD_SUBDIRECTORY(Wrappers)
 ENDIF(BUILD_APPLICATIONS)
 
-#ADD_SUBDIRECTORY(ContrastEnhancement)
+ADD_SUBDIRECTORY(ContrastEnhancement)
 
 IF(OTB_COMPILE_WITH_FULL_WARNING)
   IF(CMAKE_COMPILER_IS_GNUCXX)

File Code/ContrastEnhancement/otbGammaCorrectionFilter.h

+/*=========================================================================
+
+  Program:   ORFEO Toolbox
+  Language:  C++
+  Date:      $Date$
+  Version:   $Revision$
+
+
+  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
+  See OTBCopyright.txt for details.
+
+  Some parts of this code are derived from ITK. See ITKCopyright.txt
+  for details.
+
+
+     This software is distributed WITHOUT ANY WARRANTY; without even
+     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+     PURPOSE.  See the above copyright notices for more information.
+
+=========================================================================*/
+#ifndef __otbGammaCorrectionFilter_h
+#define __otbGammaCorrectionFilter_h
+
+#include "itkUnaryFunctorImageFilter.h"
+
+namespace otb
+{
+
+template<class TInput, class TOutput, class TFunctor >
+class ITK_EXPORT GammaCorrectionFilter :
+    public itk::UnaryFunctorImageFilter <TInput, TOutput,TFunctor >
+{
+public:
+
+  typedef GammaCorrectionFilter                                     Self;
+  typedef itk::UnaryFunctorImageFilter<TInput, TOutput, TFunctor>   Superclass;
+  typedef itk::SmartPointer<Self>                                   Pointer;
+  typedef itk::SmartPointer<const Self>                             ConstPointer;
+
+  typedef typename TInput::InternalPixelType   InternalPixelType;
+  typedef itk::Index<3> IndexType;
+
+  itkNewMacro(Self);
+
+  itkTypeMacro(Self, Superclass);
+
+  typedef typename TInput::RegionType          RegionType;
+  typedef typename TInput::Pointer            InputImagePointer;
+  typedef typename TInput::PixelType           PixelType;
+
+  void SetGamma(double gamma)
+  {
+    this->GetFunctor().SetGamma(gamma);
+    this->Modified();
+  }
+
+  virtual void GenerateOutputInformation();
+  virtual void BeforeThreadedGenerateData();
+
+protected:
+
+  GammaCorrectionFilter()
+    {
+    this->SetNumberOfRequiredInputs( 1 );
+    }
+  virtual ~GammaCorrectionFilter() {  }
+
+  void PrintSelf(std::ostream& os, itk::Indent indent) const { }
+
+private:
+
+  GammaCorrectionFilter(const Self&); //purposely not implemented
+  void operator=(const Self&); //purposely not implemented
+
+};
+
+} // end namespace otb
+
+#ifndef OTB_MANUAL_INSTANTIATION
+#include "otbGammaCorrectionFilter.txx"
+#endif
+
+#endif

File Code/ContrastEnhancement/otbGammaCorrectionFilter.txx

+/*=========================================================================
+
+  Program:   ORFEO Toolbox
+  Language:  C++
+  Date:      $Date$
+  Version:   $Revision$
+
+
+  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
+  See OTBCopyright.txt for details.
+
+  Some parts of this code are derived from ITK. See ITKCopyright.txt
+  for details.
+
+
+     This software is distributed WITHOUT ANY WARRANTY; without even
+     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+     PURPOSE.  See the above copyright notices for more information.
+
+=========================================================================*/
+#ifndef __otbGammaCorrectionFilter_txx
+#define __otbGammaCorrectionFilter_txx
+
+
+#include "otbGammaCorrectionFilter.h"
+
+namespace otb
+{
+
+template <class TInput, class TOutput, class TFunctor>
+void
+GammaCorrectionFilter<TInput, TOutput, TFunctor>
+::GenerateOutputInformation()
+{
+  Superclass::GenerateOutputInformation();
+
+}
+
+template <class TInput, class TOutput, class TFunctor>
+void
+GammaCorrectionFilter<TInput,TOutput, TFunctor>
+::BeforeThreadedGenerateData()
+{
+
+}
+
+} // end namespace otb
+#endif

File Code/ContrastEnhancement/otbGammaFunctor.h

+/*=========================================================================
+
+  Program:   ORFEO Toolbox
+  Language:  C++
+  Date:      $Date$
+  Version:   $Revision$
+
+
+  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
+  See OTBCopyright.txt for details.
+
+  Some parts of this code are derived from ITK. See ITKCopyright.txt
+  for details.
+
+
+     This software is distributed WITHOUT ANY WARRANTY; without even
+     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+     PURPOSE.  See the above copyright notices for more information.
+
+=========================================================================*/
+#ifndef __otbGammaFunctor_h
+#define __otbGammaFunctor_h
+
+#include "vnl/vnl_math.h"
+
+namespace otb
+{
+namespace Functor
+{
+
+template<class TInput, class TOutput>
+class GammaFunctor {
+
+public:
+
+  GammaFunctor() {  }
+
+  ~GammaFunctor() {  }
+
+  inline void SetGamma(double gamma)
+  {
+    if(gamma == 0)
+      {
+      std::cerr << "Error: Division by zero " << __FILE__ << ":" << __LINE__ << std::endl;
+      }
+    else if(gamma < 0)
+      {
+      std::cerr << "Gamma Encoding"  << std::endl;
+      }
+    else
+      {
+      std::cerr << "Gamma Decoding"  << std::endl;
+      }
+
+    m_Gamma = 1.0 / gamma;
+  }
+
+  inline TOutput operator ()(const TInput& input) const
+  {
+    typedef typename TInput::ValueType InputType;
+    typedef typename TOutput::ValueType OutputType;
+
+    TOutput output;
+    output.SetSize(input.Size());
+
+   for (unsigned int i = 0; i < input.Size(); ++i)
+     {
+//output[i] = vcl_pow(m_Range *  (input[i] / m_Range) , m_Gamma);
+     output[i] = vcl_pow(input[i] , m_Gamma);
+     }
+    return output;
+  }
+
+private:
+  double m_Gamma;
+
+};
+
+} //end namespace Functor
+} //end namespace otb
+
+#endif