Commit 65f3020d8748165ae2655a5b658dc36b4a9d854b

Authored by Pierre Lassalle
1 parent d8ebd795
Exists in master

Fix bug generation of labels

Code/lsrmGraphToOtbImage.h
... ... @@ -35,9 +35,9 @@ namespace lsrm
35 35 const unsigned int width,
36 36 const unsigned int height);
37 37  
38   - ClusteredImageType::Pointer GetClusteredOutput(const GraphType& graph,
  38 + /*ClusteredImageType::Pointer GetClusteredOutput(const GraphType& graph,
39 39 const unsigned int width,
40   - const unsigned int height);
  40 + const unsigned int height);*/
41 41 };
42 42  
43 43 } // end of namespace lsrm
... ...
Code/lsrmGraphToOtbImage.h~ 0 → 100644
... ... @@ -0,0 +1,45 @@
  1 +#ifndef __LSRM_GRAPH_TO_OTBIMAGE_H
  2 +#define __LSRM_GRAPH_TO_OTBIMAGE_H
  3 +#include <itkRGBPixel.h>
  4 +#include <itkImageRegion.h>
  5 +#include <otbImage.h>
  6 +#include <otbVectorImage.h>
  7 +#include <otbImageFileReader.h>
  8 +#include <otbImageFileWriter.h>
  9 +#include "lsrmGraph.h"
  10 +#include <string>
  11 +#include <stdlib.h>
  12 +#include <time.h>
  13 +#include "lpContour.h"
  14 +
  15 +namespace lsrm
  16 +{
  17 + template<class TGraph>
  18 + class GraphToOtbImage
  19 + {
  20 + public:
  21 +
  22 + /* Some convenient typedefs */
  23 + typedef TGraph GraphType;
  24 + typedef typename GraphType::NodeType NodeType;
  25 + typedef std::vector< std::shared_ptr<NodeType> > NodeList;
  26 + typedef typename NodeList::const_iterator NodeConstIterator;
  27 + typedef unsigned long int LabelPixelType;
  28 + typedef otb::Image<LabelPixelType, 2> LabelImageType;
  29 + typedef unsigned char ClusterPixelType;
  30 + typedef otb::VectorImage<ClusterPixelType, 2> ClusteredImageType;
  31 + using ContourOperator = lp::ContourOperations;
  32 +
  33 +
  34 + LabelImageType::Pointer GetLabelImage(const GraphType& graph,
  35 + const unsigned int width,
  36 + const unsigned int height);
  37 +
  38 + ClusteredImageType::Pointer GetClusteredOutput(const GraphType& graph,
  39 + const unsigned int width,
  40 + const unsigned int height);
  41 + };
  42 +
  43 +} // end of namespace lsrm
  44 +#include "lsrmGraphToOtbImage.txx"
  45 +#endif
... ...
Code/lsrmGraphToOtbImage.txx
... ... @@ -2,6 +2,7 @@
2 2 #define __LSRM_GRAPH_TO_OTBIMAGE_TXX
3 3 #include "lsrmGraphToOtbImage.h"
4 4 #include "itkImageRegionIterator.h"
  5 +#include "itkGrayscaleFillholeImageFilter.h"
5 6  
6 7 namespace lsrm
7 8 {
... ... @@ -45,20 +46,16 @@ namespace lsrm
45 46 ++label;
46 47 }
47 48  
48   - long unsigned int pixelValue = 0;
49   - for(it.GoToBegin(); !it.IsAtEnd(); ++it)
50   - {
51   - auto pixel = it.Get();
52   - if(pixel == 0)
53   - it.Set(pixelValue);
54   - else
55   - pixelValue = pixel;
56   - }
  49 + // Fill holes
  50 + typedef itk::GrayscaleFillholeImageFilter<LabelImageType,LabelImageType> FillholeFilterType;
  51 + FillholeFilterType::Pointer fillFilter = FillholeFilterType::New();
  52 + fillFilter->SetInput(label_img);
  53 + fillFilter->Update();
57 54  
58   - return label_img;
  55 + return fillFilter->GetOutput();
59 56 }
60 57  
61   - template<class TGraph>
  58 + /*template<class TGraph>
62 59 typename GraphToOtbImage<TGraph>::ClusteredImageType::Pointer
63 60 GraphToOtbImage<TGraph>::GetClusteredOutput(const GraphType& graph,
64 61 const unsigned int width,
... ... @@ -122,7 +119,7 @@ namespace lsrm
122 119 }
123 120  
124 121 return clusterImg;
125   - }
  122 + }*/
