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>
201 itk::ThreadIdType itkNotUsed(threadId))
203 const TDisparityImage* directHmap = this->GetDirectHorizontalDisparityMapInput();
204 const TDisparityImage* directVmap = this->GetDirectVerticalDisparityMapInput();
205 const TDisparityImage* reverseHmap = this->GetReverseHorizontalDisparityMapInput();
206 const TDisparityImage* reverseVmap = this->GetReverseVerticalDisparityMapInput();
208 TOutputImage* output = this->GetOutput();
212 typedef itk::ImageRegionIterator<TOutputImage> MaskIteratorType;
213 MaskIteratorType outIter = MaskIteratorType(output, outputRegionForThread);
215 typedef itk::ImageRegionConstIteratorWithIndex<TDisparityImage> DispIteratorType;
216 DispIteratorType directHorizIter = DispIteratorType(directHmap, outputRegionForThread);
218 DispIteratorType directVertiIter;
221 directVertiIter = DispIteratorType(directVmap, outputRegionForThread);
222 directVertiIter.GoToBegin();
226 directHorizIter.GoToBegin();
228 while (!outIter.IsAtEnd())
230 IndexType startIndex = directHorizIter.GetIndex();
231 itk::ContinuousIndex<double, 2> tmpIndex(startIndex);
233 tmpIndex[0] += directHorizIter.Get();
235 tmpIndex[1] += directVertiIter.Get();
238 typedef typename IndexType::IndexValueType IndexValueType;
240 ul[0] =
static_cast<long>(std::floor(tmpIndex[0]));
241 ul[1] =
static_cast<long>(std::floor(tmpIndex[1]));
242 if (ul[0] < buffered.GetIndex()[0])
243 ul[0] = buffered.GetIndex()[0];
244 if (ul[1] < buffered.GetIndex()[1])
245 ul[1] = buffered.GetIndex()[1];
246 if (ul[0] >
static_cast<IndexValueType
>(buffered.GetIndex()[0] + buffered.GetSize()[0] - 1))
247 ul[0] = (buffered.GetIndex()[0] + buffered.GetSize()[0] - 1);
248 if (ul[1] >
static_cast<IndexValueType
>(buffered.GetIndex()[1] + buffered.GetSize()[1] - 1))
249 ul[1] = (buffered.GetIndex()[1] + buffered.GetSize()[1] - 1);
254 if (ul[0] <
static_cast<IndexValueType
>(buffered.GetIndex()[0] + buffered.GetSize()[0] - 1))
259 if (ul[1] <
static_cast<IndexValueType
>(buffered.GetIndex()[1] + buffered.GetSize()[1] - 1))
265 double rx = tmpIndex[0] -
static_cast<double>(ul[0]);
266 double ry = tmpIndex[1] -
static_cast<double>(ul[1]);
268 itk::ContinuousIndex<double, 2> backIndex(tmpIndex);
269 backIndex[0] += (1. - ry) * ((1. - rx) * reverseHmap->GetPixel(ul) + rx * reverseHmap->GetPixel(ur)) +
270 ry * ((1. - rx) * reverseHmap->GetPixel(ll) + rx * reverseHmap->GetPixel(lr));
273 backIndex[1] += (1. - ry) * ((1. - rx) * reverseVmap->GetPixel(ul) + rx * reverseVmap->GetPixel(ur)) +
274 ry * ((1. - rx) * reverseVmap->GetPixel(ll) + rx * reverseVmap->GetPixel(lr));
277 if (std::abs(backIndex[0] -
static_cast<double>(startIndex[0])) < this->m_Tolerance &&
278 std::abs(backIndex[1] -
static_cast<double>(startIndex[1])) < this->m_Tolerance)