diff --git a/include/grmGraphToOtbImage.txx b/include/grmGraphToOtbImage.txx index 649354386791798154c5b7b792635d05c7a0e42b..7303b195a7bf0679593cd0cf1bf198ce828c110f 100644 --- a/include/grmGraphToOtbImage.txx +++ b/include/grmGraphToOtbImage.txx @@ -19,6 +19,7 @@ #define __GRM_GRAPH_TO_OTBIMAGE_TXX #include "grmGraphToOtbImage.h" #include "itkImageRegionIterator.h" +#include "itkGrayscaleFillholeImageFilter.h" namespace grm { @@ -40,11 +41,7 @@ namespace grm LabelImageType::Pointer label_img = LabelImageType::New(); label_img->SetRegions(region); label_img->Allocate(); - - using LabelImageIterator = itk::ImageRegionIterator<LabelImageType>; - LabelImageIterator it(label_img, label_img->GetLargestPossibleRegion()); - for(it.GoToBegin();!it.IsAtEnd(); ++it) - it.Set(0); + label_img->FillBuffer(0); // Start at 1 (value 0 can be used for invalid pixels) unsigned int label = 1; @@ -62,17 +59,13 @@ namespace grm ++label; } - unsigned int pixelValue = 0; - for(it.GoToBegin(); !it.IsAtEnd(); ++it) - { - auto pixel = it.Get(); - if(pixel == 0) - it.Set(pixelValue); - else - pixelValue = pixel; - } + // Fill holes + typedef itk::GrayscaleFillholeImageFilter<LabelImageType,LabelImageType> FillholeFilterType; + FillholeFilterType::Pointer fillFilter = FillholeFilterType::New(); + fillFilter->SetInput(label_img); + fillFilter->Update(); - return label_img; + return fillFilter->GetOutput(); } template<class TGraph> @@ -96,48 +89,31 @@ namespace grm clusterImg->Allocate(); ClusteredImageType::PixelType pixelValue; - pixelValue.Reserve(3); - pixelValue[0] = 255; - pixelValue[1] = 255; - pixelValue[2] = 255; + pixelValue.SetSize(3); + pixelValue.Fill(255); - using ClusterImageIterator = itk::ImageRegionIterator<ClusteredImageType>; - ClusterImageIterator it(clusterImg, clusterImg->GetLargestPossibleRegion()); - for(it.GoToBegin();!it.IsAtEnd(); ++it) - it.Set(pixelValue); + clusterImg->FillBuffer(pixelValue); + LabelImageType::Pointer label_img = this->GetLabelImage(graph, width, height); - srand(time(NULL)); - unsigned char c1, c2, c3; - for(auto& node : graph.m_Nodes) - { - auto rv = rand() % 255; - c1 = rv;//rand() % 256; - c2 = rv;//rand() % 256; - c3 = rv;//rand() % 256; + typedef typename itk::ImageRegionConstIterator<LabelImageType> LabelImageIteratorType; + typedef typename itk::ImageRegionIterator<ClusteredImageType> ClusteredImageIteratorType; - lp::CellLists borderPixels; - ContourOperator::GenerateBorderCells(borderPixels, node->m_Contour, node->m_Id, width); - - for (auto& pix : borderPixels) - { - index[0] = pix % width; - index[1] = pix / width; - pixelValue[0] = c1; - pixelValue[1] = c2; - pixelValue[2] = c3; - clusterImg->SetPixel(index, pixelValue); - } - } + LabelImageIteratorType it1 (label_img, region); + ClusteredImageIteratorType it2 (clusterImg, region); - - for(it.GoToBegin(); !it.IsAtEnd(); ++it) - { - auto pixel = it.Get(); - if(pixel[0] == 255 && pixel[1] == 255 && pixel[2] == 255) - it.Set(pixelValue); - else - pixelValue = pixel; - } + // Generate a random color vector + srand(time(NULL)); + std::vector<double> colorMap; + colorMap.reserve(graph.m_Nodes.size()); + std::generate_n(std::back_inserter(colorMap), graph.m_Nodes.size(), rand); + + for (it1.GoToBegin(), it2.GoToBegin() ; !it1.IsAtEnd() ; ++it1, ++it2) + { + pixelValue[0] = colorMap[it1.Get()]; + pixelValue[1] = (16807 * pixelValue[0]) % 255 ; + pixelValue[2] = (16807 * pixelValue[1]) % 255 ; + it2.Set(pixelValue); + } return clusterImg; }