otbStreamingGraphToImageFilter.txx 5.19 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * otbStreamingGraphToImageFilter.txx
 *
 *  Created on: 6 nov. 2017
 *      Author: cresson
 */

#ifndef MODULES_REMOTE_LSGRM_INCLUDE_OTBSTREAMINGGRAPHTOIMAGEFILTER_TXX_
#define MODULES_REMOTE_LSGRM_INCLUDE_OTBSTREAMINGGRAPHTOIMAGEFILTER_TXX_

#include <otbStreamingGraphToImageFilter.h>

namespace lsgrm
{

template <class TGraph, class TLabelImage>
void
StreamingGraphToImageFilter<TGraph, TLabelImage>
::SetGraph(const TGraph graph)
 {
  m_Graph = graph;

  // Build a R-Tree
24 25
  itkDebugMacro( << "Building R-Tree" );

26 27 28 29 30
  rtree.clear();
  unsigned int count = 0;
  for(auto& node : m_Graph.m_Nodes)
    {
      // create a box
31 32 33 34 35 36 37
      box b(
          point(
              node->m_Bbox.m_UX,
              node->m_Bbox.m_UY),
          point(
              node->m_Bbox.m_UX + node->m_Bbox.m_W,
              node->m_Bbox.m_UY + node->m_Bbox.m_H));
38 39 40 41 42 43

      // insert new value
      rtree.insert(std::make_pair(b, count));
      count++;
    }

44
  itkDebugMacro( << "Building R-Tree finished" );
45 46 47 48 49 50 51 52

 }

template <class TGraph, class TLabelImage>
void
StreamingGraphToImageFilter<TGraph, TLabelImage>
::GenerateOutputInformation()
 {
53 54 55
  itkDebugMacro( << "Entering GenerateOutputInformation()" );

  // Output Largest Possible Region
56 57 58
  IndexType index;
  index.Fill(0);
  RegionType outputRegion(index, m_OutputSize);
59 60 61 62 63

  // Set output informations
  TLabelImage * outputPtr = this->GetOutput();
  outputPtr->SetOrigin ( m_OutputOrigin );
  outputPtr->SetSpacing ( m_OutputSpacing );
64
  outputPtr->SetLargestPossibleRegion( outputRegion );
65
  outputPtr->SetProjectionRef(m_OutputProjectionRef);
66 67 68 69 70 71 72 73 74

 }


template <class TGraph, class TLabelImage>
void
StreamingGraphToImageFilter<TGraph, TLabelImage>
::GenerateData()
 {
75 76
  itkDebugMacro( << "Entering GenerateData()" );

77 78 79 80 81 82 83
  // Allocate the output buffer
  TLabelImage * outputPtr = this->GetOutput();
  RegionType outReqRegion = outputPtr->GetRequestedRegion();
  outputPtr->SetBufferedRegion(outputPtr->GetRequestedRegion());
  outputPtr->Allocate();

  // Find nodes intersecting find the output requested region
84 85 86 87 88 89 90
  box query_box(
      point(
          outReqRegion.GetIndex(0),
          outReqRegion.GetIndex(1)),
      point(
          outReqRegion.GetIndex(0)+outReqRegion.GetSize(0),
          outReqRegion.GetIndex(1)+outReqRegion.GetSize(1)));
91
  std::vector<value> result_s;
92
  itkDebugMacro( << "R-Tree query on output requested region " << outReqRegion );
93
  rtree.query(bgi::intersects(query_box), std::back_inserter(result_s));
94
  itkDebugMacro( << "R-Tree query done. Number of nodes: " << result_s.size() );
95 96 97 98 99 100 101 102 103 104 105 106 107 108

  // Retrieve the bounding box of the intersecting nodes (a kind of "Input requested region")
  box realBBox(query_box);
  for(auto& res : result_s)
    {
      boost::geometry::expand(realBBox, res.first);
    }
  IndexType index;
  index[0] = realBBox.min_corner().get<0>();
  index[1] = realBBox.min_corner().get<1>();
  SizeType size;
  size[0] = realBBox.max_corner().get<0>() - realBBox.min_corner().get<0>();
  size[1] = realBBox.max_corner().get<1>() - realBBox.min_corner().get<1>();
  RegionType inputRequestedRegion(index, size);
109
  itkDebugMacro( << "Input Requested region: " << inputRequestedRegion );
110 111

  // Generate the label image
112
  itkDebugMacro( << "Allocate buffered region ");
113 114 115 116 117
  const typename TLabelImage::InternalPixelType noDataLabel = 0;
  typename TLabelImage::Pointer labelImage = TLabelImage::New();
  labelImage->SetRegions(inputRequestedRegion);
  labelImage->Allocate();
  labelImage->FillBuffer(noDataLabel);
118
  itkDebugMacro( << "Allocate buffered region ok" );
119 120 121 122

  using LabelImageIterator = itk::ImageRegionIterator<TLabelImage>;
  LabelImageIterator it(labelImage, inputRequestedRegion);

123 124
  // Burn boundaries
  itkDebugMacro( << "Burning boundaries " );
125 126 127 128 129 130 131 132 133
  for(auto& res : result_s)
    {
      NodePointerType node = m_Graph.m_Nodes[res.second];

      lp::CellLists borderPixels;
      lp::ContourOperations::GenerateBorderCells(borderPixels, node->m_Contour, node->m_Id, m_OutputSize[0]);

      for (auto& pix: borderPixels)
        {
134 135 136 137
		  // patch raf (catch exception if position of pixel to set is not correct)
		  try {
          index[0] = (unsigned int)(pix % (std::size_t)(m_OutputSize[0]));
          index[1] = (unsigned int)(pix / (std::size_t)(m_OutputSize[0]));
138
          labelImage->SetPixel(index, res.second + 1);
139 140 141 142 143 144
		  } catch (std::exception e) {
			  std::cout << "Pixel ID: " << pix << std::endl;
			  std::cout << "Derived index: " << index[0] << "," << index[1] << std::endl;
			  std::cout << "Node position in list: " << res.second << std::endl;
		  }
		  // end patch
145 146
        }
    }
147
  itkDebugMacro( << "Burning boundaries ok ");
148 149

  // Fill holes
150
  itkDebugMacro( << "fill Hole filter" );
151 152 153
  typename FillholeFilterType::Pointer fillFilter = FillholeFilterType::New();
  fillFilter->SetInput(labelImage);
  fillFilter->Update();
154 155
  itkDebugMacro( << "Fill Hole filter OutputLargestPossibleRegion:" << fillFilter->GetOutput()->GetLargestPossibleRegion() );
  itkDebugMacro( << "fill Hole filter ok" );
156 157 158 159 160 161 162 163 164 165 166 167 168

  // Extract the stable region
  LabelImageIterator outIt(outputPtr, outReqRegion);
  LabelImageIterator inIt (fillFilter->GetOutput(), outReqRegion);
  for (inIt.GoToBegin(), outIt.GoToBegin(); !outIt.IsAtEnd(); ++outIt, ++inIt)
    outIt.Set(inIt.Get());
 }



} /* namespace lsgrm */

#endif