diff --git a/Code/lsgrmGraphOperations.h b/Code/lsgrmGraphOperations.h index 4352c89afaf3a5c25bd5a624b0824a95ad604ed3..862f0e1534569d7e7908bcd8fe1193a0e6799343 100644 --- a/Code/lsgrmGraphOperations.h +++ b/Code/lsgrmGraphOperations.h @@ -2,6 +2,7 @@ #define __LSGRM_GRAPH_OPERATIONS_H #include "lsgrmHeader.h" #include "lsrmGraphOperations.h" +#include "otbImage.h" #include "otbImageFileReader.h" namespace lsgrm diff --git a/Code/lsgrmGraphOperations.txx b/Code/lsgrmGraphOperations.txx index be32e32641639ac57d109738b10d4d405723cb42..02a9b002832c2e7b186aa9c2fb33e6f0e10b3750 100644 --- a/Code/lsgrmGraphOperations.txx +++ b/Code/lsgrmGraphOperations.txx @@ -79,6 +79,7 @@ namespace lsgrm } } +<<<<<<< HEAD if (myrank == 0) { std::cout << "\nAchieve segmentation process..." << std::endl; @@ -106,6 +107,31 @@ namespace lsgrm clusterWriter->Update(); } } +======= + std::cout << "\nAchieve segmentation process..." << std::endl; + + // Segmentation of the graph + segmenter.SetImageWidth(imageWidth); + segmenter.SetImageHeight(imageHeight); + segmenter.SetNumberOfComponentsPerPixel(imageBands); + segmenter.SetParam(params); + segmenter.SetThreshold(threshold); + segmenter.SetDoBFSegmentation(true); + segmenter.SetNumberOfIterations(75); + + lsrm::GraphOperations<TSegmenter>::PerfomAllIterationsWithLMBFAndConstThreshold(segmenter); + + // Write output graph to the output graph directory + WriteGraph<TSegmenter>(segmenter.m_Graph, outputGraphDirectory, 0, 0); + + typedef unsigned long int LabelPixelType; + typedef otb::Image<LabelPixelType, 2> LabelImageType; + typedef otb::ImageFileWriter<LabelImageType> LabelImageWriterType; + auto labelWriter = LabelImageWriterType::New(); + labelWriter->SetFileName("out/labelImage.tiff"); + labelWriter->SetInput(segmenter.GetLabeledClusteredOutput()); + labelWriter->Update(); +>>>>>>> upstream/master } template<class TSegmenter> diff --git a/Code/lsrmBaatzSegmenter.txx b/Code/lsrmBaatzSegmenter.txx index f751ccd1208a6b7cf24185712bf33a08b629f3ac..abc0273b14c2737509515bf07190535f087ac1fe 100644 --- a/Code/lsrmBaatzSegmenter.txx +++ b/Code/lsrmBaatzSegmenter.txx @@ -57,7 +57,7 @@ namespace lsrm std = std::sqrt((square_mean - 2*mean*sum + a_sum * mean* mean) / a_sum); spect_cost += (a_sum * std - a1 * n1->m_Std[b] - a2 * n2->m_Std[b]); } - spect_cost *= this->m_Param.m_ShapeWeight; + spect_cost *= this->m_Param.m_SpectralWeight; if(spect_cost < this->m_Threshold) { @@ -79,7 +79,7 @@ namespace lsrm shape_cost = this->m_Param.m_ShapeWeight * compact_f + (1-this->m_Param.m_ShapeWeight) * smooth_f; - return (spect_cost + (1-this->m_Param.m_ShapeWeight)*shape_cost); + return (spect_cost + (1-this->m_Param.m_SpectralWeight)*shape_cost); } else return spect_cost; diff --git a/Code/lsrmGraphToOtbImage.h b/Code/lsrmGraphToOtbImage.h index 89ef969ce259a05f70f6c70432d40fab054654cf..19a448caedebff6c02fc2a8fd3cdaa1a8e61b243 100644 --- a/Code/lsrmGraphToOtbImage.h +++ b/Code/lsrmGraphToOtbImage.h @@ -26,18 +26,12 @@ namespace lsrm typedef typename NodeList::const_iterator NodeConstIterator; typedef unsigned long int LabelPixelType; typedef otb::Image<LabelPixelType, 2> LabelImageType; - typedef unsigned char ClusterPixelType; - typedef otb::VectorImage<ClusterPixelType, 2> ClusteredImageType; using ContourOperator = lp::ContourOperations; LabelImageType::Pointer GetLabelImage(const GraphType& graph, const unsigned int width, const unsigned int height); - - ClusteredImageType::Pointer GetClusteredOutput(const GraphType& graph, - const unsigned int width, - const unsigned int height); }; } // end of namespace lsrm diff --git a/Code/lsrmGraphToOtbImage.h~ b/Code/lsrmGraphToOtbImage.h~ new file mode 100644 index 0000000000000000000000000000000000000000..89ef969ce259a05f70f6c70432d40fab054654cf --- /dev/null +++ b/Code/lsrmGraphToOtbImage.h~ @@ -0,0 +1,45 @@ +#ifndef __LSRM_GRAPH_TO_OTBIMAGE_H +#define __LSRM_GRAPH_TO_OTBIMAGE_H +#include <itkRGBPixel.h> +#include <itkImageRegion.h> +#include <otbImage.h> +#include <otbVectorImage.h> +#include <otbImageFileReader.h> +#include <otbImageFileWriter.h> +#include "lsrmGraph.h" +#include <string> +#include <stdlib.h> +#include <time.h> +#include "lpContour.h" + +namespace lsrm +{ + template<class TGraph> + class GraphToOtbImage + { + public: + + /* Some convenient typedefs */ + typedef TGraph GraphType; + typedef typename GraphType::NodeType NodeType; + typedef std::vector< std::shared_ptr<NodeType> > NodeList; + typedef typename NodeList::const_iterator NodeConstIterator; + typedef unsigned long int LabelPixelType; + typedef otb::Image<LabelPixelType, 2> LabelImageType; + typedef unsigned char ClusterPixelType; + typedef otb::VectorImage<ClusterPixelType, 2> ClusteredImageType; + using ContourOperator = lp::ContourOperations; + + + LabelImageType::Pointer GetLabelImage(const GraphType& graph, + const unsigned int width, + const unsigned int height); + + ClusteredImageType::Pointer GetClusteredOutput(const GraphType& graph, + const unsigned int width, + const unsigned int height); + }; + +} // end of namespace lsrm +#include "lsrmGraphToOtbImage.txx" +#endif diff --git a/Code/lsrmGraphToOtbImage.txx b/Code/lsrmGraphToOtbImage.txx index 66052088b818c16e71fd8396b3b1af60b6c0980e..9042bec00f32b0430d94fd1a71a940bd3a4473bd 100644 --- a/Code/lsrmGraphToOtbImage.txx +++ b/Code/lsrmGraphToOtbImage.txx @@ -2,6 +2,7 @@ #define __LSRM_GRAPH_TO_OTBIMAGE_TXX #include "lsrmGraphToOtbImage.h" #include "itkImageRegionIterator.h" +#include "itkGrayscaleFillholeImageFilter.h" namespace lsrm { @@ -45,83 +46,13 @@ namespace lsrm ++label; } - long unsigned int pixelValue = 0; - for(it.GoToBegin(); !it.IsAtEnd(); ++it) - { - auto pixel = it.Get(); - if(pixel == 0) - it.Set(pixelValue); - else - pixelValue = pixel; - } - - return label_img; - } - - template<class TGraph> - typename GraphToOtbImage<TGraph>::ClusteredImageType::Pointer - GraphToOtbImage<TGraph>::GetClusteredOutput(const GraphType& graph, - const unsigned int width, - const unsigned int height) - { - ClusteredImageType::IndexType index; - ClusteredImageType::SizeType size; - ClusteredImageType::RegionType region; - - index[0] = 0; index[1] = 0; - size[0] = width; size[1] = height; - region.SetIndex(index); - region.SetSize(size); - - ClusteredImageType::Pointer clusterImg = ClusteredImageType::New(); - clusterImg->SetRegions(region); - clusterImg->SetNumberOfComponentsPerPixel(3); - clusterImg->Allocate(); - - ClusteredImageType::PixelType pixelValue; - pixelValue.Reserve(3); - pixelValue[0] = 0; - pixelValue[1] = 0; - pixelValue[2] = 0; - - using ClusterImageIterator = itk::ImageRegionIterator<ClusteredImageType>; - ClusterImageIterator it(clusterImg, clusterImg->GetLargestPossibleRegion()); - for(it.GoToBegin();!it.IsAtEnd(); ++it) - it.Set(pixelValue); - - srand(time(NULL)); - unsigned char c1, c2, c3; - for(auto& region : graph.m_Nodes) - { - c1 = rand() % 256; - c2 = rand() % 256; - c3 = rand() % 256; - - lp::CellLists borderPixels; - ContourOperator::GenerateBorderCells(borderPixels, region->m_Contour, region->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); - } - } - - - for(it.GoToBegin(); !it.IsAtEnd(); ++it) - { - auto pixel = it.Get(); - if(pixel[0] == 0 && pixel[1] == 0 && pixel[2] == 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 clusterImg; + return fillFilter->GetOutput(); } } // end of namespace lsrm diff --git a/Code/lsrmGraphToOtbImage.txx~ b/Code/lsrmGraphToOtbImage.txx~ new file mode 100644 index 0000000000000000000000000000000000000000..5fb16c51b876a86c25e0911ea65641d2ee50fd20 --- /dev/null +++ b/Code/lsrmGraphToOtbImage.txx~ @@ -0,0 +1,125 @@ +#ifndef __LSRM_GRAPH_TO_OTBIMAGE_TXX +#define __LSRM_GRAPH_TO_OTBIMAGE_TXX +#include "lsrmGraphToOtbImage.h" +#include "itkImageRegionIterator.h" +#include "itkGrayscaleFillholeImageFilter.h" + +namespace lsrm +{ + template<class TGraph> + typename GraphToOtbImage<TGraph>::LabelImageType::Pointer + GraphToOtbImage<TGraph>::GetLabelImage(const GraphType& graph, + const unsigned int width, + const unsigned int height) + { + LabelImageType::IndexType index; + LabelImageType::SizeType size; + LabelImageType::RegionType region; + + index[0] = 0; index[1] = 0; + size[0] = width; size[1] = height; + region.SetIndex(index); + region.SetSize(size); + + 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); + + // Start at 1 (value 0 can be used for invalid pixels) + long unsigned int label = 1; + for(auto& region : graph.m_Nodes) + { + lp::CellLists borderPixels; + ContourOperator::GenerateBorderCells(borderPixels, region->m_Contour, region->m_Id, width); + + for (auto& pix: borderPixels) + { + index[0] = pix % width; + index[1] = pix / width; + label_img->SetPixel(index, label); + } + ++label; + } + + // Fill holes + typedef itk::GrayscaleFillholeImageFilter<LabelImageType,LabelImageType> FillholeFilterType; + FillholeFilterType::Pointer fillFilter = FillholeFilterType::New(); + fillFilter->SetInput(label_img); + fillFilter->Update(); + + return fillFilter->GetOutput(); + } + + template<class TGraph> + typename GraphToOtbImage<TGraph>::ClusteredImageType::Pointer + GraphToOtbImage<TGraph>::GetClusteredOutput(const GraphType& graph, + const unsigned int width, + const unsigned int height) + { + ClusteredImageType::IndexType index; + ClusteredImageType::SizeType size; + ClusteredImageType::RegionType region; + + index[0] = 0; index[1] = 0; + size[0] = width; size[1] = height; + region.SetIndex(index); + region.SetSize(size); + + ClusteredImageType::Pointer clusterImg = ClusteredImageType::New(); + clusterImg->SetRegions(region); + clusterImg->SetNumberOfComponentsPerPixel(3); + clusterImg->Allocate(); + + ClusteredImageType::PixelType pixelValue; + pixelValue.Reserve(3); + pixelValue[0] = 0; + pixelValue[1] = 0; + pixelValue[2] = 0; + + using ClusterImageIterator = itk::ImageRegionIterator<ClusteredImageType>; + ClusterImageIterator it(clusterImg, clusterImg->GetLargestPossibleRegion()); + for(it.GoToBegin();!it.IsAtEnd(); ++it) + it.Set(pixelValue); + + srand(time(NULL)); + unsigned char c1, c2, c3; + for(auto& region : graph.m_Nodes) + { + c1 = rand() % 256; + c2 = rand() % 256; + c3 = rand() % 256; + + lp::CellLists borderPixels; + ContourOperator::GenerateBorderCells(borderPixels, region->m_Contour, region->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); + } + } + + + for(it.GoToBegin(); !it.IsAtEnd(); ++it) + { + auto pixel = it.Get(); + if(pixel[0] == 0 && pixel[1] == 0 && pixel[2] == 0) + it.Set(pixelValue); + else + pixelValue = pixel; + } + + return clusterImg; + } + +} // end of namespace lsrm +#endif diff --git a/Code/lsrmSegmenter.h b/Code/lsrmSegmenter.h index f48f9290d0abfdfa357c1b8b9a343d04003bc775..92771b9b902aa2d64984e7f2626322c927c7718b 100644 --- a/Code/lsrmSegmenter.h +++ b/Code/lsrmSegmenter.h @@ -24,7 +24,6 @@ namespace lsrm typedef typename GraphOperatorType::NodePointerType NodePointerType; typedef GraphToOtbImage<GraphType> IOType; typedef typename IOType::LabelImageType LabelImageType; - typedef typename IOType::ClusteredImageType ClusteredImageType; /* Default constructor and destructor */ @@ -81,13 +80,6 @@ namespace lsrm auto labelImg = io.GetLabelImage(this->m_Graph, this->m_ImageWidth, this->m_ImageHeight); return labelImg; } - - inline typename ClusteredImageType::Pointer GetClusteredImageOutput() - { - IOType io; - auto clusteredImg = io.GetClusteredOutput(this->m_Graph, this->m_ImageWidth, this->m_ImageHeight); - return clusteredImg; - } /* Set methods */ SetMacro(bool, DoBFSegmentation);