Commit 06561db9 authored by Cédric Traizet's avatar Cédric Traizet

ENH: remove the old localRxDetectionFilter

parent a785fda6
......@@ -47,9 +47,6 @@ public:
typedef DoubleVectorImageType VectorImageType;
typedef DoubleImageType ImageType;
/** Filter typedefs */
typedef otb::LocalRxDetectorFilter<VectorImageType, ImageType> LocalRxDetectorFilterType; //TODO remove this
private:
void DoInit() override
{
......@@ -108,30 +105,19 @@ private:
auto inputImage = GetParameterDoubleVectorImage("in");
inputImage->UpdateOutputInformation();
// The localRxDetectionFilter can be replaced by a functorImageFilter using the appropriate
// functor. However using functorImageFilter with neighborhood is buggy (see issue #1802). Still,
// the functor has been implemented and localRxDetectionFilter will be deprecated when the
// bug is corrected.
#if 0 // Using localRxDetectionFilter
auto localRxDetectionFilter = LocalRxDetectorFilterType::New();
localRxDetectionFilter->SetInput(inputImage);
Functor::LocalRxDetectionFunctor<double> detectorFunctor;
unsigned int externalRadius = GetParameterInt("er");
unsigned int internalRadius = GetParameterInt("ir");
localRxDetectionFilter->SetInternalRadius(internalRadius);
localRxDetectionFilter->SetExternalRadius(externalRadius);
#else // Using a functorImageFilter
Functor::LocalRxDetectionFunctor<double> detectorFunctor;
detectorFunctor.SetInternalRadius(GetParameterInt("ir"), GetParameterInt("ir"));
detectorFunctor.SetInternalRadius(internalRadius, internalRadius);
// Create a functorImageFilter with the localRx functor and the appropriate
// external radius.
auto localRxDetectionFilter = otb::NewFunctorFilter
(detectorFunctor ,{{GetParameterInt("er"),GetParameterInt("er")}});
(detectorFunctor ,{{externalRadius,externalRadius}});
localRxDetectionFilter->SetInputs(inputImage);
#endif
SetParameterOutputImage("out", localRxDetectionFilter->GetOutput());
RegisterPipeline();
......
......@@ -24,96 +24,14 @@
#include "itkImageToImageFilter.h"
#include "itkConstShapedNeighborhoodIterator.h"
#include "itkNeighborhoodAlgorithm.h"
#include "itkImageRegionIterator.h"
#include "itkListSample.h"
#include "itkCovarianceSampleFilter.h"
#include "itkProgressReporter.h"
#include "itkConstNeighborhoodIterator.h"
#include "otbVectorImage.h"
namespace otb
{
/** \class otbLocalRxDetectorFilter
* \brief Local-RX detector algorithm with multichannel VectorImage data as input
*
*
* \ingroup ImageFilters
*
* \ingroup OTBAnomalyDetection
*/
template <class TInputImage, class TOutputImage>
class ITK_EXPORT LocalRxDetectorFilter:
public itk::ImageToImageFilter<TInputImage, TOutputImage>
{
public:
/** Standard class typedefs. */
typedef LocalRxDetectorFilter Self;
typedef itk::ImageToImageFilter< TInputImage, TOutputImage > Superclass;
typedef itk::SmartPointer<Self> Pointer;
typedef itk::SmartPointer<const Self> ConstPointer;
/** Type macro */
itkNewMacro(Self);
/** Creation through object factory macro */
itkTypeMacro(LocalRxDetectorFilter, ImageToImageFilter);
/** typedef related to input and output images */
typedef TInputImage InputImageType;
typedef typename InputImageType::Pointer InputPointerType;
typedef typename InputImageType::ConstPointer InputConstPointerType;
typedef typename InputImageType::IndexType InputIndexType;
typedef typename InputImageType::SizeType InputSizeType;
typedef TOutputImage OutputImageType;
typedef typename OutputImageType::Pointer OutputPointerType;
typedef typename OutputImageType::IndexType OutputIndexType;
typedef typename OutputImageType::OffsetType OutputOffsetType;
typedef typename OutputImageType::SizeType OutputSizeType;
typedef typename OutputImageType::RegionType OutputImageRegionType;
/** typedef related to iterators */
typedef itk::ConstShapedNeighborhoodIterator<InputImageType> ConstShapedNeighborhoodIteratorType;
typedef itk::NeighborhoodAlgorithm::ImageBoundaryFacesCalculator<InputImageType> VectorFaceCalculatorType;
typedef itk::NeighborhoodAlgorithm::ImageBoundaryFacesCalculator<OutputImageType> FaceCalculatorType;
typedef itk::ImageRegionIterator<OutputImageType> ImageRegionIteratorType;
/** typedef related to statistics */
typedef typename InputImageType::PixelType VectorMeasurementType;
typedef itk::Statistics::ListSample<VectorMeasurementType> ListSampleType;
typedef itk::Statistics::CovarianceSampleFilter<ListSampleType> CovarianceCalculatorType;
typedef typename CovarianceCalculatorType::MeasurementVectorRealType MeasurementVectorRealType;
typedef typename CovarianceCalculatorType::MatrixType MatrixType;
/** Getter and Setter */
itkSetMacro(InternalRadius, int);
itkGetMacro(InternalRadius, int);
itkSetMacro(ExternalRadius, int);
itkGetMacro(ExternalRadius, int);
/** Main computation method */
void GenerateInputRequestedRegion() override;
// virtual void GenerateData();
void BeforeThreadedGenerateData() override;
void ThreadedGenerateData(const OutputImageRegionType& outputRegionForThread, itk::ThreadIdType threadId) override;
protected:
LocalRxDetectorFilter();
~LocalRxDetectorFilter() override {}
void PrintSelf(std::ostream& os, itk::Indent indent) const override;
private:
LocalRxDetectorFilter(const Self&) = delete;
void operator=(const Self&) = delete;
int m_InternalRadius;
int m_ExternalRadius;
};
/** \class LocalRxDetectionFunctor
* \brief This functor computes a local Rx score on an input neighborhood. Pixel of the neighborhood
* inside the internal radius are not considered during the computation of local statistics.
......@@ -224,12 +142,8 @@ public:
}
};
} // end namespace functor
} // end namespace functor
} // end namespace otb
#ifndef OTB_MANUAL_INSTANTIATION
#include "otbLocalRxDetectorFilter.hxx"
#endif
#endif
......@@ -27,206 +27,6 @@
namespace otb
{
/**
*
*/
template <class TInputImage, class TOutputImage>
LocalRxDetectorFilter<TInputImage, TOutputImage>
::LocalRxDetectorFilter()
: m_InternalRadius(1), m_ExternalRadius(2)
{
}
/**
*
*/
template <class TInputImage, class TOutputImage>
void
LocalRxDetectorFilter<TInputImage, TOutputImage>
::PrintSelf(std::ostream& os, itk::Indent indent) const
{
Superclass::PrintSelf(os, indent);
os << indent << "Internal Radius: " << m_InternalRadius << std::endl;
os << indent << "External Radius: " << m_ExternalRadius << std::endl;
}
/**
*
*/
template <class TInputImage, class TOutputImage>
void
LocalRxDetectorFilter<TInputImage, TOutputImage>
::BeforeThreadedGenerateData()
{
// Get the input and output pointers
OutputPointerType outputPtr = this->GetOutput();
// Fill the buffer with black pixels
outputPtr->FillBuffer(0);
}
/**
*
*/
template <class TInputImage, class TOutputImage>
void
LocalRxDetectorFilter<TInputImage, TOutputImage>
::ThreadedGenerateData(const OutputImageRegionType& outputRegionForThread,
itk::ThreadIdType threadId)
{
// Get the input and output pointers
InputConstPointerType inputPtr = this->GetInput();
OutputPointerType outputPtr = this->GetOutput();
// Support progress methods/callbacks
itk::ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
// Compute input region for thread
typename TInputImage::RegionType inputRegionForThread;
inputRegionForThread = outputRegionForThread;
inputRegionForThread.PadByRadius(m_ExternalRadius);
inputRegionForThread.Crop(inputPtr->GetLargestPossibleRegion());
// Iterator on input region
typename ConstShapedNeighborhoodIteratorType::RadiusType radius;
radius.Fill(m_ExternalRadius);
VectorFaceCalculatorType vectorFaceCalculator;
typename VectorFaceCalculatorType::FaceListType vectorFaceList;
typename VectorFaceCalculatorType::FaceListType::iterator vectorFit;
vectorFaceList = vectorFaceCalculator(inputPtr, inputRegionForThread, radius);
vectorFit = vectorFaceList.begin(); // Only the first face is used
ConstShapedNeighborhoodIteratorType inputIt(radius, inputPtr, *vectorFit);
// Neighborhood Configuration
typename ConstShapedNeighborhoodIteratorType::OffsetType off;
for (int y = -m_ExternalRadius; y <= m_ExternalRadius; y++)
{
off[1] = y;
for (int x = -m_ExternalRadius; x <= m_ExternalRadius; x++)
{
off[0] = x;
if ((abs(x) > m_InternalRadius) || (abs(y) > m_InternalRadius))
{
inputIt.ActivateOffset(off);
}
}
}
// iterator on output region
FaceCalculatorType faceCalculator;
typename FaceCalculatorType::FaceListType faceList;
typename FaceCalculatorType::FaceListType::iterator fit;
faceList = faceCalculator(outputPtr, inputRegionForThread, radius);
fit = faceList.begin(); // Only the first face is used
ImageRegionIteratorType outputIt(outputPtr, *fit);
// Run Input Image
for (inputIt.GoToBegin(), outputIt.GoToBegin(); !inputIt.IsAtEnd(); ++inputIt, ++outputIt)
{
// Create ListSample
typename ListSampleType::Pointer listSample = ListSampleType::New();
listSample->SetMeasurementVectorSize(inputPtr->GetNumberOfComponentsPerPixel());
// Run neighborhood
typename ConstShapedNeighborhoodIteratorType::ConstIterator ci;
for (ci = inputIt.Begin(); !ci.IsAtEnd(); ++ci)
{
// Pushback element in listSample
listSample->PushBack(ci.Get());
}
// Compute mean & covariance matrix
typename CovarianceCalculatorType::Pointer covarianceCalculator = CovarianceCalculatorType::New();
covarianceCalculator->SetInput(listSample);
covarianceCalculator->Update();
MeasurementVectorRealType meanVector = covarianceCalculator->GetMean();
MatrixType covarianceMatrix = covarianceCalculator->GetCovarianceMatrix();
// Compute RX value
typename MatrixType::InternalMatrixType invCovMat = covarianceMatrix.GetInverse();
VectorMeasurementType testPixVec;
testPixVec = inputPtr->GetPixel(inputIt.GetIndex());
VectorMeasurementType meanVec(meanVector.GetNumberOfElements());
for(unsigned int i = 0; i < meanVector.GetNumberOfElements(); ++i)
{
meanVec.SetElement(i, meanVector.GetElement(i));
}
typename MatrixType::InternalMatrixType centeredTestPixMat(meanVector.GetNumberOfElements(), 1);
for (unsigned int i = 0; i < centeredTestPixMat.rows(); ++i)
{
centeredTestPixMat.put(i, 0, (testPixVec.GetElement(i) - meanVector.GetElement(i)));
}
typename MatrixType::InternalMatrixType rxValue = centeredTestPixMat.transpose() * invCovMat * centeredTestPixMat;
outputIt.Set(rxValue.get(0, 0));
}
}
/**
*
*/
template <class TInputImage, class TOutputImage>
void
LocalRxDetectorFilter<TInputImage, TOutputImage>
::GenerateInputRequestedRegion()
{
// call the superclass' implementation of this method
Superclass::GenerateInputRequestedRegion();
// get pointers to the input and output
InputPointerType inputPtr =
const_cast< InputImageType * >( this->GetInput());
OutputPointerType outputPtr = this->GetOutput();
if ( !inputPtr || !outputPtr )
{
return;
}
// get a copy of the input requested region (should equal the output
// requested region)
typename TInputImage::RegionType inputRequestedRegion;
inputRequestedRegion = inputPtr->GetRequestedRegion();
// pad the input requested region by the operator radius
inputRequestedRegion.PadByRadius( m_ExternalRadius );
// crop the input requested region at the input's largest possible region
if ( inputRequestedRegion.Crop(inputPtr->GetLargestPossibleRegion()) )
{
inputPtr->SetRequestedRegion( inputRequestedRegion );
return;
}
else
{
// Couldn't crop the region (requested region is outside the largest
// possible region). Throw an exception.
// store what we tried to request (prior to trying to crop)
inputPtr->SetRequestedRegion( inputRequestedRegion );
// build an exception
itk::InvalidRequestedRegionError e(__FILE__, __LINE__);
e.SetLocation(ITK_LOCATION);
e.SetDescription("Requested region is (at least partially) outside the largest possible region.");
e.SetDataObject(inputPtr);
throw e;
}
}
} // end namespace otb
......
......@@ -19,56 +19,58 @@
*/
#include "otbImageFileReader.h"
#include "otbImage.h"
#include "otbVectorImage.h"
#include "otbImageFileWriter.h"
#include "otbLocalRxDetectorFilter.h"
#include "itkUnaryFunctorImageFilter.h"
#include "itkRescaleIntensityImageFilter.h"
#include "otbFunctorImageFilter.h"
int LocalRXDetectorTest(int itkNotUsed(argc), char * argv[])
{
typedef double PixelType;
typedef otb::VectorImage<PixelType, 2> VectorImageType;
typedef otb::Image<PixelType, 2> ImageType;
typedef otb::LocalRxDetectorFilter<VectorImageType, ImageType> LocalRxDetectorFilterType;
typedef double PixelType;
typedef otb::VectorImage<PixelType, 2> VectorImageType;
typedef otb::Image<PixelType, 2> ImageType;
typedef otb::Functor::LocalRxDetectionFunctor<PixelType> LocalRxDetectorFunctorType;
typedef otb::ImageFileReader<VectorImageType> ReaderType;
typedef otb::ImageFileReader<VectorImageType> ReaderType;
typedef unsigned char WritePixelType;
typedef otb::Image<WritePixelType> WriteImageType;
typedef otb::ImageFileWriter<WriteImageType> WriterType;
typedef itk::RescaleIntensityImageFilter<ImageType, WriteImageType> RescalerType;
typedef unsigned char WritePixelType;
typedef otb::Image<WritePixelType> WriteImageType;
typedef otb::ImageFileWriter<WriteImageType> WriterType;
typedef itk::RescaleIntensityImageFilter<ImageType, WriteImageType> RescalerType;
/////////// PARAMETERS ///////////
/////////// PARAMETERS ///////////
const char * filename = argv[1];
const char * outputFilename = argv[2];
const unsigned int externalRadius = atoi(argv[3]);
const unsigned int internalRadius = atoi(argv[4]);
//////// RX Filtering /////////
const char * filename = argv[1];
const char * outputFilename = argv[2];
const int externalRadius = atoi(argv[3]);
const int internalRadius = atoi(argv[4]);
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(filename);
//////// RX Filtering /////////
LocalRxDetectorFunctorType detectorFunctor;
detectorFunctor.SetInternalRadius(internalRadius, internalRadius);
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(filename);
auto rxDetector = otb::NewFunctorFilter
(detectorFunctor ,{{externalRadius,externalRadius}});
LocalRxDetectorFilterType::Pointer rxDetector = LocalRxDetectorFilterType::New();
rxDetector->SetExternalRadius(externalRadius);
rxDetector->SetInternalRadius(internalRadius);
rxDetector->SetInput(reader->GetOutput());
rxDetector->SetInputs(reader->GetOutput());
RescalerType::Pointer rescaler = RescalerType::New();
rescaler->SetOutputMinimum(0);
rescaler->SetOutputMaximum(255);
rescaler->SetInput(rxDetector->GetOutput());
//////// Rescaling /////////
RescalerType::Pointer rescaler = RescalerType::New();
rescaler->SetOutputMinimum(0);
rescaler->SetOutputMaximum(255);
rescaler->SetInput(rxDetector->GetOutput());
WriterType::Pointer writer = WriterType::New();
writer->SetFileName(outputFilename);
writer->SetInput(rescaler->GetOutput());
writer->Update();
WriterType::Pointer writer = WriterType::New();
writer->SetFileName(outputFilename);
writer->SetInput(rescaler->GetOutput());
writer->Update();
return EXIT_SUCCESS;
return EXIT_SUCCESS;
}
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