diff --git a/include/lsgrmController.h b/include/lsgrmController.h index 54e5d349fd989475bab8a5b92204d0b2e2d516e4..d560f49f21666f2910689fc14e1e9913313019c7 100644 --- a/include/lsgrmController.h +++ b/include/lsgrmController.h @@ -3,6 +3,7 @@ #include "lsrmGetInternalMemory.h" #include "lsgrmSplitter.h" #include "lsgrmGraphOperations.h" +#include "itkMacro.h" namespace lsgrm { @@ -28,7 +29,8 @@ namespace lsgrm void SetTemporaryDirectory(const std::string& str); void SetTileWidth(const unsigned int v); void SetTileHeight(const unsigned int v); - void SetNumberOfFirstIterations(const unsigned int v); + void SetNumberOfFirstIterations(const unsigned int v); + void SetNumberOfIterations(const unsigned int v); void SetSpecificParameters(const SegmentationParameterType& params); void SetThreshold(const float& t); void SetInternalMemoryAvailable(long long unsigned int v); // expecting a value in Mbytes. @@ -36,6 +38,8 @@ namespace lsgrm typename LabelImageType::Pointer GetLabeledClusteredOutput(); + itkGetMacro(ListOfTemporaryFiles, std::vector<std::string>); + private: void GetAutomaticConfiguration(); @@ -55,13 +59,15 @@ namespace lsgrm /* Internal attribute members.*/ unsigned int m_NbTilesX; unsigned int m_NbTilesY; - unsigned int m_NumberOfFirstIterations; + unsigned int m_NumberOfFirstIterations; + unsigned int m_NumberOfIterations; unsigned int m_Margin; unsigned int m_TileWidth; unsigned int m_TileHeight; bool m_AutomaticTilingLayout; std::vector<ProcessingTile> m_Tiles; + std::vector<std::string> m_ListOfTemporaryFiles; typename LabelImageType::Pointer m_LabelImage; }; diff --git a/include/lsgrmController.txx b/include/lsgrmController.txx index 4ed9c9d56bf524203620c675504c8970e48b1957..ba55f0fa2a1bb0e0b4546c512a0240e5d5896c8c 100644 --- a/include/lsgrmController.txx +++ b/include/lsgrmController.txx @@ -19,8 +19,12 @@ template<class TSegmenter> void Controller<TSegmenter>::RunSegmentation() { + // TODO: smarter value? + const unsigned int numberOfIterationsForPartialSegmentations = 3; + unsigned int numberOfIterations = m_NumberOfIterations; + // Automatic procedure - if(m_Memory < 1) + if(m_Memory == 0) { this->GetAutomaticConfiguration(); } @@ -45,17 +49,18 @@ void Controller<TSegmenter>::RunSegmentation() m_SpecificParameters, m_Threshold, m_NumberOfFirstIterations, - 3, + numberOfIterationsForPartialSegmentations, m_Tiles, m_NbTilesX, m_NbTilesY, m_TileWidth, m_TileHeight, - m_InputImage->GetLargestPossibleRegion().GetSize()[0], - m_InputImage->GetLargestPossibleRegion().GetSize()[1], m_TemporaryDirectory, isFusion); + // Update the given number of iterations + numberOfIterations -= m_NumberOfFirstIterations; + // Gathering useful variables GatherUsefulVariables(accumulatedMemory, isFusion); @@ -69,7 +74,7 @@ void Controller<TSegmenter>::RunSegmentation() accumulatedMemory = RunPartialSegmentation<TSegmenter>( m_SpecificParameters, m_Threshold, - 3, + numberOfIterationsForPartialSegmentations, m_Tiles, m_TemporaryDirectory, m_NbTilesX, @@ -79,6 +84,9 @@ void Controller<TSegmenter>::RunSegmentation() m_InputImage->GetNumberOfComponentsPerPixel(), isFusion); + // Update the given number of iterations + numberOfIterations -= numberOfIterationsForPartialSegmentations; + // Gathering useful variables GatherUsefulVariables(accumulatedMemory, isFusion); @@ -104,7 +112,8 @@ void Controller<TSegmenter>::RunSegmentation() m_NbTilesY, m_InputImage->GetLargestPossibleRegion().GetSize()[0], m_InputImage->GetLargestPossibleRegion().GetSize()[1], - m_InputImage->GetNumberOfComponentsPerPixel()); + m_InputImage->GetNumberOfComponentsPerPixel(), + numberOfIterations); // ShowTime(t); @@ -280,6 +289,12 @@ void Controller<TSegmenter>::SetNumberOfFirstIterations(const unsigned int v) m_Margin = static_cast<unsigned int>(pow(2, m_NumberOfFirstIterations + 1) - 2);// 2^{n+1}-2 } +template<class TSegmenter> +void Controller<TSegmenter>::SetNumberOfIterations(const unsigned int v) +{ + m_NumberOfIterations = v; +} + template<class TSegmenter> void Controller<TSegmenter>::SetSpecificParameters(const SegmentationParameterType& params) { diff --git a/include/lsgrmGraphOperations.h b/include/lsgrmGraphOperations.h index 49ff0b5119382bc2af4727d283500e7799cae0ab..a0a9a10302d408d00084eb8b60d3bc92a35895ef 100644 --- a/include/lsgrmGraphOperations.h +++ b/include/lsgrmGraphOperations.h @@ -35,7 +35,8 @@ MergeAllGraphsAndAchieveSegmentation( const unsigned int nbTilesY, const unsigned int imageWidth, const unsigned int imageHeight, - const unsigned int imageBands); + const unsigned int imageBands, + unsigned int numberOfIterations); template<class TSegmenter> long long unsigned int RunFirstPartialSegmentation( @@ -49,8 +50,6 @@ long long unsigned int RunFirstPartialSegmentation( const unsigned int nbTilesY, const unsigned int tileWidth, const unsigned int tileHeight, - const unsigned int imageWidth, - const unsigned int imageHeight, const std::string& tmpDir, bool& isFusion); diff --git a/include/lsgrmGraphOperations.txx b/include/lsgrmGraphOperations.txx index 4b54333ab173d6c62a8e0e38401f1789512a6594..303ac4678bd0325d2aca6f56ebfdce6a8b8a89ac 100644 --- a/include/lsgrmGraphOperations.txx +++ b/include/lsgrmGraphOperations.txx @@ -32,7 +32,8 @@ MergeAllGraphsAndAchieveSegmentation( const unsigned int nbTilesY, const unsigned int imageWidth, const unsigned int imageHeight, - const unsigned int imageBands) + const unsigned int imageBands, + unsigned int numberOfIterations) { // TODO parallelize this ... @@ -104,7 +105,7 @@ MergeAllGraphsAndAchieveSegmentation( segmenter.SetParam(params); segmenter.SetThreshold(threshold); segmenter.SetDoBFSegmentation(true); - segmenter.SetNumberOfIterations(75); + segmenter.SetNumberOfIterations(numberOfIterations); lsrm::GraphOperations<TSegmenter>::PerfomAllIterationsWithLMBFAndConstThreshold(segmenter); @@ -143,8 +144,9 @@ long long unsigned int RunPartialSegmentation(const typename TSegmenter::Paramet if (MyTurn(row*nbTilesX + col)) { TSegmenter segmenter; - std::cout << "Tile " << row << " " << col << std::endl; + std::cout << "Processing tile " << row << ", " << col << std::endl; std::cout << "\tLoad graph..." << std::endl; + // Load the graph nodesPath = tmpDir + "tile_nodes_" + std::to_string(row) + "_" + std::to_string(col) + ".bin"; edgesPath = tmpDir + "tile_edges_" + std::to_string(row) + "_" + std::to_string(col) + ".bin"; @@ -692,13 +694,13 @@ long long unsigned int RunFirstPartialSegmentation( const unsigned int nbTilesY, const unsigned int tileWidth, const unsigned int tileHeight, - const unsigned int imageWidth, - const unsigned int imageHeight, const std::string& tmpDir, bool& isFusion) { using ImageType = typename TSegmenter::ImageType; + const unsigned int imageWidth = inputPtr->GetLargestPossibleRegion().GetSize()[0]; + const unsigned int imageHeight = inputPtr->GetLargestPossibleRegion().GetSize()[1]; long long unsigned int accumulatedMemory = 0; isFusion = false; @@ -736,7 +738,7 @@ long long unsigned int RunFirstPartialSegmentation( isFusion = true; // Rescale the graph to be in the reference of the image - std::cout << "\tRescale graph" << std::endl; + std::cout << "\tRescaling graph..." << std::endl; RescaleGraph<TSegmenter>(segmenter.m_Graph, tiles[row*nbTilesX + col], row, @@ -746,19 +748,20 @@ long long unsigned int RunFirstPartialSegmentation( imageWidth); // Remove unstable segments - std::cout << "\tRemoving unstable segments" << std::endl; + std::cout << "\tRemoving unstable segments..." << std::endl; RemoveUnstableSegments<TSegmenter>(segmenter.m_Graph, tiles[row*nbTilesX + col], imageWidth); // Retrieve the amount of memory to store this graph - std::cout << "\tGet graph memory" << std::endl; + std::cout << "\tRetrieving graph memory..." << std::endl; accumulatedMemory += GetGraphMemory<TSegmenter>(segmenter.m_Graph); // Write graph to temporay directory (warning specific to Baatz & Schape !!!) - std::cout << "\tWrite graph" << std::endl; + std::cout << "\tWriting graph..." << std::endl; WriteGraph<TSegmenter>(segmenter.m_Graph, tmpDir, row, col); // Extract stability margin for all borders different from 0 imageWidth-1 et imageHeight -1 // and write them to the stability margin + std::cout << "\tComputing stability margin..." << std::endl; { std::unordered_map<typename TSegmenter::NodePointerType, unsigned int> borderNodeMap; @@ -1230,34 +1233,37 @@ void RescaleGraph(typename TSegmenter::GraphType& graph, unsigned int rowNodeTile, colNodeTile; unsigned int rowNodeImg, colNodeImg; - unsigned int currTileWidth = tile.columns[1] - tile.columns[0] + 1; - unsigned int diffHeightMargin = tile.margin[0]; - currTileWidth += tile.margin[1]; - unsigned int diffWidthMargin = tile.margin[3]; - currTileWidth += tile.margin[3]; +// unsigned int currTileWidth = tile.columns[1] - tile.columns[0] + 1; +// currTileWidth += tile.margin[1]; +// currTileWidth += tile.margin[3]; +// unsigned int diffHeightMargin = tile.margin[0]; +// unsigned int diffWidthMargin = tile.margin[3]; - unsigned int count = 0; for(auto& node : graph.m_Nodes) { - // Row of the start pixel of the node - rowNodeTile = node->m_Id / currTileWidth; - // Col of the start pixel of the node - colNodeTile = node->m_Id % currTileWidth; - - // Row of the start pixel of the node in the image - rowNodeImg = rowTile * tileHeight + rowNodeTile - diffHeightMargin; - // Col of the start pixel of the node in the image - colNodeImg = colTile * tileWidth + colNodeTile - diffWidthMargin; - - // New id of the node in the image +// // Row of the start pixel of the node +// rowNodeTile = node->m_Id / currTileWidth; +// // Col of the start pixel of the node +// colNodeTile = node->m_Id % currTileWidth; +// +// // Row of the start pixel of the node in the image +// rowNodeImg = rowTile * tileHeight + rowNodeTile - diffHeightMargin; +// // Col of the start pixel of the node in the image +// colNodeImg = colTile * tileWidth + colNodeTile - diffWidthMargin; +// +// node->m_Id = rowNodeImg * imageWidth + colNodeImg; + + rowNodeTile = node->m_Id / tile.region.GetSize()[0]; + colNodeTile = node->m_Id % tile.region.GetSize()[0]; + rowNodeImg = rowTile * tileHeight + rowNodeTile - tile.margin[0]; + colNodeImg = colTile * tileWidth + colNodeTile - tile.margin[3]; node->m_Id = rowNodeImg * imageWidth + colNodeImg; // Change also its bounding box - node->m_Bbox.m_UX = colTile * tileWidth + node->m_Bbox.m_UX - diffWidthMargin; - node->m_Bbox.m_UY = rowTile * tileHeight + node->m_Bbox.m_UY - diffHeightMargin; - - ++count; + node->m_Bbox.m_UX = colTile * tileWidth + node->m_Bbox.m_UX - tile.margin[3]; + node->m_Bbox.m_UY = rowTile * tileHeight + node->m_Bbox.m_UY - tile.margin[0]; } + } }