Skip to content
Snippets Groups Projects
Commit 102b2cee authored by Otmane Lahlou's avatar Otmane Lahlou
Browse files

ENH : simplify the GenericRSResampleImageFilter

parent 40726fc4
No related branches found
No related tags found
No related merge requests found
......@@ -81,33 +81,45 @@ public:
/** Public Method prototypes */
virtual void GenerateData();
/** Accessors to internal filters parameters */
itkSetObjectMacro(Transform,GenericRSTransformType);
itkGetObjectMacro(Transform,GenericRSTransformType);
/** The Deformation field spacing & size */
itkSetMacro(DeformationFieldSpacing,SpacingType);
void SetDeformationFieldSpacing(const SpacingType & spacing)
{
m_Resampler->SetDeformationFieldSpacing(spacing);
this->Modified();
}
otbGetObjectMemberConstReferenceMacro(Resampler,DeformationFieldSpacing,SpacingType);
/** The resampled image parameters */
// Output Origin
itkSetMacro(OutputOrigin,OriginType);
itkGetMacro(OutputOrigin,OriginType);
void SetOutputOrigin(const OriginType & origin)
{
m_Resampler->SetOutputOrigin(origin);
this->Modified();
}
otbGetObjectMemberConstReferenceMacro(Resampler,OutputOrigin,OriginType);
// Output Start index
itkSetMacro(OutputIndex,IndexType);
itkGetMacro(OutputIndex,IndexType);
otbSetObjectMemberMacro(Resampler,OutputStartIndex,IndexType);
otbGetObjectMemberConstReferenceMacro(Resampler,OutputStartIndex,IndexType);
// Output Size
itkSetMacro(OutputSize,SizeType);
itkGetMacro(OutputSize,SizeType);
otbSetObjectMemberMacro(Resampler,OutputSize,SizeType);
otbGetObjectMemberConstReferenceMacro(Resampler,OutputSize,SizeType);
// Output Spacing
itkSetMacro(OutputSpacing,SpacingType);
itkGetMacro(OutputSpacing,SpacingType);
otbSetObjectMemberMacro(Resampler,OutputSpacing,SpacingType);
otbGetObjectMemberConstReferenceMacro(Resampler,OutputSpacing,SpacingType);
/** Methods to Set/Get the interpolator */
itkSetObjectMacro( Interpolator, InterpolatorType );
itkGetObjectMacro( Interpolator, InterpolatorType );
/** Methods to Set/Get the interpolator */
void SetInterpolator(InterpolatorType * interpolator)
{
m_Resampler->SetInterpolator(interpolator);
this->Modified();
}
otbGetObjectMemberConstMacro(Resampler, Interpolator, const InterpolatorType *);
/** Set/Get for input and output projections. */
itkSetStringMacro(InputProjectionRef);
itkGetStringMacro(InputProjectionRef);
......@@ -115,31 +127,30 @@ public:
itkSetStringMacro(OutputProjectionRef);
itkGetStringMacro(OutputProjectionRef);
/** Set/Get Keywordlist*/
itkGetMacro(InputKeywordList, ImageKeywordlist);
/** Set/Get Input Keywordlist*/
void SetInputKeywordList(const ImageKeywordlist& kwl)
{
this->m_InputKeywordList = kwl;
m_Transform->SetOutputKeywordList(kwl);
this->Modified();
}
itkGetMacro(OutputKeywordList, ImageKeywordlist);
otbGetObjectMemberConstMacro(Transform,InputKeywordList,ImageKeywordlist);
/** Set/Get output Keywordlist*/
void SetOutputKeywordList(const ImageKeywordlist& kwl)
{
this->m_OutputKeywordList = kwl;
m_Transform->SetInputKeywordList(kwl);
this->Modified();
}
otbGetObjectMemberConstMacro(Transform,OutputKeywordList,ImageKeywordlist);
itkSetStringMacro(DEMDirectory);
itkGetStringMacro(DEMDirectory);
itkSetMacro(AverageElevation, double);
itkGetMacro(AverageElevation, double);
virtual void GenerateOutputInformation();
/** Set/Get the DEMDirectory*/
void SetDEMDirectory(const std::string& dem)
{
m_Transform->SetDEMDirectory(dem);
this->Modified();
}
otbGetObjectMemberConstMacro(Transform,DEMDirectory,std::string);
virtual void GenerateInputRequestedRegion();
void SetOutputParametersFromImage(const InputImageType * image);
protected:
......@@ -147,8 +158,11 @@ protected:
/** Destructor */
virtual ~GenericRSResampleImageFilter() {};
// called in the GenerateInputRequestedRegion cause the transform
// is needed there and have to be set once.
virtual void GenerateOutputInformation();
virtual void GenerateInputRequestedRegion();
// Method to instanciate the Generic RS transform
void UpdateTransform();
private:
......@@ -156,28 +170,12 @@ private:
void operator =(const Self&); //purposely not implemented
// GenericRSTransform Parameters
ImageKeywordlist m_InputKeywordList;
ImageKeywordlist m_OutputKeywordList;
std::string m_InputProjectionRef;
std::string m_OutputProjectionRef;
std::string m_DEMDirectory;
double m_AverageElevation;
unsigned int m_InterpolatorNeighborhoodRadius;
unsigned int m_AddedRadius ;
// Filters pointers
ResamplerPointerType m_Resampler;
GenericRSTransformPointerType m_Transform;
// Output parameters
SpacingType m_DeformationFieldSpacing;
SizeType m_OutputSize;
OriginType m_OutputOrigin;
IndexType m_OutputIndex;
SpacingType m_OutputSpacing;
// smartPointer on the interpolator
typename InterpolatorType::Pointer m_Interpolator;
};
} // namespace otb
......
......@@ -30,24 +30,17 @@ namespace otb
template <class TInputImage, class TOutputImage, class TDeformationField>
GenericRSResampleImageFilter<TInputImage, TOutputImage, TDeformationField>
::GenericRSResampleImageFilter() :m_Interpolator(),m_DeformationFieldSpacing(2),
m_InterpolatorNeighborhoodRadius(1),m_AddedRadius(2)
{
// default values for Resampled Image
m_OutputSize.Fill(1);
m_OutputOrigin.Fill(0);
m_OutputIndex.Fill(0);
m_OutputSpacing.Fill(1);
::GenericRSResampleImageFilter()
{
// internal filters instanciation
m_Resampler = ResamplerType::New();
// default identity transform
m_Transform = GenericRSTransformType::New();
// Setup default interpolator
typename DefaultInterpolatorType::Pointer interp = DefaultInterpolatorType::New();
m_Interpolator = static_cast<InterpolatorType*>( interp.GetPointer() );
// Initialize the deformation field spacing
SpacingType initSpacing;
initSpacing[0]= 2.;
initSpacing[1]=-2.;
this->SetDeformationFieldSpacing(initSpacing);
}
......@@ -57,29 +50,6 @@ void
GenericRSResampleImageFilter<TInputImage, TOutputImage, TDeformationField>
::GenerateData()
{
// Get the input image
typename InputImageType::Pointer input = const_cast<InputImageType *>(this->GetInput());
this->UpdateTransform();
// Remote Sensing images have y axe flipped
m_DeformationFieldSpacing[1] = -m_DeformationFieldSpacing[1];
// Use the RSTransform with the OptResampleImageFilter
m_Resampler->SetInput(input);
m_Resampler->SetTransform(m_Transform);
m_Resampler->SetDeformationFieldSpacing(m_DeformationFieldSpacing);
// resampler output parameters
m_Resampler->SetOutputSize(m_OutputSize);
m_Resampler->SetOutputSpacing(m_OutputSpacing);
m_Resampler->SetOutputStartIndex(m_OutputIndex);
m_Resampler->SetOutputOrigin(m_OutputOrigin);
// expose the metadata to the output of the resample filter
itk::MetaDataDictionary& dict = m_Resampler->GetOutput()->GetMetaDataDictionary();
itk::EncapsulateMetaData<std::string>(dict, otb::MetaDataKey::ProjectionRefKey,m_OutputProjectionRef);
m_Resampler->GraftOutput(this->GetOutput());
m_Resampler->Update();
this->GraftOutput(m_Resampler->GetOutput());
......@@ -100,12 +70,12 @@ GenericRSResampleImageFilter<TInputImage, TOutputImage, TDeformationField>
typename OutputImageType::Pointer outputPtr = this->GetOutput();
outputPtr->SetSpacing( m_OutputSpacing );
outputPtr->SetOrigin( m_OutputOrigin );
outputPtr->SetSpacing( this->GetOutputSpacing() );
outputPtr->SetOrigin( this->GetOutputOrigin() );
typename OutputImageType::RegionType region;
region.SetSize(m_OutputSize);
region.SetIndex(m_OutputIndex);
region.SetSize(this->GetOutputSize());
region.SetIndex(this->GetOutputStartIndex() );
outputPtr->SetLargestPossibleRegion(region);
......@@ -121,13 +91,13 @@ GenericRSResampleImageFilter<TInputImage, TOutputImage, TDeformationField>
::UpdateTransform()
{
// Get the input image
typename InputImageType::Pointer input = const_cast<InputImageType *>(this->GetInput());
const InputImageType* input = this->GetInput();
// Instanciate the RS Transform
m_Transform->SetOutputProjectionRef(input->GetProjectionRef());
m_Transform->SetOutputKeywordList(input->GetImageKeywordlist());
m_Transform->SetInputProjectionRef(m_OutputProjectionRef);
m_Transform->SetInputKeywordList(m_OutputKeywordList);
m_Transform->SetOutputKeywordList(input->GetImageKeywordlist());
m_Transform->SetInputKeywordList(this->GetOutputKeywordList());
m_Transform->InstanciateTransform();
}
......@@ -136,107 +106,25 @@ void
GenericRSResampleImageFilter<TInputImage, TOutputImage, TDeformationField>
::GenerateInputRequestedRegion()
{
// Instanciate the transform
this->UpdateTransform();
// call the superclass's implementation
Superclass::GenerateInputRequestedRegion();
// Retrieve output pointer
OutputImageType * outputPtr = this->GetOutput();
// Retrieve input pointer
const InputImageType * inputPtr = this->GetInput();
// Load I/O
typename InputImageType::Pointer inputImage = const_cast<InputImageType*>(this->GetInput());
typename OutputImageType::Pointer outputImage = this->GetOutput();
// Retrieve output requested region
RegionType requestedRegion = outputPtr->GetRequestedRegion();
IndexType index = outputImage->GetRequestedRegion().GetIndex();
SizeType size = outputImage->GetRequestedRegion().GetSize();
// Instanciate the RS transform
this->UpdateTransform();
// Obtain coordinates of upperleft, upperright, lowerleft and lowerright points in the image
IndexType indexTmp;
std::vector<IndexType> vPoints;
typename std::vector<IndexType>::iterator it;
indexTmp[0] = index[0];
indexTmp[1] = index[1];
vPoints.push_back(indexTmp);
indexTmp[0] = index[0] + size[0];
indexTmp[1] = index[1];
vPoints.push_back(indexTmp);
indexTmp[0] = index[0] + size[0];
indexTmp[1] = index[1] + size[1];
vPoints.push_back(indexTmp);
indexTmp[0] = index[0];
indexTmp[1] = index[1] + size[1];
vPoints.push_back(indexTmp);
typedef double CoordRepType; // to maintain compatibility with ITK
typedef itk::ContinuousIndex<CoordRepType, 2> ContinuousIndexType;
typename ContinuousIndexType::ValueType minX = itk::NumericTraits<typename ContinuousIndexType::ValueType>::max();
typename ContinuousIndexType::ValueType maxX = 0;
typename ContinuousIndexType::ValueType minY = itk::NumericTraits<typename ContinuousIndexType::ValueType>::max();
typename ContinuousIndexType::ValueType maxY = 0;
// Coordinates of current output pixel
PointType outputPoint;
PointType inputPoint;
// Transform each "corner" point
for (it = vPoints.begin(); it != vPoints.end(); ++it)
{
// Build the output point location
// Not good because the matrice indexToPhysicalPoint is identity :
// not updated.....
outputImage->TransformIndexToPhysicalPoint(*it, outputPoint);
// Compute corresponding input pixel continuous index
ContinuousIndexType indexTmpTr;
inputPoint = m_Transform->TransformPoint(outputPoint);
inputImage->TransformPhysicalPointToContinuousIndex(inputPoint,indexTmpTr);
//std::cout <<"########: *it" << *it <<" outputPoint " << outputPoint << " -->inputPoint " <<inputPoint <<" --> physitoIndex "<< indexTmpTr<< std::endl;
if (indexTmpTr[0] > maxX) maxX = indexTmpTr[0];
if (indexTmpTr[0] < minX) minX = indexTmpTr[0];
if (indexTmpTr[1] > maxY) maxY = indexTmpTr[1];
if (indexTmpTr[1] < minY) minY = indexTmpTr[1];
}
// Create region needed in previous filter in the pipeline, which is the bounding box of previous transformed points
typename InputImageType::RegionType region;
index[0] = static_cast<long int>(minX);
index[1] = static_cast<long int>(minY);
size[0] = static_cast<long unsigned int>(maxX - minX);
size[1] = static_cast<long unsigned int>(maxY - minY);
region.SetSize(size);
region.SetIndex(index);
// Grow region to be sure that interpolator can found needed point on image borders
unsigned int neededRadius =
StreamingTraits<typename Superclass::InputImageType>::CalculateNeededRadiusForInterpolator(this->GetInterpolator());
if (neededRadius == 0)
{
neededRadius = m_InterpolatorNeighborhoodRadius;
}
region.PadByRadius(neededRadius + m_AddedRadius);
// If requested region is not contained in input image, then result region is null
if (!region.Crop(inputImage->GetLargestPossibleRegion()))
{
index[0] = 0;
index[1] = 0;
size[0] = 0;
size[1] = 0;
region.SetIndex(index);
region.SetSize(size);
}
inputImage->SetRequestedRegion(region);
// Generate input requested region
m_Resampler->SetInput(inputPtr);
m_Resampler->SetTransform(m_Transform);
m_Resampler->SetDeformationFieldSpacing(this->GetDeformationFieldSpacing());
m_Resampler->GetOutput()->UpdateOutputInformation();
m_Resampler->GetOutput()->SetRequestedRegion(requestedRegion);
m_Resampler->GetOutput()->PropagateRequestedRegion();
}
/**
......@@ -250,7 +138,7 @@ GenericRSResampleImageFilter<TInputImage, TOutputImage, TDeformationField>
{
this->SetOutputOrigin ( image->GetOrigin() );
this->SetOutputSpacing ( image->GetSpacing() );
this->SetOutputIndex ( image->GetLargestPossibleRegion().GetIndex() );
this->SetOutputStartIndex ( image->GetLargestPossibleRegion().GetIndex() );
this->SetOutputSize ( image->GetLargestPossibleRegion().GetSize() );
}
......
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