21 #ifndef otbBijectionCoherencyFilter_hxx
22 #define otbBijectionCoherencyFilter_hxx
26 #include "itkImageRegionConstIteratorWithIndex.h"
27 #include "itkImageRegionIterator.h"
32 template <
class TDisparityImage,
class TOutputImage>
36 this->SetNumberOfRequiredInputs(4);
37 this->SetNumberOfRequiredInputs(1);
38 this->m_Tolerance = 1.;
39 this->m_MinHDisp = -5;
41 this->m_MinVDisp = -5;
45 this->SetNumberOfRequiredOutputs(1);
46 this->SetNthOutput(0, TOutputImage::New());
49 template <
class TDisparityImage,
class TOutputImage>
53 this->SetNthInput(0,
const_cast<TDisparityImage*
>(hmap));
56 template <
class TDisparityImage,
class TOutputImage>
60 this->SetNthInput(1,
const_cast<TDisparityImage*
>(vmap));
63 template <
class TDisparityImage,
class TOutputImage>
67 this->SetNthInput(2,
const_cast<TDisparityImage*
>(hmap));
70 template <
class TDisparityImage,
class TOutputImage>
74 this->SetNthInput(3,
const_cast<TDisparityImage*
>(vmap));
77 template <
class TDisparityImage,
class TOutputImage>
80 if (this->GetNumberOfInputs() < 1)
84 return static_cast<const TDisparityImage*
>(this->itk::ProcessObject::GetInput(0));
87 template <
class TDisparityImage,
class TOutputImage>
90 if (this->GetNumberOfInputs() < 2)
94 return static_cast<const TDisparityImage*
>(this->itk::ProcessObject::GetInput(1));
97 template <
class TDisparityImage,
class TOutputImage>
100 if (this->GetNumberOfInputs() < 3)
104 return static_cast<const TDisparityImage*
>(this->itk::ProcessObject::GetInput(2));
107 template <
class TDisparityImage,
class TOutputImage>
110 if (this->GetNumberOfInputs() < 4)
114 return static_cast<const TDisparityImage*
>(this->itk::ProcessObject::GetInput(3));
117 template <
class TDisparityImage,
class TOutputImage>
120 this->Superclass::GenerateOutputInformation();
123 const TDisparityImage* directHmap = this->GetDirectHorizontalDisparityMapInput();
124 const TDisparityImage* reverseHmap = this->GetReverseHorizontalDisparityMapInput();
126 const TDisparityImage* directVmap = this->GetDirectVerticalDisparityMapInput();
127 const TDisparityImage* reverseVmap = this->GetReverseVerticalDisparityMapInput();
131 itkExceptionMacro(<<
"Direct horizontal disparity map is missing");
136 itkExceptionMacro(<<
"Reverse horizontal disparity map is missing");
139 if (directVmap && directVmap->GetLargestPossibleRegion() != directHmap->GetLargestPossibleRegion())
141 itkExceptionMacro(<<
"Horizontal and vertical direct disparity maps have different sizes.");
144 if (reverseVmap && reverseVmap->GetLargestPossibleRegion() != reverseHmap->GetLargestPossibleRegion())
146 itkExceptionMacro(<<
"Horizontal and vertical reverse disparity maps have different sizes.");
149 if (this->m_MinHDisp > this->m_MaxHDisp)
151 itkExceptionMacro(<<
"Wrong horizontal exploration values");
153 if (this->m_MinVDisp > this->m_MaxVDisp)
155 itkExceptionMacro(<<
"Wrong horizontal exploration values");
159 template <
class TDisparityImage,
class TOutputImage>
162 this->Superclass::GenerateInputRequestedRegion();
165 InputRegionType directLargest = this->GetDirectHorizontalDisparityMapInput()->GetLargestPossibleRegion();
167 InputRegionType reverseLargest = this->GetReverseHorizontalDisparityMapInput()->GetLargestPossibleRegion();
170 this->CallCopyOutputRegionToInputRegion(directRequested, requested);
172 reverseRequested.SetIndex(0, requested.GetIndex(0) + this->m_MinHDisp);
173 reverseRequested.SetIndex(1, requested.GetIndex(1) + this->m_MinVDisp);
174 reverseRequested.SetSize(0, requested.GetSize(0) + this->m_MaxHDisp - this->m_MinHDisp);
175 reverseRequested.SetSize(1, requested.GetSize(1) + this->m_MaxVDisp - this->m_MinVDisp);
177 if (!reverseRequested.Crop(reverseLargest))
179 reverseRequested.SetIndex(0, reverseLargest.GetIndex(0));
180 reverseRequested.SetIndex(1, reverseLargest.GetIndex(1));
181 reverseRequested.SetSize(0, 0);
182 reverseRequested.SetSize(1, 0);
185 TDisparityImage* directHmap =
const_cast<TDisparityImage*
>(this->GetDirectHorizontalDisparityMapInput());
186 TDisparityImage* directVmap =
const_cast<TDisparityImage*
>(this->GetDirectVerticalDisparityMapInput());
187 TDisparityImage* reverseHmap =
const_cast<TDisparityImage*
>(this->GetReverseHorizontalDisparityMapInput());
188 TDisparityImage* reverseVmap =
const_cast<TDisparityImage*
>(this->GetReverseVerticalDisparityMapInput());
190 directHmap->SetRequestedRegion(directRequested);
192 directVmap->SetRequestedRegion(directRequested);
194 reverseHmap->SetRequestedRegion(reverseRequested);
196 reverseVmap->SetRequestedRegion(reverseRequested);
199 template <
class TDisparityImage,
class TOutputImage>
202 const TDisparityImage* directHmap = this->GetDirectHorizontalDisparityMapInput();
203 const TDisparityImage* directVmap = this->GetDirectVerticalDisparityMapInput();
204 const TDisparityImage* reverseHmap = this->GetReverseHorizontalDisparityMapInput();
205 const TDisparityImage* reverseVmap = this->GetReverseVerticalDisparityMapInput();
207 TOutputImage* output = this->GetOutput();
211 typedef itk::ImageRegionIterator<TOutputImage> MaskIteratorType;
212 MaskIteratorType outIter = MaskIteratorType(output, outputRegionForThread);
214 typedef itk::ImageRegionConstIteratorWithIndex<TDisparityImage> DispIteratorType;
215 DispIteratorType directHorizIter = DispIteratorType(directHmap, outputRegionForThread);
217 DispIteratorType directVertiIter;
220 directVertiIter = DispIteratorType(directVmap, outputRegionForThread);
221 directVertiIter.GoToBegin();
225 directHorizIter.GoToBegin();
227 while (!outIter.IsAtEnd())
229 IndexType startIndex = directHorizIter.GetIndex();
230 itk::ContinuousIndex<double, 2> tmpIndex(startIndex);
232 tmpIndex[0] += directHorizIter.Get();
234 tmpIndex[1] += directVertiIter.Get();
237 typedef typename IndexType::IndexValueType IndexValueType;
239 ul[0] =
static_cast<long>(std::floor(tmpIndex[0]));
240 ul[1] =
static_cast<long>(std::floor(tmpIndex[1]));
241 if (ul[0] < buffered.GetIndex()[0])
242 ul[0] = buffered.GetIndex()[0];
243 if (ul[1] < buffered.GetIndex()[1])
244 ul[1] = buffered.GetIndex()[1];
245 if (ul[0] >
static_cast<IndexValueType
>(buffered.GetIndex()[0] + buffered.GetSize()[0] - 1))
246 ul[0] = (buffered.GetIndex()[0] + buffered.GetSize()[0] - 1);
247 if (ul[1] >
static_cast<IndexValueType
>(buffered.GetIndex()[1] + buffered.GetSize()[1] - 1))
248 ul[1] = (buffered.GetIndex()[1] + buffered.GetSize()[1] - 1);
253 if (ul[0] <
static_cast<IndexValueType
>(buffered.GetIndex()[0] + buffered.GetSize()[0] - 1))
258 if (ul[1] <
static_cast<IndexValueType
>(buffered.GetIndex()[1] + buffered.GetSize()[1] - 1))
264 double rx = tmpIndex[0] -
static_cast<double>(ul[0]);
265 double ry = tmpIndex[1] -
static_cast<double>(ul[1]);
267 itk::ContinuousIndex<double, 2> backIndex(tmpIndex);
268 backIndex[0] += (1. - ry) * ((1. - rx) * reverseHmap->GetPixel(ul) + rx * reverseHmap->GetPixel(ur)) +
269 ry * ((1. - rx) * reverseHmap->GetPixel(ll) + rx * reverseHmap->GetPixel(lr));
272 backIndex[1] += (1. - ry) * ((1. - rx) * reverseVmap->GetPixel(ul) + rx * reverseVmap->GetPixel(ur)) +
273 ry * ((1. - rx) * reverseVmap->GetPixel(ll) + rx * reverseVmap->GetPixel(lr));
276 if (std::abs(backIndex[0] -
static_cast<double>(startIndex[0])) < this->m_Tolerance &&
277 std::abs(backIndex[1] -
static_cast<double>(startIndex[1])) < this->m_Tolerance)
void SetReverseHorizontalDisparityMapInput(const TDisparityImage *hmap)
void GenerateOutputInformation() override
void SetDirectHorizontalDisparityMapInput(const TDisparityImage *hmap)
void SetDirectVerticalDisparityMapInput(const TDisparityImage *vmap)
const TDisparityImage * GetDirectHorizontalDisparityMapInput() const
DispMapType::RegionType InputRegionType
DispMapType::IndexType IndexType
BijectionCoherencyFilter()
void DynamicThreadedGenerateData(const OutputRegionType &outputRegionForThread) override
MaskType::RegionType OutputRegionType
void GenerateInputRequestedRegion() override
const TDisparityImage * GetReverseVerticalDisparityMapInput() const
const TDisparityImage * GetDirectVerticalDisparityMapInput() const
void SetReverseVerticalDisparityMapInput(const TDisparityImage *vmap)
const TDisparityImage * GetReverseHorizontalDisparityMapInput() const
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.