126 123  
127 124 } // end of namespace lsrm
128 125 #endif
... ...
Code/lsrmGraphToOtbImage.txx~ 0 → 100644
... ... @@ -0,0 +1,125 @@
  1 +#ifndef __LSRM_GRAPH_TO_OTBIMAGE_TXX
  2 +#define __LSRM_GRAPH_TO_OTBIMAGE_TXX
  3 +#include "lsrmGraphToOtbImage.h"
  4 +#include "itkImageRegionIterator.h"
  5 +#include "itkGrayscaleFillholeImageFilter.h"
  6 +
  7 +namespace lsrm
  8 +{
  9 + template<class TGraph>
  10 + typename GraphToOtbImage<TGraph>::LabelImageType::Pointer
  11 + GraphToOtbImage<TGraph>::GetLabelImage(const GraphType& graph,
  12 + const unsigned int width,
  13 + const unsigned int height)
  14 + {
  15 + LabelImageType::IndexType index;
  16 + LabelImageType::SizeType size;
  17 + LabelImageType::RegionType region;
  18 +
  19 + index[0] = 0; index[1] = 0;
  20 + size[0] = width; size[1] = height;
  21 + region.SetIndex(index);
  22 + region.SetSize(size);
  23 +
  24 + LabelImageType::Pointer label_img = LabelImageType::New();
  25 + label_img->SetRegions(region);
  26 + label_img->Allocate();
  27 +
  28 + using LabelImageIterator = itk::ImageRegionIterator<LabelImageType>;
  29 + LabelImageIterator it(label_img, label_img->GetLargestPossibleRegion());
  30 + for(it.GoToBegin();!it.IsAtEnd(); ++it)
  31 + it.Set(0);
  32 +
  33 + // Start at 1 (value 0 can be used for invalid pixels)
  34 + long unsigned int label = 1;
  35 + for(auto& region : graph.m_Nodes)
  36 + {
  37 + lp::CellLists borderPixels;
  38 + ContourOperator::GenerateBorderCells(borderPixels, region->m_Contour, region->m_Id, width);
  39 +
  40 + for (auto& pix: borderPixels)
  41 + {
  42 + index[0] = pix % width;
  43 + index[1] = pix / width;
  44 + label_img->SetPixel(index, label);
  45 + }
  46 + ++label;
  47 + }
  48 +
  49 + // Fill holes
  50 + typedef itk::GrayscaleFillholeImageFilter<LabelImageType,LabelImageType> FillholeFilterType;
  51 + FillholeFilterType::Pointer fillFilter = FillholeFilterType::New();
  52 + fillFilter->SetInput(label_img);
  53 + fillFilter->Update();
  54 +
  55 + return fillFilter->GetOutput();
  56 + }
  57 +
  58 + template<class TGraph>
  59 + typename GraphToOtbImage<TGraph>::ClusteredImageType::Pointer
  60 + GraphToOtbImage<TGraph>::GetClusteredOutput(const GraphType& graph,
  61 + const unsigned int width,
  62 + const unsigned int height)
  63 + {
  64 + ClusteredImageType::IndexType index;
  65 + ClusteredImageType::SizeType size;
  66 + ClusteredImageType::RegionType region;
  67 +
  68 + index[0] = 0; index[1] = 0;
  69 + size[0] = width; size[1] = height;
  70 + region.SetIndex(index);
  71 + region.SetSize(size);
  72 +
  73 + ClusteredImageType::Pointer clusterImg = ClusteredImageType::New();
  74 + clusterImg->SetRegions(region);
  75 + clusterImg->SetNumberOfComponentsPerPixel(3);
  76 + clusterImg->Allocate();
  77 +
  78 + ClusteredImageType::PixelType pixelValue;
  79 + pixelValue.Reserve(3);
  80 + pixelValue[0] = 0;
  81 + pixelValue[1] = 0;
  82 + pixelValue[2] = 0;
  83 +
  84 + using ClusterImageIterator = itk::ImageRegionIterator<ClusteredImageType>;
  85 + ClusterImageIterator it(clusterImg, clusterImg->GetLargestPossibleRegion());
  86 + for(it.GoToBegin();!it.IsAtEnd(); ++it)
  87 + it.Set(pixelValue);
  88 +
  89 + srand(time(NULL));
  90 + unsigned char c1, c2, c3;
  91 + for(auto& region : graph.m_Nodes)
  92 + {
  93 + c1 = rand() % 256;
  94 + c2 = rand() % 256;
  95 + c3 = rand() % 256;
  96 +
  97 + lp::CellLists borderPixels;
  98 + ContourOperator::GenerateBorderCells(borderPixels, region->m_Contour, region->m_Id, width);
  99 +
  100 + for (auto& pix : borderPixels)
  101 + {
  102 + index[0] = pix % width;
  103 + index[1] = pix / width;
  104 + pixelValue[0] = c1;
  105 + pixelValue[1] = c2;
  106 + pixelValue[2] = c3;
  107 + clusterImg->SetPixel(index, pixelValue);
  108 + }
  109 + }
  110 +
  111 +
  112 + for(it.GoToBegin(); !it.IsAtEnd(); ++it)
  113 + {
  114 + auto pixel = it.Get();
  115 + if(pixel[0] == 0 && pixel[1] == 0 && pixel[2] == 0)
  116 + it.Set(pixelValue);
  117 + else
  118 + pixelValue = pixel;
  119 + }
  120 +
  121 + return clusterImg;
  122 + }
  123 +
  124 +} // end of namespace lsrm
  125 +#endif
... ...