Commit d3651dc7 authored by Rashad Kanavath's avatar Rashad Kanavath

ENH: add FFT forward and inverse transform

parent 9439a2b2
......@@ -21,13 +21,68 @@
#include "otbWaveletImageFilter.h"
#include "otbWaveletInverseImageFilter.h"
#include "otbWaveletGenerator.h"
#include "itkConfigure.h"
#include <itkConfigure.h>
#include <itkForwardFFTImageFilter.h>
#include <itkInverseFFTImageFilter.h>
#include <itkUnaryFunctorImageFilter.h>
namespace otb
{
namespace Wrapper
{
template< class TInput, class TOutput>
class FromComplexPixel
{
public:
FromComplexPixel ( ) { };
~FromComplexPixel( ) { };
bool operator!=( const FromComplexPixel & ) const
{
return false;
}
bool operator==( const FromComplexPixel & other ) const
{
return !(*this != other);
}
inline TOutput operator( )( const TInput & A ) const
{
TOutput out;
out.SetSize(2);
out[0] = A.real();
out[1] = A.imag();
return out;
}
};
template< class TInput, class TOutput>
class ToComplexPixel
{
public:
ToComplexPixel ( ) { };
~ToComplexPixel( ) { };
bool operator!=( const ToComplexPixel & ) const
{
return false;
}
bool operator==( const ToComplexPixel & other ) const
{
return !(*this != other);
}
inline TOutput operator( )( const TInput & A ) const
{
TOutput out(A[0], A[1]);
return out;
}
};
class DomainTransform : public Application
{
public:
......@@ -35,7 +90,10 @@ namespace otb
typedef DomainTransform Self;
typedef Application Superclass;
typedef itk::SmartPointer<Self> Pointer;
typedef itk::SmartPointer<const Self> ConstPointer;
typedef float TInputPixel;
typedef float TOutputPixel;
/** Standard macro */
itkNewMacro(Self);
......@@ -47,11 +105,8 @@ namespace otb
void DoInit() ITK_OVERRIDE
{
SetName("DomainTransform");
#if defined(ITK_USE_FFTWF) || defined(ITK_USE_FFTWD)
const char * app_descr = "Domain Transform application for wavelet and fourier";
#else
const char * app_descr = "Domain Transform application for wavelet";
#endif
SetDescription(app_descr);
// Documentation
......@@ -69,10 +124,10 @@ namespace otb
AddParameter(ParameterType_Choice, "mode", "mode");
SetParameterDescription("mode", "transform mode");
#if defined(ITK_USE_FFTWF) || defined(ITK_USE_FFTWD)
//#if defined(ITK_USE_FFTWF) || defined(ITK_USE_FFTWD)
AddChoice("mode.fft", "FFT transform");
SetParameterDescription("mode.fft", "FFT transform");
#endif
//#endif
AddChoice("mode.wavelet", "wavelet");
SetParameterDescription("mode.wavelet", "Wavelet transform");
AddParameter(ParameterType_Choice,
......@@ -130,90 +185,34 @@ namespace otb
#endif
}
template<otb::Wavelet::Wavelet TWaveletOperator>
void DoWaveletTransform(const int direction,
const std::string inkey = "in",
const std::string outkey = "out")
{
typedef FloatImageType TInputImage;
typedef FloatImageType TOutputImage;
typedef typename TInputImage::Pointer TInputImagePointer;
TInputImagePointer inImage = GetParameterImage<TInputImage>(inkey);
inImage->UpdateOutputInformation();
if( direction == 0)
{
typedef otb::WaveletImageFilter<
TInputImage,
TOutputImage,
TWaveletOperator> TWaveletImageFilter;
typedef typename
TWaveletImageFilter::Pointer
TWaveletImageFilterPointer;
TWaveletImageFilterPointer waveletImageFilter =
TWaveletImageFilter::New();
waveletImageFilter->SetInput(inImage);
waveletImageFilter->Update();
SetParameterOutputImage< TOutputImage >
(outkey,
waveletImageFilter->GetOutput()
);
}
else if(direction == 1)
{
typedef otb::WaveletInverseImageFilter<
TInputImage,
TOutputImage,
TWaveletOperator > TWaveletImageFilter;
typedef typename
TWaveletImageFilter::Pointer
TWaveletImageFilterPointer;
TWaveletImageFilterPointer waveletImageFilter =
TWaveletImageFilter::New();
waveletImageFilter->SetInput(inImage);
waveletImageFilter->Update();
SetParameterOutputImage< TOutputImage >
( outkey,
waveletImageFilter->GetOutput() );
}
else
{
itkExceptionMacro( << "direction must be either 0 or 1")
}
}
void DoExecute() ITK_OVERRIDE
{
int direction = GetParameterInt("direction");
int mode = GetParameterInt("mode");
if( direction != 0 || direction != 1)
{
itkExceptionMacro( << "direction must be either 'fwd' or 'inv'");
}
if( mode != 0 || mode != 1)
{
itkExceptionMacro( << "mode must be either 'fft' or 'wavelet'");
}
if ( mode == 1)
{
int wavelet_type = GetParameterInt("mode.wavelet.form");
switch (wavelet_type)
{
case 0:
DoWaveletTransform<otb::Wavelet::HAAR> (direction);
break;
DoWaveletTransform<otb::Wavelet::HAAR> ( direction);
break;
case 1:
DoWaveletTransform< otb::Wavelet::DB4 > (direction);
break;
......@@ -240,20 +239,163 @@ namespace otb
break;
case 9:
DoWaveletTransform<otb::Wavelet::SYMLET8> (direction);
break;
break;
default:
itkExceptionMacro( << "Invalid wavelet type: '" << wavelet_type << "'");
break;
}
}
}
// fft ttransform
else
{
itkExceptionMacro( << "fft NOT IMPLEMENTED yet");
//forward fft
if (direction == 0 )
{
typedef otb::Image<TInputPixel> TInputImage;
typedef typename TInputImage::Pointer TInputImagePointer;
//get input paramter as otb::Image<TInputPixel>
TInputImagePointer inImage = GetParameterImage<TInputImage>("in");
inImage->UpdateOutputInformation();
//typedef itk::::ForwardFFTImageFilter over otbImage< TInputPixel >
typedef itk::ForwardFFTImageFilter < TInputImage> FFTFilter;
FFTFilter::Pointer fwdFilter = FFTFilter::New();
fwdFilter->SetInput( inImage );
//typedef VectorImage for output of UnaryFunctorImageFilter
typedef otb::VectorImage<TOutputPixel> TOutputImage;
//UnaryFunctorImageFilter for Complex to VectorImage
typedef itk::UnaryFunctorImageFilter<
typename FFTFilter::OutputImageType,
TOutputImage,
FromComplexPixel<
typename FFTFilter::OutputImageType::PixelType,
TOutputImage::PixelType> > UnaryFunctorImageFilter;
//convert complex pixel to variable length vector
//with unaryfunctor image filter
UnaryFunctorImageFilter::Pointer unaryFunctorImageFilter
= UnaryFunctorImageFilter::New();
unaryFunctorImageFilter->SetInput(fwdFilter->GetOutput());
unaryFunctorImageFilter->Update();
//set output image
SetParameterOutputImage<TOutputImage>
( "out", unaryFunctorImageFilter->GetOutput() );
}
//inverse fft
else
{
typedef otb::VectorImage<TInputPixel> TInputImage;
typedef typename TInputImage::Pointer TInputImagePointer;
TInputImagePointer inImage = GetParameterImage("in");
inImage->UpdateOutputInformation();
// typedef TComplexImage for InverseFFTImageFilter input
// This a image type of std::complex<TInputPixel>
typedef otb::Image<
std::complex<TInputPixel>, 2 > TComplexImage;
//typedef TOutputImage for InverseFFTImageFilter output
typedef otb::Image< TOutputPixel > TOutputImage;
// a unary functor to convert vectorimage to complex image
typedef itk::UnaryFunctorImageFilter
<TInputImage,
TComplexImage,
ToComplexPixel
<TInputImage::PixelType,
TComplexImage::PixelType> > UnaryFunctorImageFilter;
UnaryFunctorImageFilter::Pointer
unary_filter = UnaryFunctorImageFilter::New();
unary_filter->SetInput(inImage);
//typedef itk::::InverseFFTImageFilter over TComplexImage
typedef itk::InverseFFTImageFilter
< TComplexImage,
TOutputImage > FFTFilter;
FFTFilter::Pointer invFilter = FFTFilter::New();
invFilter->SetInput( unary_filter->GetOutput() );
invFilter->Update();
//set output image
SetParameterOutputImage<TOutputImage>( "out", invFilter->GetOutput() );
}
}
}
template<otb::Wavelet::Wavelet TWaveletOperator>
void DoWaveletTransform(const int direction,
const std::string inkey = "in",
const std::string outkey = "out")
{
typedef otb::Image< TInputPixel > TInputImage;
typedef otb::Image< TOutputPixel > TOutputImage;
typedef typename TInputImage::Pointer TInputImagePointer;
TInputImagePointer inImage = GetParameterImage<TInputImage>(inkey);
inImage->UpdateOutputInformation();
if( direction == 0)
{
typedef otb::WaveletImageFilter<
TInputImage,
TOutputImage,
TWaveletOperator> TWaveletImageFilter;
typedef typename
TWaveletImageFilter::Pointer
TWaveletImageFilterPointer;
TWaveletImageFilterPointer waveletImageFilter =
TWaveletImageFilter::New();
waveletImageFilter->SetInput(inImage);
waveletImageFilter->Update();
SetParameterOutputImage<TOutputImage>(outkey, waveletImageFilter->GetOutput() );
}
else
{
typedef otb::WaveletInverseImageFilter<
TInputImage,
TOutputImage,
TWaveletOperator > TWaveletImageFilter;
typedef typename
TWaveletImageFilter::Pointer
TWaveletImageFilterPointer;
TWaveletImageFilterPointer waveletImageFilter =
TWaveletImageFilter::New();
waveletImageFilter->SetInput(inImage);
waveletImageFilter->Update();
SetParameterOutputImage<TOutputImage>( outkey, waveletImageFilter->GetOutput() );
}
}
};
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment