diff --git a/include/lsgrmController.txx b/include/lsgrmController.txx index feb4b4014c55c1f6bced673aa076fda5115e9db2..6e072f0f9e7d803caa72777bf85ba09b42b10bdc 100644 --- a/include/lsgrmController.txx +++ b/include/lsgrmController.txx @@ -88,6 +88,7 @@ void Controller<TSegmenter>::RunSegmentation() // Run first partial segmentation boost::timer t; t.restart(); + auto accumulatedMemory = RunFirstPartialSegmentation<TSegmenter>( m_InputImage, m_SpecificParameters, @@ -140,7 +141,7 @@ void Controller<TSegmenter>::RunSegmentation() numberOfIterationsRemaining -= numberOfIterationsForPartialSegmentations; } } - + #ifdef OTB_USE_MPI // Only the master process is doing the next part // TODO: Use the MPI process wich has the largest amount of memory diff --git a/include/lsgrmGraphOperations.h b/include/lsgrmGraphOperations.h index 9ad45c8cd87c98093bf7e60cec7517aca02652bf..3ce66f2d87d3ce5589f312e41a1ceb68fa43926f 100644 --- a/include/lsgrmGraphOperations.h +++ b/include/lsgrmGraphOperations.h @@ -98,7 +98,8 @@ void BuildBorderPixelMap(typename TSegmenter::GraphType& graph, const unsigned int nbTilesY, std::unordered_map<std::size_t, std::vector<typename TSegmenter::NodePointerType> >& borderPixelMap, - const unsigned int imageWidth); + const unsigned int imageWidth, + bool merging = false); template<class TSegmenter> void InsertNodesFromTile(typename TSegmenter::GraphType& graph, diff --git a/include/lsgrmGraphOperations.txx b/include/lsgrmGraphOperations.txx index b50872aa8ed5499ebddf06fdcff3971d13502e4e..bebfc46ed6a90a30df3001cae1ad76c0a057c1ad 100644 --- a/include/lsgrmGraphOperations.txx +++ b/include/lsgrmGraphOperations.txx @@ -69,7 +69,7 @@ MergeAllGraphsAndAchieveSegmentation( std::cout << "\tBuildBorderPixelMap..." << std::endl; BuildBorderPixelMap<TSegmenter>(segmenter.m_Graph, tiles[row*nbTilesX + col], row, col, - nbTilesX, nbTilesY, borderPixelMap, imageWidth); + nbTilesX, nbTilesY, borderPixelMap, imageWidth, true); std::cout << "\tRemoveDuplicatedNodes..." << std::endl; @@ -93,7 +93,9 @@ MergeAllGraphsAndAchieveSegmentation( segmenter.SetNumberOfIterations(numberOfIterations); grm::GraphOperations<TSegmenter>::PerfomAllIterationsWithLMBFAndConstThreshold(segmenter); + // patch raf (sort using node id directly --> seems ok) // Sort the nodes top-->down and left-->right + /* std::sort(segmenter.m_Graph.m_Nodes.begin(), segmenter.m_Graph.m_Nodes.end(), [imageWidth](const auto & a, const auto & b) -> bool { @@ -101,8 +103,10 @@ MergeAllGraphsAndAchieveSegmentation( lp::ContourOperations::GenerateBorderCells(borderPixelsA, a->m_Contour, a->m_Id, imageWidth); lp::ContourOperations::GenerateBorderCells(borderPixelsB, b->m_Contour, b->m_Id, imageWidth); - unsigned int offA = 0; - unsigned int offB = 0; + // patch raf (were unsigned int) + std::size_t offA = 0; + std::size_t offB = 0; + // end patch for (auto& pix: borderPixelsA) if (pix>offA) offA=pix; @@ -113,6 +117,13 @@ MergeAllGraphsAndAchieveSegmentation( return offA > offB; }); + */ + std::sort(segmenter.m_Graph.m_Nodes.begin(), segmenter.m_Graph.m_Nodes.end(), + [](const auto & a, const auto & b) -> bool + { + return a->m_Id < b->m_Id; + }); + // end patch return segmenter.m_Graph; } @@ -466,7 +477,8 @@ void BuildBorderPixelMap(typename TSegmenter::GraphType& graph, const unsigned int nbTilesY, std::unordered_map<std::size_t, std::vector<typename TSegmenter::NodePointerType> >& borderPixelMap, - const unsigned int imageWidth) + const unsigned int imageWidth, + bool merging) { unsigned int rowPixel, colPixel; unsigned int rowMin = (tile.rows[0] > 0) ? tile.rows[0] - 1 : tile.rows[0]; @@ -483,6 +495,15 @@ void BuildBorderPixelMap(typename TSegmenter::GraphType& graph, { continue; } + // patch raf (exclude nodes all outside tiles - on merging stage only) + else if(merging && (node->m_Bbox.m_UX > tile.columns[1] || + node->m_Bbox.m_UY > tile.rows[1] || + node->m_Bbox.m_UX + node->m_Bbox.m_W - 1 < tile.columns[0] || + node->m_Bbox.m_UY + node->m_Bbox.m_H - 1 < tile.rows[0])) + { + continue; + } + // end patch else { lp::CellLists borderPixels; @@ -618,9 +639,11 @@ long long unsigned int RunFirstPartialSegmentation( std::cout << "--- Running fist partial segmentation...\nNumber of neighbor layers " << numberOfNeighborLayers << std::endl; - for(unsigned int row = 0; row < nbTilesY; ++row) + //for(unsigned int row = 0; row < nbTilesY; ++row) + for(unsigned int row = 24; row < 25; ++row) { - for(unsigned int col = 0; col < nbTilesX; col++) + //for(unsigned int col = 0; col < nbTilesX; col++) + for(unsigned int col = 30; col < 31; col++) { #ifdef OTB_USE_MPI if (MyTurn(row*nbTilesX + col)) @@ -645,7 +668,7 @@ long long unsigned int RunFirstPartialSegmentation( segmenter.SetNumberOfIterations(niter); segmenter.SetInput(imageTile); segmenter.Update(); - + if(segmenter.GetComplete() == false) isFusion = true; @@ -658,7 +681,7 @@ long long unsigned int RunFirstPartialSegmentation( tileWidth, tileHeight, imageWidth); - + // Remove unstable segments std::cout << "\tRemoving unstable segments..." << std::endl; RemoveUnstableSegments<TSegmenter>(segmenter.m_Graph, currentTile, imageWidth); @@ -991,7 +1014,7 @@ void RescaleGraph(typename TSegmenter::GraphType& graph, // Start pixel index of the node (in the image) rowNodeImg = rowTile * tileHeight + rowNodeTile - tile.margin[0]; colNodeImg = colTile * tileWidth + colNodeTile - tile.margin[3]; - node->m_Id = rowNodeImg * imageWidth + colNodeImg; + node->m_Id = (std::size_t)rowNodeImg * (std::size_t)imageWidth + (std::size_t)colNodeImg; // Change also its bounding box node->m_Bbox.m_UX = colTile * tileWidth + node->m_Bbox.m_UX - tile.margin[3]; diff --git a/include/otbStreamingGraphToImageFilter.txx b/include/otbStreamingGraphToImageFilter.txx index 26ef5d011d376fae3fe55b9b0f4d0aee1005b323..0332f547bf63ae56813caca4989eb8f0da2d955c 100644 --- a/include/otbStreamingGraphToImageFilter.txx +++ b/include/otbStreamingGraphToImageFilter.txx @@ -131,9 +131,17 @@ StreamingGraphToImageFilter<TGraph, TLabelImage> for (auto& pix: borderPixels) { - index[0] = pix % m_OutputSize[0]; - index[1] = pix / m_OutputSize[0]; + // patch raf (catch exception if position of pixel to set is not correct) + try { + index[0] = (unsigned int)(pix % (std::size_t)(m_OutputSize[0])); + index[1] = (unsigned int)(pix / (std::size_t)(m_OutputSize[0])); labelImage->SetPixel(index, res.second); + } catch (std::exception e) { + std::cout << "Pixel ID: " << pix << std::endl; + std::cout << "Derived index: " << index[0] << "," << index[1] << std::endl; + std::cout << "Node position in list: " << res.second << std::endl; + } + // end patch } } itkDebugMacro( << "Burning boundaries ok ");