OTB  6.1.0
Orfeo Toolbox
otbBinaryImageToDensityImageFilter.txx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2017 Centre National d'Etudes Spatiales (CNES)
3  *
4  * This file is part of Orfeo Toolbox
5  *
6  * https://www.orfeo-toolbox.org/
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #ifndef otbBinaryImageToDensityImageFilter_txx
22 #define otbBinaryImageToDensityImageFilter_txx
23 
25 #include "itkImageRegionIterator.h"
26 #include "itkProgressReporter.h"
29 
30 #include "otbMacro.h"
31 
32 namespace otb
33 {
35 template <class TInputImage, class TOutputImage, class TCountFunction>
38 {
39  m_NeighborhoodRadius.Fill(1);
40  m_CountFunction = CountFunctionType::New();
41 }
43 
45 template <class TInputImage, class TOutputImage, class TCountFunction>
48 {}
49 
50 template <class TInputImage, class TOutputImage, class TCountFunction>
51 void
54 {
55  // call the superclass' implementation of this method
56  Superclass::GenerateInputRequestedRegion();
57 
58  // get pointers to the input and output
59  InputImagePointerType inputPtr = const_cast<TInputImage *>(this->GetInput());
60  OutputImagePointerType outputPtr = this->GetOutput();
61 
62  if (!inputPtr || !outputPtr)
63  {
64  return;
65  }
66  // get a copy of the input requested region (should equal the output
67  // requested region)
68  InputImageRegionType inputRequestedRegion = inputPtr->GetRequestedRegion();
69 
70  // pad the input requested region by the operator radius
71  inputRequestedRegion.PadByRadius(m_NeighborhoodRadius);
72 
73  // crop the input requested region at the input's largest possible region
74  if (inputRequestedRegion.Crop(inputPtr->GetLargestPossibleRegion()))
75  {
76  inputPtr->SetRequestedRegion(inputRequestedRegion);
77  return;
78  }
79  else
80  {
81  // Couldn't crop the region (requested region is outside the largest
82  // possible region). Throw an exception.
83 
84  // store what we tried to request (prior to trying to crop)
85  inputPtr->SetRequestedRegion(inputRequestedRegion);
86 
87  // build an exception
88  itk::InvalidRequestedRegionError e(__FILE__, __LINE__);
89  std::ostringstream msg;
90  msg << this->GetNameOfClass()
91  << "::GenerateInputRequestedRegion()";
92  e.SetLocation(msg.str().c_str());
93  e.SetDescription("Requested region is (at least partially) outside the largest possible region.");
94  e.SetDataObject(inputPtr);
95  throw e;
96  }
97 }
98 
99 template <class TInputImage, class TOutputImage, class TCountFunction>
100 void
103 {
104  Superclass::BeforeThreadedGenerateData();
105 
106  m_CountFunction->SetInputImage(this->GetInput());
107  m_CountFunction->SetNeighborhoodRadius(m_NeighborhoodRadius);
108 }
109 
111 template <class TInputImage, class TOutputImage, class TCountFunction>
112 void
114 ::ThreadedGenerateData(const InputImageRegionType& outputRegionForThread, itk::ThreadIdType threadId)
115 {
116  InputImagePointerType inputPtr = const_cast<InputImageType *> (this->GetInput());
117  OutputImagePointerType outputPtr = this->GetOutput();
119 
121  RadiusType r;
122  r[0] = m_NeighborhoodRadius[0];
123  r[1] = m_NeighborhoodRadius[1];
124 
125  NeighborhoodIteratorType it;
128  faceList = bC(inputPtr, outputRegionForThread, r);
130 
131  itk::ImageRegionIterator<OutputImageType> itOut(outputPtr, outputRegionForThread);
132 
133  itk::ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
134 
135  typename InputImageType::IndexType index;
136 
137  for (fit = faceList.begin(); fit != faceList.end(); ++fit)
138  {
139  it = itk::ConstNeighborhoodIterator<TInputImage>(r, inputPtr, *fit);
140 
141  itOut = itk::ImageRegionIterator<TOutputImage>(outputPtr, *fit);
142  it.OverrideBoundaryCondition(&nbc);
143  it.GoToBegin();
144 
145  while (!itOut.IsAtEnd())
146  {
147  index = it.GetIndex();
148 
149  if (outputRegionForThread.IsInside(index))
150  {
151  itOut.Set(m_CountFunction->EvaluateAtIndex(index));
152  }
153 
154  ++itOut;
155  ++it;
156  progress.CompletedPixel(); // potential exception thrown here
157  }
158  }
159 }
160 
virtual bool IsAtEnd() const
void BeforeThreadedGenerateData() ITK_OVERRIDE
void SetDataObject(DataObject *dobj)
virtual void SetDescription(const std::string &s)
virtual void SetLocation(const std::string &s)
itk::Index< Monteverdi_DIMENSION > IndexType
Definition: mvdTypes.h:142
unsigned int ThreadIdType
~BinaryImageToDensityImageFilter() ITK_OVERRIDE
void GenerateInputRequestedRegion() ITK_OVERRIDE
void ThreadedGenerateData(const InputImageRegionType &outputRegionForThread, itk::ThreadIdType threadId) ITK_OVERRIDE