Commit 2ec06ed1 authored by Cédric Traizet's avatar Cédric Traizet

Merge branch 'replace_unary_functor_image_filter' into 'develop'

Remove UnaryFunctorImageFilter

See merge request !427
parents f514aeb5 ca11121b
Pipeline #560 failed with stage
in 25 seconds
......@@ -119,7 +119,7 @@ int main(int argc, char* argv[])
typedef otb::UnConstrainedLeastSquareImageFilter<ImageType, ImageType, double> UCLSUnmixingFilterType;
UCLSUnmixingFilterType::Pointer unmixer = UCLSUnmixingFilterType::New();
unmixer->SetInput(rescaler->GetOutput());
unmixer->SetMatrix(endMember2Matrix->GetMatrix());
unmixer->GetModifiableFunctor().SetMatrix(endMember2Matrix->GetMatrix());
unmixer->SetNumberOfThreads(1); // FIXME : currently buggy
......
......@@ -39,7 +39,7 @@
#include "otbLabelImageToVectorDataFilter.h"
#include "itkBinaryThresholdImageFilter.h"
#include "otbUnaryFunctorImageFilter.h"
#include "otbFunctorImageFilter.h"
namespace otb
{
......@@ -79,6 +79,7 @@ public:
typedef itk::BinaryThresholdImageFilter<LabelImageType,
LabelImageType> ThresholdFilterType;
template <class TInput, class TOutput>
struct EncoderFunctorType
{
StatsFilterType::LabelPopulationMapType* m_CountMap;
......@@ -92,14 +93,19 @@ public:
static constexpr size_t m_NbStatsPerBand{4};
static constexpr size_t m_NbGlobalStats{1};
size_t GetOutputSize()
size_t OutputSize(const std::array<size_t, 1>&) const
{
return m_NbInputComponents*m_NbStatsPerBand+m_NbGlobalStats;
}
FloatVectorImageType::PixelType operator()(LabelValueType const &pix)
size_t OutputSize() const
{
FloatVectorImageType::PixelType outPix(GetOutputSize());
return m_NbInputComponents * m_NbStatsPerBand + m_NbGlobalStats;
}
TOutput operator()(TInput const& pix)
{
TOutput outPix(OutputSize());
outPix.Fill(m_OutBvValue);
if(pix != m_InNoData)
{
......@@ -129,9 +135,8 @@ public:
else return false;
}
};
typedef otb::UnaryFunctorImageFilter<LabelImageType,
FloatVectorImageType,
EncoderFunctorType> EncoderFilterType;
typedef otb::FunctorImageFilter<EncoderFunctorType<LabelValueType, FloatVectorImageType::PixelType>> EncoderFilterType;
/** Standard macro */
itkNewMacro(Self);
itkTypeMacro(ZonalStatistics, Application);
......@@ -406,14 +411,18 @@ public:
m_EncoderFilter->SetInput(m_RasterizeFilter->GetOutput());
}
m_EncoderFilter->SetFunctor(EncoderFunctorType{&m_CountMap, &m_MeanMap, &m_StdMap,
&m_MinMap, &m_MaxMap,
m_InputImage->GetNumberOfComponentsPerPixel(),
m_IntNoData, m_OutBvValue });
otbAppLogINFO("Output raster image will have " <<
(m_EncoderFilter->GetFunctor()).GetOutputSize() << " bands\n");
AddProcess(m_EncoderFilter, "Encode output raster image");
SetParameterOutputImage("out.raster.filename", m_EncoderFilter->GetOutput());
m_EncoderFilter->GetModifiableFunctor().m_CountMap = &m_CountMap;
m_EncoderFilter->GetModifiableFunctor().m_MeanMap = &m_MeanMap;
m_EncoderFilter->GetModifiableFunctor().m_StdMap = &m_StdMap;
m_EncoderFilter->GetModifiableFunctor().m_MinMap = &m_MinMap;
m_EncoderFilter->GetModifiableFunctor().m_MaxMap = &m_MaxMap;
m_EncoderFilter->GetModifiableFunctor().m_NbInputComponents = m_InputImage->GetNumberOfComponentsPerPixel();
m_EncoderFilter->GetModifiableFunctor().m_InNoData = m_IntNoData;
m_EncoderFilter->GetModifiableFunctor().m_OutBvValue = m_OutBvValue;
otbAppLogINFO("Output raster image will have " << (m_EncoderFilter->GetFunctor()).OutputSize() << " bands\n");
AddProcess(m_EncoderFilter, "Encode output raster image");
SetParameterOutputImage("out.raster.filename", m_EncoderFilter->GetOutput());
}
void WriteXMLStatsFile()
......
......@@ -49,6 +49,7 @@ otb_module(OTBAppClassification
OTBObjectList
OTBCommon
OTBSampling
OTBFunctor
TEST_DEPENDS
OTBTestKernel
......
......@@ -25,7 +25,7 @@
#include "otbImageListToVectorImageFilter.h"
#include "otbStreamingStatisticsVectorImageFilter.h"
#include "otbStreamingStatisticsImageFilter.h"
#include "otbUnaryFunctorImageFilter.h"
#include "otbFunctorImageFilter.h"
#include "itkStreamingImageFilter.h"
#include "otbInPlacePassFilter.h"
......@@ -46,25 +46,23 @@ namespace Wrapper
namespace Functor
{
template <class TInput, class TOutput>
class LuminanceOperator
{
typedef FloatVectorImageType::PixelType OutPixel;
typedef FloatVectorImageType::PixelType InPixel;
public:
LuminanceOperator() {}
unsigned int GetOutputSize()
LuminanceOperator() = default;
size_t OutputSize(const std::array<size_t, 1>&) const
{
return 1;
}
virtual ~LuminanceOperator() { }
virtual ~LuminanceOperator() = default;
OutPixel operator() ( InPixel input )
{
OutPixel out(1);
out[0] = m_LumCoef[0] * input[m_Rgb[0]] +
m_LumCoef[1] * input[m_Rgb[1]] +
m_LumCoef[2] * input[m_Rgb[2]] ;
return out;
TOutput operator()(TInput input)
{
TOutput out(1);
out[0] = m_LumCoef[0] * input[m_Rgb[0]] + m_LumCoef[1] * input[m_Rgb[1]] + m_LumCoef[2] * input[m_Rgb[2]];
return out;
} // end operator ()
......@@ -84,9 +82,9 @@ public:
private:
std::vector<unsigned int> m_Rgb;
std::vector<float> m_LumCoef;
}; // end of functor class MultiplyOperator
}; // end of functor class LuminanceOperator
} // end of functor
} // namespace functor
class ContrastEnhancement : public Application
{
......@@ -127,9 +125,7 @@ public:
typedef otb::StreamingStatisticsImageFilter < FloatImageType >
StatsFilterType;
typedef otb::UnaryFunctorImageFilter < FloatVectorImageType ,
FloatVectorImageType , Functor::LuminanceOperator >
LuminanceFunctorType;
typedef otb::FunctorImageFilter<Functor::LuminanceOperator<FloatVectorImageType::PixelType, FloatVectorImageType::PixelType>> LuminanceFunctorType;
typedef itk::StreamingImageFilter < LutType , LutType >
StreamingImageFilterType;
......@@ -705,8 +701,8 @@ private:
lumCoef[i] /= sum;
}
m_LuminanceFunctor = LuminanceFunctorType::New() ;
m_LuminanceFunctor->GetFunctor().SetRgb( rgb );
m_LuminanceFunctor->GetFunctor().SetLumCoef( lumCoef );
m_LuminanceFunctor->GetModifiableFunctor().SetRgb(rgb);
m_LuminanceFunctor->GetModifiableFunctor().SetLumCoef(lumCoef);
m_LuminanceFunctor->SetInput( inImage );
m_LuminanceFunctor->UpdateOutputInformation();
}
......
......@@ -29,6 +29,7 @@ otb_module(OTBAppFiltering
OTBContrast
OTBStatistics
OTBStreaming
OTBFunctor
TEST_DEPENDS
OTBTestKernel
......
......@@ -196,7 +196,7 @@ private:
UCLSUnmixingFilterType::New();
unmixer->SetInput(inputImage);
unmixer->SetMatrix(endMembersMatrix);
unmixer->GetModifiableFunctor().SetMatrix(endMembersMatrix);
unmixer->SetNumberOfThreads(1); // FIXME : currently buggy
abundanceMap = unmixer->GetOutput();
......@@ -212,7 +212,7 @@ private:
ISRAUnmixingFilterType::New();
unmixer->SetInput(inputImage);
unmixer->SetEndmembersMatrix(endMembersMatrix);
unmixer->GetModifiableFunctor().SetEndmembersMatrix(endMembersMatrix);
abundanceMap = unmixer->GetOutput();
m_ProcessObjects.push_back(unmixer.GetPointer());
......@@ -226,7 +226,7 @@ private:
NCLSUnmixingFilterType::New();
unmixer->SetInput(inputImage);
unmixer->SetEndmembersMatrix(endMembersMatrix);
unmixer->GetModifiableFunctor().SetEndmembersMatrix(endMembersMatrix);
abundanceMap = unmixer->GetOutput();
m_ProcessObjects.push_back(unmixer.GetPointer());
......
......@@ -39,7 +39,7 @@
#include "itkVariableLengthVector.h"
#include "itkImageRegionConstIterator.h"
#include "otbUnaryFunctorImageFilter.h"
#include "otbFunctorImageFilter.h"
#include "itkBinaryFunctorImageFilter.h"
#include "itkCastImageFilter.h"
......@@ -84,7 +84,7 @@ public:
typedef typename TOutput::ValueType ValueType;
VectorMapping() : m_OutputSize(0) {}
virtual ~VectorMapping() {}
virtual ~VectorMapping() = default;
typedef std::map<TInput, TOutput, VectorLexicographicCompare<TInput> > ChangeMapType;
......@@ -92,22 +92,12 @@ public:
{
m_OutputSize = nb;
}
unsigned int GetOutputSize()
size_t OutputSize(const std::array<size_t, 1>&) const
{
return m_OutputSize;
}
bool operator !=(const VectorMapping& other) const
{
if (m_ChangeMap != other.m_ChangeMap)
{
return true;
}
return false;
}
bool operator ==(const VectorMapping& other) const
{
return !(*this != other);
}
TOutput GetChange(const TInput& original)
{
return m_ChangeMap[original];
......@@ -234,10 +224,7 @@ public:
StreamingStatisticsMapFromLabelImageFilterType;
// Inverse mapper for color->label operation
typedef otb::UnaryFunctorImageFilter
<RGBImageType, LabelVectorImageType,
Functor::VectorMapping
<RGBPixelType, LabelVectorType> > ColorToLabelFilterType;
typedef otb::FunctorImageFilter<Functor::VectorMapping<RGBPixelType, LabelVectorType>> ColorToLabelFilterType;
// Streaming the input image for color->label operation
typedef RGBImageType::RegionType RegionType;
......@@ -685,10 +672,10 @@ private:
RGBImageType::Pointer input = GetParameterUInt8RGBImage("in");
m_InverseMapper = ColorToLabelFilterType::New();
m_InverseMapper->SetInput(input);
m_InverseMapper->GetFunctor().SetOutputSize(1);
m_InverseMapper->GetModifiableFunctor().SetOutputSize(1);
LabelVectorType notFoundValue(1);
notFoundValue[0] = GetParameterInt("op.colortolabel.notfound");
m_InverseMapper->GetFunctor().SetNotFoundValue(notFoundValue);
m_InverseMapper->GetModifiableFunctor().SetNotFoundValue(notFoundValue);
if(GetParameterInt("method")==0)
{
......@@ -710,7 +697,7 @@ private:
colorList.insert(background);
LabelVectorType currentVectorLabel(1);
currentVectorLabel[0] = currentLabel;
m_InverseMapper->GetFunctor().SetChange(background, currentVectorLabel);
m_InverseMapper->GetModifiableFunctor().SetChange(background, currentVectorLabel);
++currentLabel;
// Setting up local streaming capabilities
......@@ -747,7 +734,7 @@ private:
{
colorList.insert(it.Get());
currentVectorLabel[0] = currentLabel;
m_InverseMapper->GetFunctor().SetChange(it.Get(), currentVectorLabel);
m_InverseMapper->GetModifiableFunctor().SetChange(it.Get(), currentVectorLabel);
++currentLabel;
}
++it;
......@@ -824,7 +811,7 @@ private:
rgbcolor[0] = static_cast<int>(color[0]);
rgbcolor[1] = static_cast<int>(color[1]);
rgbcolor[2] = static_cast<int>(color[2]);
m_InverseMapper->GetFunctor().SetChange(rgbcolor, cvlabel);
m_InverseMapper->GetModifiableFunctor().SetChange(rgbcolor, cvlabel);
}
}
}
......
......@@ -26,7 +26,7 @@
#include "otbLabelMapToAttributeImageFilter.h"
#include "itkLabelImageToLabelMapFilter.h"
#include "otbUnaryFunctorImageFilter.h"
#include "otbFunctorImageFilter.h"
namespace otb
{
......@@ -43,10 +43,11 @@ public:
typedef std::vector<TOutput> ColorListType;
unsigned int GetOutputSize()
size_t OutputSize(const std::array<size_t, 1>&) const
{
return 3;
}
void AddColor(const TOutput& color)
{
m_ScoreColors.push_back(color);
......@@ -123,11 +124,7 @@ public:
typedef otb::HooverInstanceFilter<LabelMapType> InstanceFilterType;
typedef otb::LabelMapToAttributeImageFilter
<LabelMapType, FloatVectorImageType> AttributeImageFilterType;
typedef otb::UnaryFunctorImageFilter
<FloatVectorImageType,
Int16VectorImageType,
Functor::HooverColorMapping
<FloatPixelType, Int16PixelType> > HooverColorFilterType;
typedef otb::FunctorImageFilter<Functor::HooverColorMapping<FloatPixelType, Int16PixelType>> HooverColorFilterType;
private:
void DoInit() override
......@@ -259,31 +256,31 @@ private:
colorPixel[0] = 255;
colorPixel[1] = 255;
colorPixel[2] = 255;
m_GTColorFilter->GetFunctor().SetBackground(colorPixel);
m_MSColorFilter->GetFunctor().SetBackground(colorPixel);
m_GTColorFilter->GetModifiableFunctor().SetBackground(colorPixel);
m_MSColorFilter->GetModifiableFunctor().SetBackground(colorPixel);
// Correct detection : green
colorPixel[0] = 0;
colorPixel[1] = 255;
colorPixel[2] = 0;
m_GTColorFilter->GetFunctor().AddColor(colorPixel);
m_MSColorFilter->GetFunctor().AddColor(colorPixel);
m_GTColorFilter->GetModifiableFunctor().AddColor(colorPixel);
m_MSColorFilter->GetModifiableFunctor().AddColor(colorPixel);
// Over-segmentation : magenta
colorPixel[0] = 255;
colorPixel[1] = 0;
colorPixel[2] = 255;
m_GTColorFilter->GetFunctor().AddColor(colorPixel);
m_MSColorFilter->GetFunctor().AddColor(colorPixel);
m_GTColorFilter->GetModifiableFunctor().AddColor(colorPixel);
m_MSColorFilter->GetModifiableFunctor().AddColor(colorPixel);
// Under-segmentation : cyan
colorPixel[0] = 0;
colorPixel[1] = 255;
colorPixel[2] = 255;
m_GTColorFilter->GetFunctor().AddColor(colorPixel);
m_MSColorFilter->GetFunctor().AddColor(colorPixel);
m_GTColorFilter->GetModifiableFunctor().AddColor(colorPixel);
m_MSColorFilter->GetModifiableFunctor().AddColor(colorPixel);
// Missed detection (only for GT) : red
colorPixel[0] = 255;
colorPixel[1] = 0;
colorPixel[2] = 0;
m_GTColorFilter->GetFunctor().AddColor(colorPixel);
m_GTColorFilter->GetModifiableFunctor().AddColor(colorPixel);
if (HasValue("outgt"))
{
......
/*
* Copyright (C) 2005-2019 Centre National d'Etudes Spatiales (CNES)
*
* This file is part of Orfeo Toolbox
*
* https://www.orfeo-toolbox.org/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef otbDotProductImageFilter_hxx
#define otbDotProductImageFilter_hxx
#include "otbDotProductImageFilter.h"
namespace otb
{
/**
*
*/
template <class TInputImage, class TOutputImage>
DotProductImageFilter<TInputImage, TOutputImage>
::DotProductImageFilter()
{
}
template <class TInputImage, class TOutputImage>
void
DotProductImageFilter<TInputImage, TOutputImage>
::PrintSelf(std::ostream& os, itk::Indent indent) const
{
Superclass::PrintSelf(os, indent);
}
} // end namespace
#endif
/*
* Copyright (C) 2005-2019 Centre National d'Etudes Spatiales (CNES)
*
* This file is part of Orfeo Toolbox
*
* https://www.orfeo-toolbox.org/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef otbUnaryFunctorImageFilter_h
#define otbUnaryFunctorImageFilter_h
#include "itkUnaryFunctorImageFilter.h"
namespace otb
{
/**
* \class UnaryFunctorImageFilter
* \brief Implements pixel-wise generic operation on one image.
*
* Add the capability to change the number of channel when operation on
* VectorImage compared to the itk::UnaryFunctorImageFilter
*
* The number of channel is provided by the functor: TFunction::GetOutputSize. If
* this number is lower or equal to zero, the behavior of the itk::UnaryFunctorImageFilter
* remains unchanged.
*
* \sa itk::UnaryFunctorImageFilter
*
* \ingroup OTBCommon
*/
template <class TInputImage, class TOutputImage, class TFunction>
class ITK_EXPORT UnaryFunctorImageFilter : public itk::UnaryFunctorImageFilter<TInputImage, TOutputImage, TFunction>
{
public:
/** Standard class typedefs. */
typedef UnaryFunctorImageFilter Self;
typedef itk::UnaryFunctorImageFilter<TInputImage, TOutputImage, TFunction> Superclass;
typedef itk::SmartPointer<Self> Pointer;
typedef itk::SmartPointer<const Self> ConstPointer;
/** Method for creation through the object factory. */
itkNewMacro(Self);
/** Run-time type information (and related methods). */
itkTypeMacro(UnaryFunctorImageFilter, itk::UnaryFunctorImageFilter);
protected:
UnaryFunctorImageFilter() {};
~UnaryFunctorImageFilter() override {}
/** UnaryFunctorImageFilter can produce an image which has a different number of bands
* than its input image. As such, UnaryFunctorImageFilter
* needs to provide an implementation for
* GenerateOutputInformation() in order to inform the pipeline
* execution model. The original documentation of this method is
* below.
*
* \sa ProcessObject::GenerateOutputInformaton() */
void GenerateOutputInformation() override
{
Superclass::GenerateOutputInformation();
typename Superclass::OutputImagePointer outputPtr = this->GetOutput();
outputPtr->SetNumberOfComponentsPerPixel( // propagate vector length info
this->GetFunctor().GetOutputSize());
}
private:
UnaryFunctorImageFilter(const Self &) = delete;
void operator =(const Self&) = delete;
};
} // end namespace otb
#endif
......@@ -22,39 +22,29 @@
#define otbDotProductImageFilter_h
#include "itkMacro.h"
#include "otbUnaryFunctorImageFilter.h"
#include "otbFunctorImageFilter.h"
namespace otb
{
namespace Functor {
namespace Functor
{
/** \class DotProductFunctor
*
* \brief Computes the dot product against a specific vector
*
*
* \ingroup OTBCommon
* \ingroup OTBFunctor
*/
template<class TInput, class TOutput>
template <class TInput, class TOutput>
class DotProductFunctor
{
public:
typedef TInput InputType;
typedef TOutput OutputType;
DotProductFunctor() {}
virtual ~DotProductFunctor() {}
bool operator !=(const DotProductFunctor& itkNotUsed(other)) const
{
return false;
}
typedef TInput InputType;
typedef TOutput OutputType;
bool operator ==(const DotProductFunctor& other) const
{
return !(*this != other);
}
DotProductFunctor() = default;
virtual ~DotProductFunctor() = default;
const InputType& GetVector()
{
......@@ -66,93 +56,38 @@ public:
m_Vector = m;
}
OutputType operator ()(const InputType& in)
OutputType operator()(const InputType& in)
{
assert(in.Size() == m.Size());
OutputType result = 0;
for(unsigned int i = 0; i < in.Size(); ++i)
{
for (unsigned int i = 0; i < in.Size(); ++i)
{
result += in[i] * m_Vector[i];
}
}
return result;
}
private:
InputType m_Vector;
};
}
} // namespace Functor
/** \class DotProductImageFilter
/** \typedef DotProductImageFilter
*
* \brief Applies pixel-wise dot product to a VectorImage
*
* Given a vector, this filter outputs the dot product of each pixel of a multiband image
* with respect to the specified vector
*
* \sa otb::Functor::DotProductFunctor
*
* \ingroup Streamed
* \ingroup Threaded
*
* \ingroup OTBCommon
* \ingroup OTBFunctor
*/
template <class TInputImage, class TOutputImage>
class ITK_EXPORT DotProductImageFilter :
public itk::UnaryFunctorImageFilter<TInputImage, TOutputImage,
Functor::DotProductFunctor<typename TInputImage::PixelType,
typename TOutputImage::PixelType> >
{
public:
/** Standard class typedefs. */
typedef DotProductImageFilter Self;
typedef itk::UnaryFunctorImageFilter
<TInputImage,
TOutputImage,
Functor::DotProductFunctor<
typename TInputImage::PixelType,
typename TOutputImage::PixelType>
> Superclass;
typedef itk::SmartPointer<Self> Pointer;
typedef itk::SmartPointer<const Self> ConstPointer;
typedef Functor::DotProductFunctor<
typename TInputImage::PixelType,
typename TOutputImage::PixelType> FunctorType;
/** Method for creation through the object factory. */
itkNewMacro(Self);
/** Run-time type information (and related methods). */
itkTypeMacro(DotProductImageFilter, itk::UnaryFunctorImageFilter);
/** Pixel types. */
typedef typename TInputImage::PixelType InputPixelType;
typedef typename TOutputImage::PixelType OutputPixelType;
const InputPixelType& GetVector()
{
return this->GetFunctor().GetVector();
}
void SetVector(const InputPixelType& p)
{
this->GetFunctor().SetVector(p);
this->Modified();
}
protected:
DotProductImageFilter();
~DotProductImageFilter() override {}
void PrintSelf(std::ostream& os, itk::Indent indent) const override;
private:
DotProductImageFilter(const Self &) = delete;
void operator =(const Self&) = delete;
};
} // end namespace otb
#ifndef OTB_MANUAL_INSTANTIATION
#include "otbDotProductImageFilter.hxx"
#endif
template <typename TInputImage, typename TOutputImage>
using DotProductImageFilter = FunctorImageFilter<Functor::DotProductFunctor<typename TInputImage::PixelType, typename TOutputImage::PixelType>>;
} // namespace otb
#endif
......@@ -22,7 +22,7 @@
#define otbProjectiveProjectionImageFilter_h
#include "itkMacro.h"
#include "otbUnaryFunctorImageFilter.h"
#include "otbFunctorImageFilter.h"
namespace otb
{
......@@ -31,8 +31,9 @@ namespace Functor {
/** \class ProjectiveProjectionFunctor
*
* \brief TODO
* \brief Applies a projective projection to a pixel
*
* \sa ProjectiveProjectionImageFilter
*
* \ingroup OTBProjection
*/
......@@ -47,48 +48,13 @@ public:
ProjectiveProjectionFunctor() : m_OutputSize(0) {}
virtual ~ProjectiveProjectionFunctor() {}
unsigned int GetOutputSize()
{
return m_OutputSize;
}