Commit 693f6db6e43de399fc3ccfb214fdc9c58911ed69

Authored by Rémi Cresson (IRSTEA)
1 parent b9f39c5f
Exists in master

Fix polygon filling process

Showing 1 changed file with 29 additions and 53 deletions   Show diff stats
include/grmGraphToOtbImage.txx
... ... @@ -19,6 +19,7 @@
19 19 #define __GRM_GRAPH_TO_OTBIMAGE_TXX
20 20 #include "grmGraphToOtbImage.h"
21 21 #include "itkImageRegionIterator.h"
  22 +#include "itkGrayscaleFillholeImageFilter.h"
22 23  
23 24 namespace grm
24 25 {
... ... @@ -40,11 +41,7 @@ namespace grm
40 41 LabelImageType::Pointer label_img = LabelImageType::New();
41 42 label_img->SetRegions(region);
42 43 label_img->Allocate();
43   -
44   - using LabelImageIterator = itk::ImageRegionIterator<LabelImageType>;
45   - LabelImageIterator it(label_img, label_img->GetLargestPossibleRegion());
46   - for(it.GoToBegin();!it.IsAtEnd(); ++it)
47   - it.Set(0);
  44 + label_img->FillBuffer(0);
48 45  
49 46 // Start at 1 (value 0 can be used for invalid pixels)
50 47 unsigned int label = 1;
... ... @@ -62,17 +59,13 @@ namespace grm
62 59 ++label;
63 60 }
64 61  
65   - unsigned int pixelValue = 0;
66   - for(it.GoToBegin(); !it.IsAtEnd(); ++it)
67   - {
68   - auto pixel = it.Get();
69   - if(pixel == 0)
70   - it.Set(pixelValue);
71   - else
72   - pixelValue = pixel;
73   - }
  62 + // Fill holes
  63 + typedef itk::GrayscaleFillholeImageFilter<LabelImageType,LabelImageType> FillholeFilterType;
  64 + FillholeFilterType::Pointer fillFilter = FillholeFilterType::New();
  65 + fillFilter->SetInput(label_img);
  66 + fillFilter->Update();
74 67  
75   - return label_img;
  68 + return fillFilter->GetOutput();
76 69 }
77 70  
78 71 template<class TGraph>
... ... @@ -96,48 +89,31 @@ namespace grm
96 89 clusterImg->Allocate();
97 90  
98 91 ClusteredImageType::PixelType pixelValue;
99   - pixelValue.Reserve(3);
100   - pixelValue[0] = 255;
101   - pixelValue[1] = 255;
102   - pixelValue[2] = 255;
  92 + pixelValue.SetSize(3);
  93 + pixelValue.Fill(255);
103 94  
104   - using ClusterImageIterator = itk::ImageRegionIterator<ClusteredImageType>;
105   - ClusterImageIterator it(clusterImg, clusterImg->GetLargestPossibleRegion());
106   - for(it.GoToBegin();!it.IsAtEnd(); ++it)
107   - it.Set(pixelValue);
  95 + clusterImg->FillBuffer(pixelValue);
  96 + LabelImageType::Pointer label_img = this->GetLabelImage(graph, width, height);
108 97  
109   - srand(time(NULL));
110   - unsigned char c1, c2, c3;
111   - for(auto& node : graph.m_Nodes)
112   - {
113   - auto rv = rand() % 255;
114   - c1 = rv;//rand() % 256;
115   - c2 = rv;//rand() % 256;
116   - c3 = rv;//rand() % 256;
  98 + typedef typename itk::ImageRegionConstIterator<LabelImageType> LabelImageIteratorType;
  99 + typedef typename itk::ImageRegionIterator<ClusteredImageType> ClusteredImageIteratorType;
117 100  
118   - lp::CellLists borderPixels;
119   - ContourOperator::GenerateBorderCells(borderPixels, node->m_Contour, node->m_Id, width);
120   -
121   - for (auto& pix : borderPixels)
122   - {
123   - index[0] = pix % width;
124   - index[1] = pix / width;
125   - pixelValue[0] = c1;
126   - pixelValue[1] = c2;
127   - pixelValue[2] = c3;
128   - clusterImg->SetPixel(index, pixelValue);
129   - }
130   - }
  101 + LabelImageIteratorType it1 (label_img, region);
  102 + ClusteredImageIteratorType it2 (clusterImg, region);
131 103  
132   -
133   - for(it.GoToBegin(); !it.IsAtEnd(); ++it)
134   - {
135   - auto pixel = it.Get();
136   - if(pixel[0] == 255 && pixel[1] == 255 && pixel[2] == 255)
137   - it.Set(pixelValue);
138   - else
139   - pixelValue = pixel;
140   - }
  104 + // Generate a random color vector
  105 + srand(time(NULL));
  106 + std::vector<double> colorMap;
  107 + colorMap.reserve(graph.m_Nodes.size());
  108 + std::generate_n(std::back_inserter(colorMap), graph.m_Nodes.size(), rand);
  109 +
  110 + for (it1.GoToBegin(), it2.GoToBegin() ; !it1.IsAtEnd() ; ++it1, ++it2)
  111 + {
  112 + pixelValue[0] = colorMap[it1.Get()];
  113 + pixelValue[1] = (16807 * pixelValue[0]) % 255 ;
  114 + pixelValue[2] = (16807 * pixelValue[1]) % 255 ;
  115 + it2.Set(pixelValue);
  116 + }
141 117  
142 118 return clusterImg;
143 119 }
... ...