diff --git a/Code/ChangeDetection/otbBinaryFunctorNeighborhoodJoinHistogramImageFilter.h b/Code/ChangeDetection/otbBinaryFunctorNeighborhoodJoinHistogramImageFilter.h index 8a987c76ac91e4094f50b04e0cc79f8969ed9d74..23c0bdd8e826e3d3bc8660a8ff47c24d9c9b0f43 100644 --- a/Code/ChangeDetection/otbBinaryFunctorNeighborhoodJoinHistogramImageFilter.h +++ b/Code/ChangeDetection/otbBinaryFunctorNeighborhoodJoinHistogramImageFilter.h @@ -72,6 +72,24 @@ public: typedef itk::ProcessObject ProcessObjectType; + typedef itk::ConstNeighborhoodIterator<TInputImage1> + NeighborhoodIteratorType1; + typedef itk::ConstNeighborhoodIterator<TInputImage2> + NeighborhoodIteratorType2; + + typedef typename NeighborhoodIteratorType1::RadiusType RadiusType1; + typedef typename NeighborhoodIteratorType2::RadiusType RadiusType2; + + typedef unsigned char RadiusSizeType; + + /** Typedefs for histogram. This should have been defined as + Histogram<RealType, 2> but a bug in VC++7 produced an internal compiler + error with such declaration. */ + typedef typename itk::Statistics::Histogram<double, + itk::Statistics::DenseFrequencyContainer2 > HistogramType; + typedef typename HistogramType::MeasurementVectorType MeasurementVectorType; + typedef typename HistogramType::SizeType HistogramSizeType; + /** Connect one of the operands for pixel-wise addition */ void SetInput1(const TInputImage1 * image1); @@ -103,26 +121,8 @@ public: this->Modified(); } - typedef itk::ConstNeighborhoodIterator<TInputImage1> - NeighborhoodIteratorType1; - typedef itk::ConstNeighborhoodIterator<TInputImage2> - NeighborhoodIteratorType2; - - typedef typename NeighborhoodIteratorType1::RadiusType RadiusType1; - typedef typename NeighborhoodIteratorType2::RadiusType RadiusType2; - - typedef unsigned char RadiusSizeType; - itkSetMacro(Radius, RadiusSizeType); - /** Typedefs for histogram. This should have been defined as - Histogram<RealType, 2> but a bug in VC++7 produced an internal compiler - error with such declaration. */ - typedef typename itk::Statistics::Histogram<double, - itk::Statistics::DenseFrequencyContainer2 > HistogramType; - typedef typename HistogramType::MeasurementVectorType MeasurementVectorType; - typedef typename HistogramType::SizeType HistogramSizeType; - /** Sets the histogram size. Note this function must be called before \c Initialize(). */ itkSetMacro(HistogramSize, HistogramSizeType); @@ -151,6 +151,8 @@ protected: BinaryFunctorNeighborhoodJoinHistogramImageFilter(); virtual ~BinaryFunctorNeighborhoodJoinHistogramImageFilter() {} + virtual void BeforeThreadedGenerateData(); + /** BinaryFunctorNeighborhoodJoinHistogramImageFilter can be implemented as a multithreaded filter. * Therefore, this implementation provides a ThreadedGenerateData() routine * which is called for each processing thread. The output image data is @@ -173,19 +175,12 @@ protected: HistogramType::Pointer m_Histogram; - /** The histogram size. */ - HistogramSizeType m_HistogramSize; - /** The lower bound for samples in the histogram. */ - mutable MeasurementVectorType m_LowerBound; - /** The upper bound for samples in the histogram. */ - mutable MeasurementVectorType m_UpperBound; - /** The increase in the upper bound. */ - double m_UpperBoundIncreaseFactor; - private: BinaryFunctorNeighborhoodJoinHistogramImageFilter(const Self &); //purposely not implemented void operator =(const Self&); //purposely not implemented + void ComputeHistogram(); + FunctorType m_Functor; /** The padding value. */ @@ -196,7 +191,11 @@ private: measure. */ bool m_UsePaddingValue; - HistogramType::Pointer ComputeHistogram(); + /** The histogram size. */ + HistogramSizeType m_HistogramSize; + + /** The increase in the upper bound. */ + double m_UpperBoundIncreaseFactor; }; } // end namespace otb diff --git a/Code/ChangeDetection/otbBinaryFunctorNeighborhoodJoinHistogramImageFilter.txx b/Code/ChangeDetection/otbBinaryFunctorNeighborhoodJoinHistogramImageFilter.txx index ee8702999a639a5c3ff2f944d609d38ccfa6e113..a25517df8894fcc94f85d1813008b6ca168fc442 100644 --- a/Code/ChangeDetection/otbBinaryFunctorNeighborhoodJoinHistogramImageFilter.txx +++ b/Code/ChangeDetection/otbBinaryFunctorNeighborhoodJoinHistogramImageFilter.txx @@ -171,13 +171,21 @@ BinaryFunctorNeighborhoodJoinHistogramImageFilter<TInputImage1, TInputImage2, TO return; } +template <class TInputImage1, class TInputImage2, + class TOutputImage, class TFunction> +void +BinaryFunctorNeighborhoodJoinHistogramImageFilter<TInputImage1, TInputImage2, TOutputImage, TFunction> +::BeforeThreadedGenerateData() +{ + this->ComputeHistogram(); +} + /** * Initialize the histogram */ template <class TInputImage1, class TInputImage2, class TOutputImage, class TFunction> -BinaryFunctorNeighborhoodJoinHistogramImageFilter<TInputImage1, TInputImage2, TOutputImage, TFunction> -::HistogramType::Pointer +void BinaryFunctorNeighborhoodJoinHistogramImageFilter<TInputImage1, TInputImage2, TOutputImage, TFunction> ::ComputeHistogram() { @@ -235,15 +243,16 @@ BinaryFunctorNeighborhoodJoinHistogramImageFilter<TInputImage1, TInputImage2, TO // Set the size of the upper and lower bounds of the histogram: - m_LowerBound.SetSize(2); - m_UpperBound.SetSize(2); + MeasurementVectorType lowerBound, upperBound; + lowerBound.SetSize(2); + upperBound.SetSize(2); // Initialize the upper and lower bounds of the histogram. - m_LowerBound[0] = minInput1; - m_LowerBound[1] = minInput2; - m_UpperBound[0] = + lowerBound[0] = minInput1; + lowerBound[1] = minInput2; + upperBound[0] = maxInput1 + (maxInput1 - minInput1) * m_UpperBoundIncreaseFactor; - m_UpperBound[1] = + upperBound[1] = maxInput2 + (maxInput2 - minInput2) * m_UpperBoundIncreaseFactor; typedef itk::ImageRegionConstIteratorWithIndex<Input1ImageType> Input1IteratorType; @@ -260,7 +269,7 @@ BinaryFunctorNeighborhoodJoinHistogramImageFilter<TInputImage1, TInputImage2, TO typename HistogramType::Pointer histogram = HistogramType::New(); histogram->SetMeasurementVectorSize(2); - histogram->Initialize(m_HistogramSize, m_LowerBound, m_UpperBound); + histogram->Initialize(m_HistogramSize, lowerBound, upperBound); ti1.GoToBegin(); ti2.GoToBegin(); @@ -277,8 +286,7 @@ BinaryFunctorNeighborhoodJoinHistogramImageFilter<TInputImage1, TInputImage2, TO ++ti2; } - return histogram; - + m_Histogram = histogram; } /** @@ -290,19 +298,12 @@ BinaryFunctorNeighborhoodJoinHistogramImageFilter<TInputImage1, TInputImage2, TO ::ThreadedGenerateData(const OutputImageRegionType& outputRegionForThread, itk::ThreadIdType threadId) { - - //this->Initialize(); - - typename HistogramType::Pointer histogram = ComputeHistogram(); - - //m_Functor->SetHistogram(m_Histogram); -// unsigned int i; itk::ZeroFluxNeumannBoundaryCondition<TInputImage1> nbc1; itk::ZeroFluxNeumannBoundaryCondition<TInputImage2> nbc2; -// We use dynamic_cast since inputs are stored as DataObjects. The -// ImageToJoinHistogramImageFilter::GetInput(int) always returns a pointer to a -// TInputImage1 so it cannot be used for the second input. + // We use dynamic_cast since inputs are stored as DataObjects. The + // ImageToJoinHistogramImageFilter::GetInput(int) always returns a pointer to a + // TInputImage1 so it cannot be used for the second input. Input1ImageConstPointer inputPtr1 = dynamic_cast<const TInputImage1*>(ProcessObjectType::GetInput(0)); Input2ImageConstPointer inputPtr2 @@ -353,7 +354,7 @@ BinaryFunctorNeighborhoodJoinHistogramImageFilter<TInputImage1, TInputImage2, TO while (!outputIt.IsAtEnd()) { - outputIt.Set(m_Functor(neighInputIt1, neighInputIt2, histogram)); + outputIt.Set(m_Functor(neighInputIt1, neighInputIt2, m_Histogram)); ++neighInputIt1; ++neighInputIt2; @@ -361,7 +362,6 @@ BinaryFunctorNeighborhoodJoinHistogramImageFilter<TInputImage1, TInputImage2, TO progress.CompletedPixel(); } } - } } // end namespace otb