18 #ifndef __otbDisparityMapToDEMFilter_txx
19 #define __otbDisparityMapToDEMFilter_txx
28 template <
class TDisparityImage,
class TInputImage,
class TOutputDEMImage,
29 class TEpipolarGridImage,
class TMaskImage>
34 this->SetNumberOfInputs(7);
35 this->SetNumberOfRequiredInputs(1);
38 this->SetNumberOfOutputs(1);
39 this->SetNthOutput(0,TOutputDEMImage::New());
43 m_ElevationMax = 100.0;
46 m_InputSplitter = SplitterType::New();
47 m_UsedInputSplits = 1;
50 template <
class TDisparityImage,
class TInputImage,
class TOutputDEMImage,
51 class TEpipolarGridImage,
class TMaskImage>
56 template <
class TDisparityImage,
class TInputImage,
class TOutputDEMImage,
57 class TEpipolarGridImage,
class TMaskImage>
63 this->SetNthInput(0, const_cast<TDisparityImage *>( hmap ));
66 template <
class TDisparityImage,
class TInputImage,
class TOutputDEMImage,
67 class TEpipolarGridImage,
class TMaskImage>
73 this->SetNthInput(1, const_cast<TDisparityImage *>( vmap ));
76 template <
class TDisparityImage,
class TInputImage,
class TOutputDEMImage,
77 class TEpipolarGridImage,
class TMaskImage>
83 this->SetNthInput(2, const_cast<TInputImage *>( image ));
86 template <
class TDisparityImage,
class TInputImage,
class TOutputDEMImage,
87 class TEpipolarGridImage,
class TMaskImage>
93 this->SetNthInput(3, const_cast<TInputImage *>( image ));
96 template <
class TDisparityImage,
class TInputImage,
class TOutputDEMImage,
97 class TEpipolarGridImage,
class TMaskImage>
103 this->SetNthInput(4, const_cast<TEpipolarGridImage *>( grid ));
106 template <
class TDisparityImage,
class TInputImage,
class TOutputDEMImage,
107 class TEpipolarGridImage,
class TMaskImage>
113 this->SetNthInput(5, const_cast<TEpipolarGridImage *>( grid ));
116 template <
class TDisparityImage,
class TInputImage,
class TOutputDEMImage,
117 class TEpipolarGridImage,
class TMaskImage>
123 this->SetNthInput(6, const_cast<TMaskImage *>( mask ));
126 template <
class TDisparityImage,
class TInputImage,
class TOutputDEMImage,
127 class TEpipolarGridImage,
class TMaskImage>
128 const TDisparityImage *
132 if(this->GetNumberOfInputs()<1)
139 template <
class TDisparityImage,
class TInputImage,
class TOutputDEMImage,
140 class TEpipolarGridImage,
class TMaskImage>
141 const TDisparityImage *
145 if(this->GetNumberOfInputs()<2)
152 template <
class TDisparityImage,
class TInputImage,
class TOutputDEMImage,
153 class TEpipolarGridImage,
class TMaskImage>
158 if(this->GetNumberOfInputs()<3)
165 template <
class TDisparityImage,
class TInputImage,
class TOutputDEMImage,
166 class TEpipolarGridImage,
class TMaskImage>
171 if(this->GetNumberOfInputs()<4)
178 template <
class TDisparityImage,
class TInputImage,
class TOutputDEMImage,
179 class TEpipolarGridImage,
class TMaskImage>
180 const TEpipolarGridImage *
184 if(this->GetNumberOfInputs()<5)
191 template <
class TDisparityImage,
class TInputImage,
class TOutputDEMImage,
192 class TEpipolarGridImage,
class TMaskImage>
193 const TEpipolarGridImage *
197 if(this->GetNumberOfInputs()<6)
204 template <
class TDisparityImage,
class TInputImage,
class TOutputDEMImage,
205 class TEpipolarGridImage,
class TMaskImage>
210 if(this->GetNumberOfInputs()<7)
217 template <
class TDisparityImage,
class TInputImage,
class TOutputDEMImage,
218 class TEpipolarGridImage,
class TMaskImage>
219 const TOutputDEMImage *
223 if(this->GetNumberOfOutputs()<1)
230 template <
class TDisparityImage,
class TInputImage,
class TOutputDEMImage,
231 class TEpipolarGridImage,
class TMaskImage>
236 if(this->GetNumberOfOutputs()<1)
243 template <
class TDisparityImage,
class TInputImage,
class TOutputDEMImage,
244 class TEpipolarGridImage,
class TMaskImage>
250 const TInputImage * leftImgPtr = this->GetLeftInput();
251 const TInputImage * rightImgPtr = this->GetRightInput();
252 TOutputDEMImage * outputPtr = this->GetDEMOutput();
256 RSTransform2DType::Pointer leftToGroundTransform = RSTransform2DType::New();
257 leftToGroundTransform->SetInputKeywordList(leftImgPtr->GetImageKeywordlist());
258 leftToGroundTransform->InstanciateTransform();
260 RSTransform2DType::Pointer rightToGroundTransform = RSTransform2DType::New();
261 rightToGroundTransform->SetInputKeywordList(rightImgPtr->GetImageKeywordlist());
262 rightToGroundTransform->InstanciateTransform();
265 typename SensorImageType::SizeType inputSize = leftImgPtr->GetLargestPossibleRegion().GetSize();
266 typename SensorImageType::PointType tmpPoint;
267 tmpPoint = leftImgPtr->GetOrigin();
268 RSTransform2DType::OutputPointType left_ul = leftToGroundTransform->TransformPoint(tmpPoint);
270 tmpPoint[0] = (leftImgPtr->GetOrigin())[0] + (leftImgPtr->GetSpacing())[0] * static_cast<double>(inputSize[0]);
271 tmpPoint[1] = (leftImgPtr->GetOrigin())[1];
272 RSTransform2DType::OutputPointType left_ur = leftToGroundTransform->TransformPoint(tmpPoint);
274 tmpPoint[0] = (leftImgPtr->GetOrigin())[0] + (leftImgPtr->GetSpacing())[0] * static_cast<double>(inputSize[0]);
275 tmpPoint[1] = (leftImgPtr->GetOrigin())[1] + (leftImgPtr->GetSpacing())[1] * static_cast<double>(inputSize[1]);
276 RSTransform2DType::OutputPointType left_lr = leftToGroundTransform->TransformPoint(tmpPoint);
278 tmpPoint[0] = (leftImgPtr->GetOrigin())[0];
279 tmpPoint[1] = (leftImgPtr->GetOrigin())[1] + (leftImgPtr->GetSpacing())[1] * static_cast<double>(inputSize[1]);
280 RSTransform2DType::OutputPointType left_ll = leftToGroundTransform->TransformPoint(tmpPoint);
283 inputSize = rightImgPtr->GetLargestPossibleRegion().GetSize();
284 tmpPoint = rightImgPtr->GetOrigin();
285 RSTransform2DType::OutputPointType right_ul = rightToGroundTransform->TransformPoint(tmpPoint);
287 tmpPoint[0] = (rightImgPtr->GetOrigin())[0] + (rightImgPtr->GetSpacing())[0] * static_cast<double>(inputSize[0]);
288 tmpPoint[1] = (rightImgPtr->GetOrigin())[1];
289 RSTransform2DType::OutputPointType right_ur = rightToGroundTransform->TransformPoint(tmpPoint);
291 tmpPoint[0] = (rightImgPtr->GetOrigin())[0] + (rightImgPtr->GetSpacing())[0] * static_cast<double>(inputSize[0]);
292 tmpPoint[1] = (rightImgPtr->GetOrigin())[1] + (rightImgPtr->GetSpacing())[1] * static_cast<double>(inputSize[1]);
293 RSTransform2DType::OutputPointType right_lr = rightToGroundTransform->TransformPoint(tmpPoint);
295 tmpPoint[0] = (rightImgPtr->GetOrigin())[0];
296 tmpPoint[1] = (rightImgPtr->GetOrigin())[1] + (rightImgPtr->GetSpacing())[1] * static_cast<double>(inputSize[1]);
297 RSTransform2DType::OutputPointType right_ll = rightToGroundTransform->TransformPoint(tmpPoint);
299 double left_xmin = std::min(std::min(std::min(left_ul[0],left_ur[0]),left_lr[0]),left_ll[0]);
300 double left_xmax = std::max(std::max(std::max(left_ul[0],left_ur[0]),left_lr[0]),left_ll[0]);
301 double left_ymin = std::min(std::min(std::min(left_ul[1],left_ur[1]),left_lr[1]),left_ll[1]);
302 double left_ymax = std::max(std::max(std::max(left_ul[1],left_ur[1]),left_lr[1]),left_ll[1]);
304 double right_xmin = std::min(std::min(std::min(right_ul[0],right_ur[0]),right_lr[0]),right_ll[0]);
305 double right_xmax = std::max(std::max(std::max(right_ul[0],right_ur[0]),right_lr[0]),right_ll[0]);
306 double right_ymin = std::min(std::min(std::min(right_ul[1],right_ur[1]),right_lr[1]),right_ll[1]);
307 double right_ymax = std::max(std::max(std::max(right_ul[1],right_ur[1]),right_lr[1]),right_ll[1]);
309 double box_xmin = std::max(left_xmin,right_xmin);
310 double box_xmax = std::min(left_xmax,right_xmax);
311 double box_ymin = std::max(left_ymin,right_ymin);
312 double box_ymax = std::min(left_ymax,right_ymax);
314 if (box_xmin >= box_xmax || box_ymin >= box_ymax)
316 itkExceptionMacro(<<
"Wrong reconstruction area, images don't overlap, check image corners");
320 typename TOutputDEMImage::PointType outOrigin;
321 outOrigin[0] = box_xmin;
322 outOrigin[1] = box_ymax;
323 outputPtr->SetOrigin(outOrigin);
327 typename TOutputDEMImage::SpacingType outSpacing;
328 outSpacing[0] = 57.295779513 * m_DEMGridStep / (6378137.0 * vcl_cos((box_ymin + box_ymax) * 0.5 * 0.01745329251));
329 outSpacing[1] = -57.295779513 * m_DEMGridStep / 6378137.0;
330 outputPtr->SetSpacing(outSpacing);
333 typename DEMImageType::RegionType outRegion;
334 outRegion.SetIndex(0,0);
335 outRegion.SetIndex(1,0);
336 outRegion.SetSize(0, static_cast<unsigned int>((box_xmax - box_xmin) / vcl_abs(outSpacing[0])));
337 outRegion.SetSize(1, static_cast<unsigned int>((box_ymax - box_ymin) / vcl_abs(outSpacing[1])));
339 outputPtr->SetLargestPossibleRegion(outRegion);
340 outputPtr->SetNumberOfComponentsPerPixel(1);
343 template <
class TDisparityImage,
class TInputImage,
class TOutputDEMImage,
344 class TEpipolarGridImage,
class TMaskImage>
350 TEpipolarGridImage * leftGrid =
const_cast<TEpipolarGridImage*
>(this->GetLeftEpipolarGridInput());
351 TEpipolarGridImage * rightGrid =
const_cast<TEpipolarGridImage*
>(this->GetRightEpipolarGridInput());
353 leftGrid->SetRequestedRegionToLargestPossibleRegion();
354 rightGrid->SetRequestedRegionToLargestPossibleRegion();
356 leftGrid->UpdateOutputData();
357 rightGrid->UpdateOutputData();
361 const TInputImage * leftSensor = this->GetLeftInput();
362 TOutputDEMImage * outputDEM = this->GetDEMOutput();
364 typename DEMImageType::RegionType outRegion = outputDEM->GetRequestedRegion();
365 typename DEMImageType::PointType outOrigin = outputDEM->GetOrigin();
366 typename DEMImageType::SpacingType outSpacing = outputDEM->GetSpacing();
369 groundToLeftTransform->SetOutputKeywordList(leftSensor->GetImageKeywordlist());
370 groundToLeftTransform->InstanciateTransform();
374 TDisparityImage * horizDisp =
const_cast<TDisparityImage*
>(this->GetHorizontalDisparityMapInput());
375 TDisparityImage * vertiDisp =
const_cast<TDisparityImage*
>(this->GetVerticalDisparityMapInput());
376 TMaskImage * maskDisp =
const_cast<TMaskImage*
>(this->GetDisparityMaskInput());
379 if(horizDisp->GetLargestPossibleRegion()
380 != vertiDisp->GetLargestPossibleRegion())
382 itkExceptionMacro(<<
"Horizontal and vertical disparity maps do not have the same size ! Horizontal largest region: "
383 <<horizDisp->GetLargestPossibleRegion()<<
", vertical largest region: "<<vertiDisp->GetLargestPossibleRegion());
386 if (maskDisp && horizDisp->GetLargestPossibleRegion() != maskDisp->GetLargestPossibleRegion())
388 itkExceptionMacro(<<
"Disparity map and mask do not have the same size ! Map region : "
389 <<horizDisp->GetLargestPossibleRegion()<<
", mask region : "<<maskDisp->GetLargestPossibleRegion());
394 corners[0][0]= outOrigin[0] + outSpacing[0] * outRegion.GetIndex(0);
395 corners[0][1]= outOrigin[1] + outSpacing[1] * outRegion.GetIndex(1);
396 corners[0][2]= m_ElevationMin;
398 corners[1][0]= corners[0][0];
399 corners[1][1]= corners[0][1];
400 corners[1][2]= m_ElevationMax;
402 corners[2][0]= outOrigin[0] + outSpacing[0] * (outRegion.GetIndex(0) + outRegion.GetSize(0));
403 corners[2][1]= outOrigin[1] + outSpacing[1] * outRegion.GetIndex(1);
404 corners[2][2]= m_ElevationMin;
406 corners[3][0]= corners[2][0];
407 corners[3][1]= corners[2][1];
408 corners[3][2]= m_ElevationMax;
410 corners[4][0]= outOrigin[0] + outSpacing[0] * (outRegion.GetIndex(0) + outRegion.GetSize(0));
411 corners[4][1]= outOrigin[1] + outSpacing[1] * (outRegion.GetIndex(1) + outRegion.GetSize(1));
412 corners[4][2]= m_ElevationMin;
414 corners[5][0]= corners[4][0];
415 corners[5][1]= corners[4][1];
416 corners[5][2]= m_ElevationMax;
418 corners[6][0]= outOrigin[0] + outSpacing[0] * outRegion.GetIndex(0);
419 corners[6][1]= outOrigin[1] + outSpacing[1] * (outRegion.GetIndex(1) + outRegion.GetSize(1));
420 corners[6][2]= m_ElevationMin;
422 corners[7][0]= corners[6][0];
423 corners[7][1]= corners[6][1];
424 corners[7][2]= m_ElevationMax;
427 double a_grid,b_grid;
431 typename GridImageType::IndexType gridIndex;
432 typename GridImageType::IndexType gridIndexU;
433 typename GridImageType::IndexType gridIndexV;
434 typename GridImageType::PointType gridPoint;
435 typename GridImageType::PointType gridPointU;
436 typename GridImageType::PointType gridPointV;
437 typename GridImageType::PixelType origLoc(2);
438 typename GridImageType::PixelType uUnitLoc(2);
439 typename GridImageType::PixelType vUnitLoc(2);
441 typename GridImageType::RegionType gridRegion = leftGrid->GetLargestPossibleRegion();
443 typename DisparityMapType::PointType epiPosition;
445 double epiIndexMin[2];
446 double epiIndexMax[2];
449 maxGridIndex[0] =
static_cast<int>(gridRegion.GetIndex(0) + gridRegion.GetSize(0)) - 2;
450 maxGridIndex[1] =
static_cast<int>(gridRegion.GetIndex(1) + gridRegion.GetSize(1)) - 2;
451 minGridIndex[0] =
static_cast<int>(gridRegion.GetIndex(0));
452 minGridIndex[1] =
static_cast<int>(gridRegion.GetIndex(1));
454 for (
unsigned int k=0; k<8; k++)
457 TDPointType tmpSensor = groundToLeftTransform->TransformPoint(corners[k]);
460 gridIndex[0] = minGridIndex[0];
461 gridIndex[1] = minGridIndex[1];
464 for (
unsigned int s=0; s<3; s++)
466 gridIndexU[0] = gridIndex[0] + 1;
467 gridIndexU[1] = gridIndex[1];
469 gridIndexV[0] = gridIndex[0];
470 gridIndexV[1] = gridIndex[1] + 1;
472 leftGrid->TransformIndexToPhysicalPoint(gridIndex, gridPoint);
473 leftGrid->TransformIndexToPhysicalPoint(gridIndexU, gridPointU);
474 leftGrid->TransformIndexToPhysicalPoint(gridIndexV, gridPointV);
476 origLoc[0] = (leftGrid->GetPixel(gridIndex))[0] + gridPoint[0];
477 origLoc[1] = (leftGrid->GetPixel(gridIndex))[1] + gridPoint[1];
478 uUnitLoc[0] = (leftGrid->GetPixel(gridIndexU))[0] + gridPointU[0];
479 uUnitLoc[1] = (leftGrid->GetPixel(gridIndexU))[1] + gridPointU[1];
480 vUnitLoc[0] = (leftGrid->GetPixel(gridIndexV))[0] + gridPointV[0];
481 vUnitLoc[1] = (leftGrid->GetPixel(gridIndexV))[1] + gridPointV[1];
483 u_loc[0] =
static_cast<double>(uUnitLoc[0] - origLoc[0]);
484 u_loc[1] =
static_cast<double>(uUnitLoc[1] - origLoc[1]);
486 v_loc[0] =
static_cast<double>(vUnitLoc[0] - origLoc[0]);
487 v_loc[1] =
static_cast<double>(vUnitLoc[1] - origLoc[1]);
489 det = u_loc[0] * v_loc[1] - v_loc[0] * u_loc[1];
491 x_loc =
static_cast<double>(tmpSensor[0]) - static_cast<double>(origLoc[0]);
492 y_loc =
static_cast<double>(tmpSensor[1]) - static_cast<double>(origLoc[1]);
494 a_grid = (x_loc * v_loc[1] - y_loc * v_loc[0]) / det;
495 b_grid = (y_loc * u_loc[0] - x_loc * u_loc[1]) / det;
497 gridContiIndex[0] =
static_cast<double>(gridIndex[0]) + a_grid;
498 gridContiIndex[1] =
static_cast<double>(gridIndex[1]) + b_grid;
500 leftGrid->TransformContinuousIndexToPhysicalPoint(gridContiIndex, epiPosition);
502 horizDisp->TransformPhysicalPointToContinuousIndex(epiPosition,epiContiIndex);
504 if (0.0 < a_grid && a_grid < 1.0 && 0.0 < b_grid && b_grid < 1.0)
512 int a_grid_int =
static_cast<int>(gridIndex[0]) + static_cast<int>(vcl_floor(a_grid));
513 int b_grid_int =
static_cast<int>(gridIndex[1]) + static_cast<int>(vcl_floor(b_grid));
515 if (a_grid_int < minGridIndex[0]) a_grid_int = minGridIndex[0];
516 if (a_grid_int > maxGridIndex[0]) a_grid_int = maxGridIndex[0];
517 if (b_grid_int < minGridIndex[1]) b_grid_int = minGridIndex[1];
518 if (b_grid_int > maxGridIndex[1]) b_grid_int = maxGridIndex[1];
520 gridIndex[0] = a_grid_int;
521 gridIndex[1] = b_grid_int;
527 epiIndexMin[0] = epiContiIndex[0];
528 epiIndexMin[1] = epiContiIndex[1];
529 epiIndexMax[0] = epiContiIndex[0];
530 epiIndexMax[1] = epiContiIndex[1];
534 if (epiContiIndex[0] < epiIndexMin[0]) epiIndexMin[0] = epiContiIndex[0];
535 if (epiContiIndex[1] < epiIndexMin[1]) epiIndexMin[1] = epiContiIndex[1];
536 if (epiIndexMax[0] < epiContiIndex[0]) epiIndexMax[0] = epiContiIndex[0];
537 if (epiIndexMax[1] < epiContiIndex[1]) epiIndexMax[1] = epiContiIndex[1];
542 typename DisparityMapType::RegionType inputDisparityRegion;
543 inputDisparityRegion.SetIndex(0, static_cast<int>(vcl_floor(epiIndexMin[0])));
544 inputDisparityRegion.SetIndex(1, static_cast<int>(vcl_floor(epiIndexMin[1])));
545 inputDisparityRegion.SetSize(0, 1 + static_cast<unsigned int>(vcl_floor(epiIndexMax[0] - epiIndexMin[0] + 0.5)));
546 inputDisparityRegion.SetSize(1, 1 + static_cast<unsigned int>(vcl_floor(epiIndexMax[1] - epiIndexMin[1] + 0.5)));
549 if ( inputDisparityRegion.Crop(horizDisp->GetLargestPossibleRegion()))
551 horizDisp->SetRequestedRegion( inputDisparityRegion );
552 vertiDisp->SetRequestedRegion( inputDisparityRegion );
556 maskDisp->SetRequestedRegion( inputDisparityRegion );
562 typename DisparityMapType::RegionType emptyRegion;
564 emptyRegion.SetIndex(horizDisp->GetLargestPossibleRegion().GetIndex());
565 emptyRegion.SetSize(0,0);
566 emptyRegion.SetSize(1,0);
568 horizDisp->SetRequestedRegion( emptyRegion );
569 vertiDisp->SetRequestedRegion( emptyRegion );
572 maskDisp->SetRequestedRegion( emptyRegion );
581 horizDisp->SetRequestedRegion( inputDisparityRegion );
582 vertiDisp->SetRequestedRegion( inputDisparityRegion );
586 std::ostringstream msg;
587 msg << this->GetNameOfClass()
588 <<
"::GenerateInputRequestedRegion()";
590 e.
SetDescription(
"Requested region is (at least partially) outside the largest possible region of disparity map.");
597 template <
class TDisparityImage,
class TInputImage,
class TOutputDEMImage,
598 class TEpipolarGridImage,
class TMaskImage>
603 const TDisparityImage * horizDisp = this->GetHorizontalDisparityMapInput();
605 const TOutputDEMImage * outputDEM = this->GetDEMOutput();
607 typename DisparityMapType::RegionType requestedRegion = horizDisp->GetRequestedRegion();
609 m_UsedInputSplits = m_InputSplitter->GetNumberOfSplits(requestedRegion, this->GetNumberOfThreads());
612 if (requestedRegion.GetSize(0) == 0 && requestedRegion.GetSize(1) == 0)
614 m_UsedInputSplits = 0;
617 if (m_UsedInputSplits <= static_cast<unsigned int>(this->GetNumberOfThreads()))
619 m_TempDEMRegions.clear();
621 for (
unsigned int i=0; i<m_UsedInputSplits; i++)
623 typename DEMImageType::Pointer tmpImg = TOutputDEMImage::New();
624 tmpImg->SetNumberOfComponentsPerPixel(1);
625 tmpImg->SetRegions(outputDEM->GetRequestedRegion());
628 typename DEMImageType::PixelType minElevation =
static_cast<typename DEMImageType::PixelType
>(m_ElevationMin);
629 tmpImg->FillBuffer(minElevation);
631 m_TempDEMRegions.push_back(tmpImg);
636 itkExceptionMacro(<<
"Wrong number of splits for input multithreading : "<<m_UsedInputSplits);
640 template <
class TDisparityImage,
class TInputImage,
class TOutputDEMImage,
641 class TEpipolarGridImage,
class TMaskImage>
646 const TDisparityImage * horizDisp = this->GetHorizontalDisparityMapInput();
647 const TDisparityImage * vertiDisp = this->GetVerticalDisparityMapInput();
649 const TInputImage * leftSensor = this->GetLeftInput();
650 const TInputImage * rightSensor = this->GetRightInput();
652 const TMaskImage * disparityMask = this->GetDisparityMaskInput();
654 const TOutputDEMImage * outputDEM = this->GetDEMOutput();
659 leftToGroundTransform->SetInputKeywordList(leftSensor->GetImageKeywordlist());
660 rightToGroundTransform->SetInputKeywordList(rightSensor->GetImageKeywordlist());
662 leftToGroundTransform->InstanciateTransform();
663 rightToGroundTransform->InstanciateTransform();
665 const TEpipolarGridImage * leftGrid = this->GetLeftEpipolarGridInput();
666 const TEpipolarGridImage * rightGrid = this->GetRightEpipolarGridInput();
668 typename TEpipolarGridImage::RegionType gridRegion = leftGrid->GetLargestPossibleRegion();
670 TOutputDEMImage * tmpDEM =
NULL;
671 typename TOutputDEMImage::RegionType outputRequestedRegion = outputDEM->GetRequestedRegion();
673 typename TDisparityImage::RegionType disparityRegion;
674 if (static_cast<unsigned int>(threadId) < m_UsedInputSplits)
676 disparityRegion = m_InputSplitter->GetSplit(threadId,m_UsedInputSplits,horizDisp->GetRequestedRegion());
677 tmpDEM = m_TempDEMRegions[threadId];
690 bool useMask =
false;
699 typename TDisparityImage::PointType epiPoint;
701 double subPixIndex[2];
702 typename GridImageType::IndexType ulIndex, urIndex, lrIndex, llIndex;
703 typename GridImageType::PixelType ulPixel(2);
704 typename GridImageType::PixelType urPixel(2);
705 typename GridImageType::PixelType lrPixel(2);
706 typename GridImageType::PixelType llPixel(2);
707 typename GridImageType::PixelType cPixel(2);
709 typename GridImageType::PointType ulPoint;
710 typename GridImageType::PointType urPoint;
711 typename GridImageType::PointType lrPoint;
712 typename GridImageType::PointType llPoint;
725 if (!(maskIt.
Get() > 0))
735 horizDisp->TransformIndexToPhysicalPoint(horizIt.
GetIndex(),epiPoint);
736 leftGrid->TransformPhysicalPointToContinuousIndex(epiPoint,gridIndexConti);
738 ulIndex[0] =
static_cast<int>(vcl_floor(gridIndexConti[0]));
739 ulIndex[1] =
static_cast<int>(vcl_floor(gridIndexConti[1]));
740 if (ulIndex[0] < gridRegion.GetIndex(0)) ulIndex[0] = gridRegion.
GetIndex(0);
741 if (ulIndex[1] < gridRegion.GetIndex(1)) ulIndex[1] = gridRegion.GetIndex(1);
742 if (ulIndex[0] > (gridRegion.GetIndex(0) +
static_cast<int>(gridRegion.GetSize(0)) - 2))
744 ulIndex[0] = gridRegion.GetIndex(0) + gridRegion.GetSize(0) - 2;
746 if (ulIndex[1] > (gridRegion.GetIndex(1) +
static_cast<int>(gridRegion.GetSize(1)) - 2))
748 ulIndex[1] = gridRegion.GetIndex(1) + gridRegion.GetSize(1) - 2;
750 urIndex[0] = ulIndex[0] + 1;
751 urIndex[1] = ulIndex[1];
752 lrIndex[0] = ulIndex[0] + 1;
753 lrIndex[1] = ulIndex[1] + 1;
754 llIndex[0] = ulIndex[0];
755 llIndex[1] = ulIndex[1] + 1;
756 subPixIndex[0] = gridIndexConti[0] -
static_cast<double>(ulIndex[0]);
757 subPixIndex[1] = gridIndexConti[1] -
static_cast<double>(ulIndex[1]);
759 leftGrid->TransformIndexToPhysicalPoint(ulIndex, ulPoint);
760 leftGrid->TransformIndexToPhysicalPoint(urIndex, urPoint);
761 leftGrid->TransformIndexToPhysicalPoint(lrIndex, lrPoint);
762 leftGrid->TransformIndexToPhysicalPoint(llIndex, llPoint);
764 ulPixel[0] = (leftGrid->GetPixel(ulIndex))[0] + ulPoint[0];
765 ulPixel[1] = (leftGrid->GetPixel(ulIndex))[1] + ulPoint[1];
766 urPixel[0] = (leftGrid->GetPixel(urIndex))[0] + urPoint[0];
767 urPixel[1] = (leftGrid->GetPixel(urIndex))[1] + urPoint[1];
768 lrPixel[0] = (leftGrid->GetPixel(lrIndex))[0] + lrPoint[0];
769 lrPixel[1] = (leftGrid->GetPixel(lrIndex))[1] + lrPoint[1];
770 llPixel[0] = (leftGrid->GetPixel(llIndex))[0] + llPoint[0];
771 llPixel[1] = (leftGrid->GetPixel(llIndex))[1] + llPoint[1];
772 cPixel = (ulPixel * (1.0 - subPixIndex[0]) + urPixel * subPixIndex[0]) * (1.0 - subPixIndex[1]) +
773 (llPixel * (1.0 - subPixIndex[0]) + lrPixel * subPixIndex[0]) * subPixIndex[1];
775 sensorPoint[0] = cPixel[0];
776 sensorPoint[1] = cPixel[1];
777 sensorPoint[2] = m_ElevationMin;
778 leftGroundHmin = leftToGroundTransform->TransformPoint(sensorPoint);
780 sensorPoint[2] = m_ElevationMax;
781 leftGroundHmax = leftToGroundTransform->TransformPoint(sensorPoint);
785 rightIndexEstimate[0] =
static_cast<double>((horizIt.
GetIndex())[0]) +
static_cast<double>(horizIt.
Get());
786 rightIndexEstimate[1] =
static_cast<double>((horizIt.
GetIndex())[1]) +
static_cast<double>(vertiIt.
Get());
788 horizDisp->TransformContinuousIndexToPhysicalPoint(rightIndexEstimate,epiPoint);
789 rightGrid->TransformPhysicalPointToContinuousIndex(epiPoint,gridIndexConti);
791 ulIndex[0] =
static_cast<int>(vcl_floor(gridIndexConti[0]));
792 ulIndex[1] =
static_cast<int>(vcl_floor(gridIndexConti[1]));
793 if (ulIndex[0] < gridRegion.GetIndex(0)) ulIndex[0] = gridRegion.GetIndex(0);
794 if (ulIndex[1] < gridRegion.GetIndex(1)) ulIndex[1] = gridRegion.GetIndex(1);
795 if (ulIndex[0] > (gridRegion.GetIndex(0) +
static_cast<int>(gridRegion.GetSize(0)) - 2))
797 ulIndex[0] = gridRegion.GetIndex(0) + gridRegion.GetSize(0) - 2;
799 if (ulIndex[1] > (gridRegion.GetIndex(1) +
static_cast<int>(gridRegion.GetSize(1)) - 2))
801 ulIndex[1] = gridRegion.GetIndex(1) + gridRegion.GetSize(1) - 2;
803 urIndex[0] = ulIndex[0] + 1;
804 urIndex[1] = ulIndex[1];
805 lrIndex[0] = ulIndex[0] + 1;
806 lrIndex[1] = ulIndex[1] + 1;
807 llIndex[0] = ulIndex[0];
808 llIndex[1] = ulIndex[1] + 1;
809 subPixIndex[0] = gridIndexConti[0] -
static_cast<double>(ulIndex[0]);
810 subPixIndex[1] = gridIndexConti[1] -
static_cast<double>(ulIndex[1]);
812 rightGrid->TransformIndexToPhysicalPoint(ulIndex, ulPoint);
813 rightGrid->TransformIndexToPhysicalPoint(urIndex, urPoint);
814 rightGrid->TransformIndexToPhysicalPoint(lrIndex, lrPoint);
815 rightGrid->TransformIndexToPhysicalPoint(llIndex, llPoint);
817 ulPixel[0] = (rightGrid->GetPixel(ulIndex))[0] + ulPoint[0];
818 ulPixel[1] = (rightGrid->GetPixel(ulIndex))[1] + ulPoint[1];
819 urPixel[0] = (rightGrid->GetPixel(urIndex))[0] + urPoint[0];
820 urPixel[1] = (rightGrid->GetPixel(urIndex))[1] + urPoint[1];
821 lrPixel[0] = (rightGrid->GetPixel(lrIndex))[0] + lrPoint[0];
822 lrPixel[1] = (rightGrid->GetPixel(lrIndex))[1] + lrPoint[1];
823 llPixel[0] = (rightGrid->GetPixel(llIndex))[0] + llPoint[0];
824 llPixel[1] = (rightGrid->GetPixel(llIndex))[1] + llPoint[1];
825 cPixel = (ulPixel * (1.0 - subPixIndex[0]) + urPixel * subPixIndex[0]) * (1.0 - subPixIndex[1]) +
826 (llPixel * (1.0 - subPixIndex[0]) + lrPixel * subPixIndex[0]) * subPixIndex[1];
828 sensorPoint[0] = cPixel[0];
829 sensorPoint[1] = cPixel[1];
830 sensorPoint[2] = m_ElevationMin;
831 rightGroundHmin = rightToGroundTransform->TransformPoint(sensorPoint);
833 sensorPoint[2] = m_ElevationMax;
834 rightGroundHmax = rightToGroundTransform->TransformPoint(sensorPoint);
837 double a = (leftGroundHmax[0] - leftGroundHmin[0]) * (leftGroundHmax[0] - leftGroundHmin[0]) +
838 (leftGroundHmax[1] - leftGroundHmin[1]) * (leftGroundHmax[1] - leftGroundHmin[1]) +
839 (leftGroundHmax[2] - leftGroundHmin[2]) * (leftGroundHmax[2] - leftGroundHmin[2]);
840 double b = (rightGroundHmax[0] - rightGroundHmin[0]) * (rightGroundHmax[0] - rightGroundHmin[0]) +
841 (rightGroundHmax[1] - rightGroundHmin[1]) * (rightGroundHmax[1] - rightGroundHmin[1]) +
842 (rightGroundHmax[2] - rightGroundHmin[2]) * (rightGroundHmax[2] - rightGroundHmin[2]);
843 double c = -(leftGroundHmax[0] - leftGroundHmin[0]) * (rightGroundHmax[0] - rightGroundHmin[0])
844 -(leftGroundHmax[1] - leftGroundHmin[1]) * (rightGroundHmax[1] - rightGroundHmin[1])
845 -(leftGroundHmax[2] - leftGroundHmin[2]) * (rightGroundHmax[2] - rightGroundHmin[2]);
846 double g = (leftGroundHmax[0] - leftGroundHmin[0]) * (rightGroundHmin[0] - leftGroundHmin[0]) +
847 (leftGroundHmax[1] - leftGroundHmin[1]) * (rightGroundHmin[1] - leftGroundHmin[1]) +
848 (leftGroundHmax[2] - leftGroundHmin[2]) * (rightGroundHmin[2] - leftGroundHmin[2]);
849 double h = -(rightGroundHmax[0] - rightGroundHmin[0]) * (rightGroundHmin[0] - leftGroundHmin[0])
850 -(rightGroundHmax[1] - rightGroundHmin[1]) * (rightGroundHmin[1] - leftGroundHmin[1])
851 -(rightGroundHmax[2] - rightGroundHmin[2]) * (rightGroundHmin[2] - leftGroundHmin[2]);
853 double rLeft = (b * g - c * h) / (a * b - c * c);
854 double rRight = (a * h - c * g) / (a * b - c * c);
866 typename DEMImageType::PointType midPoint2D;
867 midPoint2D[0] = midPoint3D[0];
868 midPoint2D[1] = midPoint3D[1];
870 outputDEM->TransformPhysicalPointToContinuousIndex(midPoint2D,midIndex);
871 typename DEMImageType::IndexType cellIndex;
872 cellIndex[0] =
static_cast<int>(vcl_floor(midIndex[0] + 0.5));
873 cellIndex[1] =
static_cast<int>(vcl_floor(midIndex[1] + 0.5));
875 if (outputRequestedRegion.IsInside(cellIndex))
882 if (cellHeight > tmpDEM->GetPixel(cellIndex) && cellHeight < static_cast<DEMPixelType>(m_ElevationMax))
884 tmpDEM->SetPixel(cellIndex,cellHeight);
891 if (useMask) ++maskIt;
897 template <
class TDisparityImage,
class TInputImage,
class TOutputDEMImage,
898 class TEpipolarGridImage,
class TMaskImage>
903 TOutputDEMImage * outputDEM = this->GetDEMOutput();
905 if (m_TempDEMRegions.size() < 1)
907 outputDEM->FillBuffer(m_ElevationMin);
915 firstDEMIt.GoToBegin();
918 while (!outputDEMIt.IsAtEnd() && !firstDEMIt.IsAtEnd())
920 outputDEMIt.Set(firstDEMIt.Get());
926 for (
unsigned int i=1; i<m_TempDEMRegions.size(); i++)
931 tmpDEMIt.GoToBegin();
933 while (!outputDEMIt.IsAtEnd() && !tmpDEMIt.IsAtEnd())
935 if (tmpDEMIt.Get() > outputDEMIt.Get())
937 outputDEMIt.Set(tmpDEMIt.Get());