OTB  5.0.0
Orfeo Toolbox
otbBinaryImageToDensityImageFilter.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: ORFEO Toolbox
4  Language: C++
5  Date: $Date$
6  Version: $Revision$
7 
8 
9  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
10  See OTBCopyright.txt for details.
11 
12 
13  This software is distributed WITHOUT ANY WARRANTY; without even
14  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  PURPOSE. See the above copyright notices for more information.
16 
17 =========================================================================*/
18 #ifndef __otbBinaryImageToDensityImageFilter_txx
19 #define __otbBinaryImageToDensityImageFilter_txx
20 
22 #include "itkImageRegionIterator.h"
23 #include "itkProgressReporter.h"
26 
27 #include "otbMacro.h"
28 
29 namespace otb
30 {
32 template <class TInputImage, class TOutputImage, class TCountFunction>
35 {
36  m_NeighborhoodRadius.Fill(1);
37  m_CountFunction = CountFunctionType::New();
38 }
40 
42 template <class TInputImage, class TOutputImage, class TCountFunction>
45 {}
46 
47 template <class TInputImage, class TOutputImage, class TCountFunction>
48 void
51 {
52  // call the superclass' implementation of this method
53  Superclass::GenerateInputRequestedRegion();
54 
55  // get pointers to the input and output
56  InputImagePointerType inputPtr = const_cast<TInputImage *>(this->GetInput());
57  OutputImagePointerType outputPtr = this->GetOutput();
58 
59  if (!inputPtr || !outputPtr)
60  {
61  return;
62  }
63  // get a copy of the input requested region (should equal the output
64  // requested region)
65  InputImageRegionType inputRequestedRegion = inputPtr->GetRequestedRegion();
66 
67  // pad the input requested region by the operator radius
68  inputRequestedRegion.PadByRadius(m_NeighborhoodRadius);
69 
70  // crop the input requested region at the input's largest possible region
71  if (inputRequestedRegion.Crop(inputPtr->GetLargestPossibleRegion()))
72  {
73  inputPtr->SetRequestedRegion(inputRequestedRegion);
74  return;
75  }
76  else
77  {
78  // Couldn't crop the region (requested region is outside the largest
79  // possible region). Throw an exception.
80 
81  // store what we tried to request (prior to trying to crop)
82  inputPtr->SetRequestedRegion(inputRequestedRegion);
83 
84  // build an exception
85  itk::InvalidRequestedRegionError e(__FILE__, __LINE__);
86  std::ostringstream msg;
87  msg << this->GetNameOfClass()
88  << "::GenerateInputRequestedRegion()";
89  e.SetLocation(msg.str().c_str());
90  e.SetDescription("Requested region is (at least partially) outside the largest possible region.");
91  e.SetDataObject(inputPtr);
92  throw e;
93  }
94 }
95 
96 template <class TInputImage, class TOutputImage, class TCountFunction>
97 void
100 {
101  Superclass::BeforeThreadedGenerateData();
102 
103  m_CountFunction->SetInputImage(this->GetInput());
104  m_CountFunction->SetNeighborhoodRadius(m_NeighborhoodRadius);
105 }
106 
108 template <class TInputImage, class TOutputImage, class TCountFunction>
109 void
111 ::ThreadedGenerateData(const InputImageRegionType& outputRegionForThread, itk::ThreadIdType threadId)
112 {
113  InputImagePointerType inputPtr = const_cast<InputImageType *> (this->GetInput());
114  OutputImagePointerType outputPtr = this->GetOutput();
116 
118  RadiusType r;
119  r[0] = m_NeighborhoodRadius[0];
120  r[1] = m_NeighborhoodRadius[1];
121 
122  NeighborhoodIteratorType it;
125  faceList = bC(inputPtr, outputRegionForThread, r);
127 
128  itk::ImageRegionIterator<OutputImageType> itOut(outputPtr, outputRegionForThread);
129 
130  itk::ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
131 
132  typename InputImageType::IndexType index;
133 
134  for (fit = faceList.begin(); fit != faceList.end(); ++fit)
135  {
136  it = itk::ConstNeighborhoodIterator<TInputImage>(r, inputPtr, *fit);
137 
138  itOut = itk::ImageRegionIterator<TOutputImage>(outputPtr, *fit);
139  it.OverrideBoundaryCondition(&nbc);
140  it.GoToBegin();
141 
142  while (!itOut.IsAtEnd())
143  {
144  index = it.GetIndex();
145 
146  if (outputRegionForThread.IsInside(index))
147  {
148  itOut.Set(m_CountFunction->EvaluateAtIndex(index));
149  }
150 
151  ++itOut;
152  ++it;
153  progress.CompletedPixel(); // potential exception thrown here
154  }
155  }
156 }
157 
virtual bool IsAtEnd() const
void SetDataObject(DataObject *dobj)
virtual void SetDescription(const std::string &s)
virtual void SetLocation(const std::string &s)
virtual void ThreadedGenerateData(const InputImageRegionType &outputRegionForThread, itk::ThreadIdType threadId)
unsigned int ThreadIdType