Skip to content
Snippets Groups Projects
Commit 6d0d8e6d authored by Cédric Traizet's avatar Cédric Traizet
Browse files

ENH: removed LocalRxDetectorNonThreadFilter

parent 46c65f75
No related branches found
No related tags found
Loading
/*
* Copyright (C) 2005-2017 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 otbLocalRxDetectorNonThreadFilter_h
#define otbLocalRxDetectorNonThreadFilter_h
#include "itkImageToImageFilter.h"
#include "itkConstShapedNeighborhoodIterator.h"
#include "itkNeighborhoodAlgorithm.h"
#include "itkImageRegionIterator.h"
#include "itkListSample.h"
#include "itkCovarianceSampleFilter.h"
namespace otb
{
/** \class otbLocalRxDetectorNonThreadFilter
* \brief Local-RX detector algorithm with multichannel VectorImage data as input
*
*
* \ingroup ImageFilters
*
* \ingroup OTBAnomalyDetection
*/
template <class TInputImage, class TOutputImage>
class ITK_EXPORT LocalRxDetectorNonThreadFilter:
public itk::ImageToImageFilter<TInputImage, TOutputImage>
{
public:
/** Standard class typedefs. */
typedef LocalRxDetectorNonThreadFilter 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(LocalRxDetectorNonThreadFilter, 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;
void GenerateData() override;
// virtual void BeforeThreadedGenerateData();
// virtual void ThreadedGenerateData(const OutputImageRegionType& outputRegionForThread, itk::ThreadIdType threadId);
protected:
LocalRxDetectorNonThreadFilter();
~LocalRxDetectorNonThreadFilter() override {}
void PrintSelf(std::ostream& os, itk::Indent indent) const override;
private:
LocalRxDetectorNonThreadFilter(const Self&) = delete;
void operator=(const Self&) = delete;
int m_InternalRadius;
int m_ExternalRadius;
};
} // end namespace otb
#ifndef OTB_MANUAL_INSTANTIATION
#include "otbLocalRxDetectorNonThreadFilter.hxx"
#endif
#endif
/*
* Copyright (C) 2005-2017 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 otbLocalRxDetectorNonThreadFilter_hxx
#define otbLocalRxDetectorNonThreadFilter_hxx
#include "otbLocalRxDetectorNonThreadFilter.h"
namespace otb
{
/**
*
*/
template <class TInputImage, class TOutputImage>
LocalRxDetectorNonThreadFilter<TInputImage, TOutputImage>
::LocalRxDetectorNonThreadFilter()
{
this->m_ExternalRadius = 0;
this->m_InternalRadius = 0;
}
/**
*
*/
template <class TInputImage, class TOutputImage>
void
LocalRxDetectorNonThreadFilter<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
LocalRxDetectorNonThreadFilter<TInputImage, TOutputImage>
::GenerateData()
{
this->AllocateOutputs();
// Get the input and output pointers
InputPointerType inputPtr =
const_cast< InputImageType * >( this->GetInput());
OutputPointerType outputPtr = this->GetOutput();
inputPtr->Update();
// Fill the output buffer with black pixels
outputPtr->FillBuffer(0);
// 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, inputPtr->GetRequestedRegion(), 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, inputPtr->GetRequestedRegion(), 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
//std::cout << "pixel of shaped iteror : " << ci.Get() << std::endl;
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
LocalRxDetectorNonThreadFilter<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
#endif
......@@ -22,7 +22,6 @@ otb_module_test()
set(OTBAnomalyDetectionTests
otbAnomalyDetectionTestDriver.cxx
otbLocalRxDetectorRoiTest.cxx
otbLocalRxDetectorTest.cxx
)
......
......@@ -22,6 +22,5 @@
void RegisterTests()
{
REGISTER_TEST(LocalRXDetectorROITest);
REGISTER_TEST(LocalRXDetectorTest);
}
/*
* Copyright (C) 2005-2017 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.
*/
#include "otbImageFileReader.h"
#include "otbVectorImage.h"
#include "otbImageFileWriter.h"
#include "otbLocalRxDetectorNonThreadFilter.h"
#include "itkUnaryFunctorImageFilter.h"
#include "itkRescaleIntensityImageFilter.h"
#include "otbExtractROI.h"
int LocalRXDetectorROITest(int itkNotUsed(argc), char * argv[])
{
typedef double PixelType;
typedef otb::VectorImage<PixelType, 2> VectorImageType;
typedef otb::Image<PixelType, 2> ImageType;
typedef otb::LocalRxDetectorNonThreadFilter<VectorImageType, ImageType> LocalRxDetectorFilterType;
typedef otb::ImageFileReader<VectorImageType> ReaderType;
typedef unsigned char WritePixelType;
typedef otb::Image<WritePixelType> WriteImageType;
typedef otb::ImageFileWriter<ImageType> WriterType;
typedef itk::RescaleIntensityImageFilter<ImageType, WriteImageType> RescalerType;
typedef otb::ExtractROI<ImageType::PixelType, ImageType::PixelType> ExtractRoiType;
//--------------- 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]);
const unsigned int indexX = atoi(argv[5]);
const unsigned int indexY = atoi(argv[6]);
const unsigned int sizeX = atoi(argv[7]);
const unsigned int sizeY = atoi(argv[8]);
//--------------- Read hsi file -----------------//
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(filename);
reader->UpdateOutputInformation();
//------------- Anomaly detection ---------------//
LocalRxDetectorFilterType::Pointer rxDetector = LocalRxDetectorFilterType::New();
rxDetector->SetExternalRadius(externalRadius);
rxDetector->SetInternalRadius(internalRadius);
rxDetector->SetInput(reader->GetOutput());
//------------ Build extractRoiFilter -----------//
ExtractRoiType::Pointer roiExtractor = ExtractRoiType::New();
roiExtractor->SetStartX(indexX);
roiExtractor->SetStartY(indexY);
roiExtractor->SetSizeX(sizeX);
roiExtractor->SetSizeY(sizeY);
roiExtractor->SetInput(rxDetector->GetOutput());
//------------ rescaling and writing ------------//
RescalerType::Pointer rescaler = RescalerType::New();
rescaler->SetOutputMinimum(0);
rescaler->SetOutputMaximum(255);
rescaler->SetInput(roiExtractor->GetOutput());
WriterType::Pointer writer = WriterType::New();
writer->SetFileName(outputFilename);
writer->SetInput(roiExtractor->GetOutput());
//---------------- Launching pipeline ------------//
writer->Update();
return EXIT_SUCCESS;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment