Commit 9369f8d147bd9f0a7b6b6dc815144f9a6a0a3a28

Authored by Pierre Lassalle
1 parent 1e0e990b
Exists in master

lss framework

Showing 61 changed files with 9079 additions and 0 deletions   Show diff stats
code/CMakeLists.txt 0 → 100644
... ... @@ -0,0 +1,105 @@
  1 +PROJECT(LSS)
  2 +
  3 +cmake_minimum_required(VERSION 2.8)
  4 +
  5 +FIND_PACKAGE(OTB)
  6 +IF(OTB_FOUND)
  7 + INCLUDE(${OTB_USE_FILE})
  8 +ELSE(OTB_FOUND)
  9 + MESSAGE(FATAL_ERROR
  10 + "Cannot build OTB project without OTB. Please set OTB_DIR.")
  11 +ENDIF(OTB_FOUND)
  12 +
  13 +include(CheckCXXCompilerFlag)
  14 +CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
  15 +CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
  16 +if(COMPILER_SUPPORTS_CXX11)
  17 + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -fpermissive")
  18 +elseif(COMPILER_SUPPORTS_CXX0X)
  19 + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x -fpermissive")
  20 +else()
  21 + message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
  22 +endif()
  23 +
  24 +set(EXECUTABLE_OUTPUT_PATH bin)
  25 +
  26 +file(
  27 + GLOB_RECURSE
  28 + HEADERS
  29 + "src/*.h"
  30 +)
  31 +
  32 +file(
  33 + GLOB_RECURSE
  34 + TEMPLATES
  35 + "src/*.txx"
  36 +)
  37 +
  38 +file(
  39 + GLOB_RECURSE
  40 + SOURCES
  41 + "src/*.cxx"
  42 +)
  43 +
  44 +set(LSSS_INCLUDE_DIRS "")
  45 +foreach(_headerFile ${HEADERS})
  46 + get_filename_component(_dir ${_headerFile} PATH)
  47 + list(APPEND LSSS_INCLUDE_DIRS ${_dir})
  48 +endforeach()
  49 +
  50 +foreach(_templateFile ${TEMPLATES})
  51 + get_filename_component(_dir ${_templateFile} PATH)
  52 + list(APPEND LSSS_INCLUDE_DIRS ${_dir})
  53 +endforeach()
  54 +
  55 +list(REMOVE_DUPLICATES LSSS_INCLUDE_DIRS)
  56 +
  57 +include_directories(${LSSS_INCLUDE_DIRS})
  58 +
  59 +ADD_EXECUTABLE(
  60 + TiledBaatzSegmentation
  61 + apps/TiledBaatzSegmentation.cxx
  62 + ${SOURCES}
  63 +)
  64 +
  65 +TARGET_LINK_LIBRARIES(TiledBaatzSegmentation OTBCommon OTBIO)
  66 +
  67 +ADD_EXECUTABLE(
  68 + TiledSpringSegmentation
  69 + apps/TiledSpringSegmentation.cxx
  70 + ${SOURCES}
  71 +)
  72 +
  73 +TARGET_LINK_LIBRARIES(TiledSpringSegmentation OTBCommon OTBIO)
  74 +
  75 +ADD_EXECUTABLE(
  76 + SpringSegmentation
  77 + apps/SpringSegmentation.cxx
  78 + ${SOURCES}
  79 +)
  80 +
  81 +TARGET_LINK_LIBRARIES(SpringSegmentation OTBCommon OTBIO)
  82 +
  83 +ADD_EXECUTABLE(
  84 + TiledFLSASegmentation
  85 + apps/TiledFLSASegmentation.cxx
  86 + ${SOURCES}
  87 +)
  88 +
  89 +TARGET_LINK_LIBRARIES(TiledFLSASegmentation OTBCommon OTBIO)
  90 +
  91 +ADD_EXECUTABLE(
  92 + BaatzSegmentation
  93 + apps/BaatzSegmentation.cxx
  94 + ${SOURCES}
  95 +)
  96 +
  97 +TARGET_LINK_LIBRARIES(BaatzSegmentation OTBCommon OTBIO)
  98 +
  99 +ADD_EXECUTABLE(
  100 + FLSASegmentation
  101 + apps/FLSASegmentation.cxx
  102 + ${SOURCES}
  103 +)
  104 +
  105 +TARGET_LINK_LIBRARIES(FLSASegmentation OTBCommon OTBIO)
... ...
code/apps/BaatzSegmentation.cxx 0 → 100644
... ... @@ -0,0 +1,51 @@
  1 +/*=========================================================================
  2 +
  3 + Program: Large Scale Segmentation (LSS)
  4 + Language: C++
  5 + author: Lassalle Pierre
  6 +
  7 +
  8 +
  9 + Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
  10 +
  11 +
  12 + This software is distributed WITHOUT ANY WARRANTY; without even
  13 + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  14 + PURPOSE. See the above copyright notices for more information.
  15 +
  16 +=========================================================================*/
  17 +#include <otbVectorImage.h>
  18 +#include "BaatzAlgorithm.h"
  19 +#include "lss_RegionBasedTileBasedReduction.h"
  20 +#include "lss_RegionBasedTileBasedIterativeReduction.h"
  21 +#include "lss_RegionBasedFinisher.h"
  22 +
  23 +int main(int argc, char **argv)
  24 +{
  25 + if(argc != 6)
  26 + {
  27 + std::cerr << "Usage: " << argv[0] << ": \n"
  28 + << "[input image path (jpg, png, tiff, jpeg200)]\n"
  29 + << "[output image path (jpg, png, tiff)]\n"
  30 + << "[scale]\n"
  31 + << "[color]\n"
  32 + << "[compactness]\n";
  33 +
  34 + return 1;
  35 + }
  36 +
  37 + std::string input = argv[1];
  38 + std::string output = argv[2];
  39 + BaatzParameters bparams = {(float)atof(argv[4]), (float)atof(argv[5]), (float)pow(atof(argv[3]), 2)};
  40 +
  41 + typedef otb::VectorImage<float, 2> ImageType;
  42 + typedef BaatzAlgorithm<ImageType> AlgorithmType;
  43 +
  44 + AlgorithmType baatz_segmenter;
  45 + baatz_segmenter.SetParameters(bparams);
  46 + baatz_segmenter.InitFromTiffImage(input);
  47 + baatz_segmenter.Segmentation();
  48 + baatz_segmenter.WriteResultingImage(output);
  49 +
  50 + return 0;
  51 +}
... ...
code/apps/FLSASegmentation.cxx 0 → 100644
... ... @@ -0,0 +1,49 @@
  1 +/*=========================================================================
  2 +
  3 + Program: Large Scale Segmentation (LSS)
  4 + Language: C++
  5 + author: Lassalle Pierre
  6 +
  7 +
  8 +
  9 + Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
  10 +
  11 +
  12 + This software is distributed WITHOUT ANY WARRANTY; without even
  13 + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  14 + PURPOSE. See the above copyright notices for more information.
  15 +
  16 +=========================================================================*/
  17 +#include <otbVectorImage.h>
  18 +#include "FLSAlgorithm.h"
  19 +#include "lss_RegionBasedTileBasedReduction.h"
  20 +#include "lss_RegionBasedTileBasedIterativeReduction.h"
  21 +#include "lss_RegionBasedFinisher.h"
  22 +
  23 +int main(int argc, char **argv)
  24 +{
  25 + if(argc != 4)
  26 + {
  27 + std::cerr << "Usage: " << argv[0] << ": \n"
  28 + << "[input image path (jpg, png, tiff, jpeg200)]\n"
  29 + << "[output image path (jpg, png, tiff)]\n"
  30 + << "[lambda]\n";
  31 +
  32 + return 1;
  33 + }
  34 +
  35 + std::string input = argv[1];
  36 + std::string output = argv[2];
  37 + FLSParameter fparam = atof(argv[3]);
  38 +
  39 + typedef otb::VectorImage<float, 2> ImageType;
  40 + typedef FLSAlgorithm<ImageType> AlgorithmType;
  41 +
  42 + AlgorithmType fls_segmenter;
  43 + fls_segmenter.SetParameters(fparam);
  44 + fls_segmenter.InitFromTiffImage(input);
  45 + fls_segmenter.Segmentation();
  46 + fls_segmenter.WriteResultingImage(output);
  47 +
  48 + return 0;
  49 +}
... ...
code/apps/SpringSegmentation.cxx 0 → 100644
... ... @@ -0,0 +1,49 @@
  1 +/*=========================================================================
  2 +
  3 + Program: Large Scale Segmentation (LSS)
  4 + Language: C++
  5 + author: Lassalle Pierre
  6 +
  7 +
  8 +
  9 + Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
  10 +
  11 +
  12 + This software is distributed WITHOUT ANY WARRANTY; without even
  13 + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  14 + PURPOSE. See the above copyright notices for more information.
  15 +
  16 +=========================================================================*/
  17 +#include <otbVectorImage.h>
  18 +#include "SpringAlgorithm.h"
  19 +#include "lss_RegionBasedTileBasedReduction.h"
  20 +#include "lss_RegionBasedTileBasedIterativeReduction.h"
  21 +#include "lss_RegionBasedFinisher.h"
  22 +
  23 +int main(int argc, char **argv)
  24 +{
  25 + if(argc != 4)
  26 + {
  27 + std::cerr << "Usage: " << argv[0] << ": \n"
  28 + << "[input image path (jpg, png, tiff, jpeg200)]\n"
  29 + << "[output image path (jpg, png, tiff)]\n"
  30 + << "[euclidean distance threshold]\n";
  31 +
  32 + return 1;
  33 + }
  34 +
  35 + std::string input = argv[1];
  36 + std::string output = argv[2];
  37 + SpringParameter sparams = (double)atof(argv[3]);
  38 +
  39 + typedef otb::VectorImage<float, 2> ImageType;
  40 + typedef SpringAlgorithm<ImageType> AlgorithmType;
  41 +
  42 + AlgorithmType spring_segmenter;
  43 + spring_segmenter.SetParameters(sparams);
  44 + spring_segmenter.InitFromTiffImage(input);
  45 + spring_segmenter.Segmentation();
  46 + spring_segmenter.WriteResultingImage(output);
  47 +
  48 + return 0;
  49 +}
... ...
code/apps/TiledBaatzSegmentation.cxx 0 → 100644
... ... @@ -0,0 +1,94 @@
  1 +/*=========================================================================
  2 +
  3 + Program: Large Scale Segmentation (LSS)
  4 + Language: C++
  5 + author: Lassalle Pierre
  6 +
  7 +
  8 +
  9 + Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
  10 +
  11 +
  12 + This software is distributed WITHOUT ANY WARRANTY; without even
  13 + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  14 + PURPOSE. See the above copyright notices for more information.
  15 +
  16 +=========================================================================*/
  17 +#include <otbVectorImage.h>
  18 +#include "BaatzAlgorithm.h"
  19 +#include "lss_RegionBasedTileBasedReduction.h"
  20 +#include "lss_RegionBasedTileBasedIterativeReduction.h"
  21 +#include "lss_RegionBasedFinisher.h"
  22 +
  23 +int main(int argc, char **argv)
  24 +{
  25 + if(argc != 11)
  26 + {
  27 + std::cerr << "Usage: " << argv[0] << ": \n"
  28 + << "[input image path (jpg, png, tiff, jpeg200)]\n"
  29 + << "[output image path (jpg, png, tiff)]\n"
  30 + << "[temporary directory path]\n"
  31 + << "[tile size according to the width]\n"
  32 + << "[tile size according to the height]\n"
  33 + << "[number of iteration for Tile-Based reduction step]\n"
  34 + << "[scale]\n"
  35 + << "[color]\n"
  36 + << "[compactness]\n"
  37 + << "[memory available (Mo)]";
  38 +
  39 + return 1;
  40 + }
  41 +
  42 + std::string input = argv[1];
  43 + std::string output = argv[2];
  44 + std::string tmpdir = argv[3];
  45 + unsigned int tilesizeX = atoi(argv[4]);
  46 + unsigned int tilesizeY = atoi(argv[5]);
  47 + unsigned int niter = atoi(argv[6]);
  48 + BaatzParameters bparams = {(float)atof(argv[8]), (float)atof(argv[9]), (float)pow(atof(argv[7]), 2)};
  49 + long unsigned int memory = atoi(argv[10]);
  50 + unsigned int margin = static_cast<unsigned int>(pow(2, niter+1) - 2);
  51 +
  52 + typedef otb::VectorImage<float, 2> ImageType;
  53 + typedef BaatzAlgorithm<ImageType> AlgorithmType;
  54 + typedef lss::RegionBasedTileBasedReduction<ImageType, AlgorithmType> TileBasedReductionType;
  55 + typedef lss::RegionBasedTileBasedIterativeReduction<AlgorithmType> TileBasedIterativeReductionType;
  56 + typedef lss::RegionBasedFinisher<AlgorithmType> FinisherType;
  57 +
  58 + // Tile Based Reduction step
  59 + std::cout << "***********************************\n";
  60 + std::cout << "* Tile Based Reduction Step *\n";
  61 + std::cout << "***********************************\n";
  62 + TileBasedReductionType tb_reduction;
  63 + tb_reduction.SetInputImagePath(input);
  64 + tb_reduction.SetTemporaryDirectoryPath(tmpdir);
  65 + tb_reduction.SetTileSizeX(tilesizeX);
  66 + tb_reduction.SetTileSizeY(tilesizeY);
  67 + tb_reduction.SetMarginOfStability(margin);
  68 + tb_reduction.SetNumberOfIterations(niter);
  69 + tb_reduction.SetSegmentationParameters(bparams);
  70 + tb_reduction.GenerateData();
  71 +
  72 + // Tile Based Iterative reduction step
  73 + std::cout << "*********************************************\n";
  74 + std::cout << "* Tile Based Iterative Reduction Step *\n";
  75 + std::cout << "*********************************************\n";
  76 + TileBasedIterativeReductionType tb_itreduction;
  77 + tb_itreduction.SetObserver(tb_reduction.GetObserver());
  78 + tb_itreduction.SetSegmentationParameters(bparams);
  79 + tb_itreduction.SetAvailableMemory(memory);
  80 + bool DoSegmentation = tb_itreduction.GenerateData();
  81 +
  82 + std::cout << "*********************************************\n";
  83 + std::cout << "* Aggregating and completion step *\n";
  84 + std::cout << "*********************************************\n";
  85 + FinisherType finisher;
  86 + finisher.SetOutputPath(output);
  87 + finisher.SetObserver(tb_itreduction.GetObserver());
  88 + finisher.SetDoSegmentation(DoSegmentation);
  89 + finisher.SetTiles(tb_itreduction.GetTiles());
  90 + finisher.SetSegmentationParameters(bparams);
  91 + finisher.GenerateData();
  92 +
  93 + return 0;
  94 +}
... ...
code/apps/TiledFLSASegmentation.cxx 0 → 100644
... ... @@ -0,0 +1,92 @@
  1 +/*=========================================================================
  2 +
  3 + Program: Large Scale Segmentation (LSS)
  4 + Language: C++
  5 + author: Lassalle Pierre
  6 +
  7 +
  8 +
  9 + Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
  10 +
  11 +
  12 + This software is distributed WITHOUT ANY WARRANTY; without even
  13 + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  14 + PURPOSE. See the above copyright notices for more information.
  15 +
  16 +=========================================================================*/
  17 +#include <otbVectorImage.h>
  18 +#include "FLSAlgorithm.h"
  19 +#include "lss_RegionBasedTileBasedReduction.h"
  20 +#include "lss_RegionBasedTileBasedIterativeReduction.h"
  21 +#include "lss_RegionBasedFinisher.h"
  22 +
  23 +int main(int argc, char **argv)
  24 +{
  25 + if(argc != 9)
  26 + {
  27 + std::cerr << "Usage: " << argv[0] << ": \n"
  28 + << "[input image path (jpg, png, tiff, jpeg200)]\n"
  29 + << "[output image path (jpg, png, tiff)]\n"
  30 + << "[temporary directory path]\n"
  31 + << "[tile size according to the width]\n"
  32 + << "[tile size according to the height]\n"
  33 + << "[number of iteration for Tile-Based reduction step]\n"
  34 + << "[lambda threshold]\n"
  35 + << "[memory available (Mo)]\n";
  36 +
  37 + return 1;
  38 + }
  39 +
  40 + std::string input = argv[1];
  41 + std::string output = argv[2];
  42 + std::string tmpdir = argv[3];
  43 + unsigned int tilesizeX = atoi(argv[4]);
  44 + unsigned int tilesizeY = atoi(argv[5]);
  45 + unsigned int niter = atoi(argv[6]);
  46 + FLSParameter sparams = (double)atof(argv[7]);
  47 + long unsigned int memory = atoi(argv[8]);
  48 + unsigned int margin = static_cast<unsigned int>(pow(2, niter+1) - 2);
  49 +
  50 + typedef otb::VectorImage<float, 2> ImageType;
  51 + typedef FLSAlgorithm<ImageType> AlgorithmType;
  52 + typedef lss::RegionBasedTileBasedReduction<ImageType, AlgorithmType> TileBasedReductionType;
  53 + typedef lss::RegionBasedTileBasedIterativeReduction<AlgorithmType> TileBasedIterativeReductionType;
  54 + typedef lss::RegionBasedFinisher<AlgorithmType> FinisherType;
  55 +
  56 + // Tile Based Reduction step
  57 + std::cout << "***********************************\n";
  58 + std::cout << "* Tile Based Reduction Step *\n";
  59 + std::cout << "***********************************\n";
  60 + TileBasedReductionType tb_reduction;
  61 + tb_reduction.SetInputImagePath(input);
  62 + tb_reduction.SetTemporaryDirectoryPath(tmpdir);
  63 + tb_reduction.SetTileSizeX(tilesizeX);
  64 + tb_reduction.SetTileSizeY(tilesizeY);
  65 + tb_reduction.SetMarginOfStability(margin);
  66 + tb_reduction.SetNumberOfIterations(niter);
  67 + tb_reduction.SetSegmentationParameters(sparams);
  68 + tb_reduction.GenerateData();
  69 +
  70 + // Tile Based Iterative reduction step
  71 + std::cout << "*********************************************\n";
  72 + std::cout << "* Tile Based Iterative Reduction Step *\n";
  73 + std::cout << "*********************************************\n";
  74 + TileBasedIterativeReductionType tb_itreduction;
  75 + tb_itreduction.SetObserver(tb_reduction.GetObserver());
  76 + tb_itreduction.SetSegmentationParameters(sparams);
  77 + tb_itreduction.SetAvailableMemory(memory);
  78 + bool DoSegmentation = tb_itreduction.GenerateData();
  79 +
  80 + std::cout << "*********************************************\n";
  81 + std::cout << "* Aggregating and completion step *\n";
  82 + std::cout << "*********************************************\n";
  83 + FinisherType finisher;
  84 + finisher.SetOutputPath(output);
  85 + finisher.SetObserver(tb_itreduction.GetObserver());
  86 + finisher.SetDoSegmentation(DoSegmentation);
  87 + finisher.SetTiles(tb_itreduction.GetTiles());
  88 + finisher.SetSegmentationParameters(sparams);
  89 + finisher.GenerateData();
  90 +
  91 + return 0;
  92 +}
... ...
code/apps/TiledSpringSegmentation.cxx 0 → 100644
... ... @@ -0,0 +1,92 @@
  1 +/*=========================================================================
  2 +
  3 + Program: Large Scale Segmentation (LSS)
  4 + Language: C++
  5 + author: Lassalle Pierre
  6 +
  7 +
  8 +
  9 + Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
  10 +
  11 +
  12 + This software is distributed WITHOUT ANY WARRANTY; without even
  13 + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  14 + PURPOSE. See the above copyright notices for more information.
  15 +
  16 +=========================================================================*/
  17 +#include <otbVectorImage.h>
  18 +#include "SpringAlgorithm.h"
  19 +#include "lss_RegionBasedTileBasedReduction.h"
  20 +#include "lss_RegionBasedTileBasedIterativeReduction.h"
  21 +#include "lss_RegionBasedFinisher.h"
  22 +
  23 +int main(int argc, char **argv)
  24 +{
  25 + if(argc != 9)
  26 + {
  27 + std::cerr << "Usage: " << argv[0] << ": \n"
  28 + << "[input image path (jpg, png, tiff, jpeg200)]\n"
  29 + << "[output image path (jpg, png, tiff)]\n"
  30 + << "[temporary directory path]\n"
  31 + << "[tile size according to the width]\n"
  32 + << "[tile size according to the height]\n"
  33 + << "[number of iteration for Tile-Based reduction step]\n"
  34 + << "[euclidean distance threshold]\n"
  35 + << "[memory available (Mo)]\n";
  36 +
  37 + return 1;
  38 + }
  39 +
  40 + std::string input = argv[1];
  41 + std::string output = argv[2];
  42 + std::string tmpdir = argv[3];
  43 + unsigned int tilesizeX = atoi(argv[4]);
  44 + unsigned int tilesizeY = atoi(argv[5]);
  45 + unsigned int niter = atoi(argv[6]);
  46 + SpringParameter sparams = (double)atof(argv[7]);
  47 + long unsigned int memory = atoi(argv[8]);
  48 + unsigned int margin = static_cast<unsigned int>(pow(2, niter+1) - 2);
  49 +
  50 + typedef otb::VectorImage<float, 2> ImageType;
  51 + typedef SpringAlgorithm<ImageType> AlgorithmType;
  52 + typedef lss::RegionBasedTileBasedReduction<ImageType, AlgorithmType> TileBasedReductionType;
  53 + typedef lss::RegionBasedTileBasedIterativeReduction<AlgorithmType> TileBasedIterativeReductionType;
  54 + typedef lss::RegionBasedFinisher<AlgorithmType> FinisherType;
  55 +
  56 + // Tile Based Reduction step
  57 + std::cout << "***********************************\n";
  58 + std::cout << "* Tile Based Reduction Step *\n";
  59 + std::cout << "***********************************\n";
  60 + TileBasedReductionType tb_reduction;
  61 + tb_reduction.SetInputImagePath(input);
  62 + tb_reduction.SetTemporaryDirectoryPath(tmpdir);
  63 + tb_reduction.SetTileSizeX(tilesizeX);
  64 + tb_reduction.SetTileSizeY(tilesizeY);
  65 + tb_reduction.SetMarginOfStability(margin);
  66 + tb_reduction.SetNumberOfIterations(niter);
  67 + tb_reduction.SetSegmentationParameters(sparams);
  68 + tb_reduction.GenerateData();
  69 +
  70 + // Tile Based Iterative reduction step
  71 + std::cout << "*********************************************\n";
  72 + std::cout << "* Tile Based Iterative Reduction Step *\n";
  73 + std::cout << "*********************************************\n";
  74 + TileBasedIterativeReductionType tb_itreduction;
  75 + tb_itreduction.SetObserver(tb_reduction.GetObserver());
  76 + tb_itreduction.SetSegmentationParameters(sparams);
  77 + tb_itreduction.SetAvailableMemory(memory);
  78 + bool DoSegmentation = tb_itreduction.GenerateData();
  79 +
  80 + std::cout << "*********************************************\n";
  81 + std::cout << "* Aggregating and completion step *\n";
  82 + std::cout << "*********************************************\n";
  83 + FinisherType finisher;
  84 + finisher.SetOutputPath(output);
  85 + finisher.SetObserver(tb_itreduction.GetObserver());
  86 + finisher.SetDoSegmentation(DoSegmentation);
  87 + finisher.SetTiles(tb_itreduction.GetTiles());
  88 + finisher.SetSegmentationParameters(sparams);
  89 + finisher.GenerateData();
  90 +
  91 + return 0;
  92 +}
... ...
code/img/test.tif 0 → 100644
No preview for this file type
code/src/algo/BaatzAlgorithm.h 0 → 100644
... ... @@ -0,0 +1,138 @@
  1 +/*=========================================================================
  2 +
  3 + Program: Large Scale Segmentation (LSS)
  4 + Language: C++
  5 + author: Lassalle Pierre
  6 +
  7 +
  8 +
  9 + Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
  10 +
  11 +
  12 + This software is distributed WITHOUT ANY WARRANTY; without even
  13 + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  14 + PURPOSE. See the above copyright notices for more information.
  15 +
  16 +=========================================================================*/
  17 +#ifndef __BaatzAlgorithm_h
  18 +#define __BaatzAlgorithm_h
  19 +
  20 +#include <otbImage.h>
  21 +#include <otbVectorImage.h>
  22 +#include <otbImageFileReader.h>
  23 +#include <otbImageFileWriter.h>
  24 +#include <itkImageRegionIterator.h>
  25 +#include "lss_GenericLMBFRegionMergingHandler.h"
  26 +#include "lss_MacroGenerator.h"
  27 +
  28 +struct BaatzRegion : public lss::CRPT_Region<BaatzRegion>
  29 +{
  30 + // Specific attributes for Baatz & Shape criterion
  31 + float m_Area;
  32 + float m_Perimeter;
  33 + std::vector<float> m_Avg_Color;
  34 + std::vector<float> m_Avg_Color_Square;
  35 + std::vector<float> m_Color_Sum;
  36 + std::vector<float> m_Std_Color;
  37 +
  38 + // Serialization for the storage on the disk (information on the number of bands is considered)
  39 + unsigned int GetSizeOfData()
  40 + {
  41 + return (3 + 4*m_Avg_Color.size());
  42 + }
  43 +
  44 + // store elements in a contiguous way in the memory
  45 + void SerializeData(float * data_block)
  46 + {
  47 + data_block[0] = m_Area;
  48 + data_block[1] = m_Perimeter;
  49 + data_block[2] = static_cast<float>(m_Avg_Color.size());
  50 + for(unsigned int b =0; b<m_Avg_Color.size(); b++)
  51 + {
  52 + data_block[3+b] = m_Avg_Color[b];
  53 + data_block[3 + m_Avg_Color.size() + b] = m_Avg_Color_Square[b];
  54 + data_block[3 + 2*m_Avg_Color.size() + b] = m_Color_Sum[b];
  55 + data_block[3 + 3*m_Avg_Color.size() + b] = m_Std_Color[b];
  56 + }
  57 + }
  58 +
  59 + // Read from the contiguous block of memory returned
  60 + void InitAttributesFromSerializeData(float * data_block)
  61 + {
  62 + m_Area = data_block[0];
  63 + m_Perimeter = data_block[1];
  64 + unsigned int bands = static_cast<unsigned int>(data_block[2]);
  65 + m_Avg_Color.reserve(bands);
  66 + m_Avg_Color_Square.reserve(bands);
  67 + m_Color_Sum.reserve(bands);
  68 + m_Std_Color.reserve(bands);
  69 + for(unsigned int b=0; b<bands; b++)
  70 + {
  71 + m_Avg_Color.push_back(data_block[3+b]);
  72 + m_Avg_Color_Square.push_back(data_block[3 + bands + b]);
  73 + m_Color_Sum.push_back(data_block[3 + 2*bands + b]);
  74 + m_Std_Color.push_back(data_block[3 + 3*bands + b]);
  75 + }
  76 + }
  77 +};
  78 +
  79 +struct BaatzParameters
  80 +{
  81 + float m_ColorWeight;
  82 + float m_CompactnessWeight;
  83 + float m_Scale;
  84 +};
  85 +
  86 +template<class TInputImage>
  87 +class BaatzAlgorithm
  88 +{
  89 + public:
  90 +
  91 + // Mandatory typedef
  92 + typedef BaatzRegion RegionType;
  93 + typedef BaatzParameters ParameterType;
  94 + typedef std::shared_ptr<BaatzRegion> RegionPointerType;
  95 + typedef std::vector<RegionPointerType> RegionListType;
  96 + typedef TInputImage InputImageType;
  97 + typedef otb::ImageFileReader<InputImageType> ReaderType;
  98 + typedef otb::ImageFileWriter<InputImageType> WriterType;
  99 + typedef itk::ImageRegionIterator<InputImageType> IteratorType;
  100 + typedef itk::ImageRegion<2>::IndexType BboxIndexType;
  101 + typedef itk::ImageRegion<2>::SizeType BboxSizeType;
  102 +
  103 + // Optional: Label Image
  104 + typedef unsigned int LabelPixelType;
  105 + typedef otb::Image<LabelPixelType,2> LabelImageType;
  106 + typedef itk::ImageRegionIterator<LabelImageType> LabelIteratorType;
  107 + typedef otb::ImageFileWriter<LabelImageType> LabelWriterType;
  108 +
  109 + SetMacro(ParameterType, Parameters)
  110 + SetMacro(unsigned int, NumberOfIterations);
  111 + GetMacro(bool, PreviousMerge);
  112 + GetMacro(RegionListType, RegionList);
  113 + SetMacro(RegionListType, RegionList);
  114 + void SetDimensionForEncoder(unsigned int, unsigned int);
  115 +
  116 + void InitFromTiffImage(const std::string& input_file);
  117 + void Segmentation();
  118 + void WriteResultingImage(const std::string& ofname);
  119 + void WriteLabelImage(const std::string& ofname);
  120 +
  121 + private:
  122 +
  123 + // Specific methods to compute the fusion cost
  124 + double ColorComponentCostFusion(RegionPointerType r1, RegionPointerType r2);
  125 + double CompactnessComponentCostFusion(RegionPointerType r1, RegionPointerType r2);
  126 + void UpdateAttribute(RegionPointerType r1, RegionPointerType r2);
  127 +
  128 + bool m_PreviousMerge;
  129 + ParameterType m_Parameters;
  130 + unsigned int m_NumberOfIterations;
  131 + RegionListType m_RegionList;
  132 + lss::GenericLMBFRegionMergingHandler<BaatzRegion> m_RMHandler;
  133 + std::function<double(RegionPointerType, RegionPointerType)> m_ComputeCostFunction;
  134 +};
  135 +
  136 +#include "BaatzAlgorithm.txx"
  137 +
  138 +#endif
... ...
code/src/algo/BaatzAlgorithm.h~ 0 → 100644
... ... @@ -0,0 +1,138 @@
  1 +/*=========================================================================
  2 +
  3 + Program: Large Scale Segmentation (LSS)
  4 + Language: C++
  5 + author: Lassalle Pierre
  6 +
  7 +
  8 +
  9 + Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
  10 +
  11 +
  12 + This software is distributed WITHOUT ANY WARRANTY; without even
  13 + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  14 + PURPOSE. See the above copyright notices for more information.
  15 +
  16 +=========================================================================*/
  17 +#ifndef __BaatzAlgorithm_h
  18 +#define __BaatzAlgorithm_h
  19 +
  20 +#include <otbImage.h>
  21 +#include <otbVectorImage.h>
  22 +#include <otbImageFileReader.h>
  23 +#include <otbImageFileWriter.h>
  24 +#include <itkImageRegionIterator.h>
  25 +#include "lss_GenericLMBFRegionMergingHandler.h"
  26 +#include "lss_MacroGenerator.h"
  27 +
  28 +struct BaatzRegion : public lss::CRPT_Region<BaatzRegion>
  29 +{
  30 + // Specific attributes for Baatz & Shape criterion
  31 + float m_Area;
  32 + float m_Perimeter;
  33 + std::vector<float> m_Avg_Color;
  34 + std::vector<float> m_Avg_Color_Square;
  35 + std::vector<float> m_Color_Sum;
  36 + std::vector<float> m_Std_Color;
  37 +
  38 + // Serialization for the storage on the disk (information on the number of bands is considered)
  39 + unsigned int GetSizeOfData()
  40 + {
  41 + return (3 + 4*m_Avg_Color.size());
  42 + }
  43 +
  44 + // store elements in a contiguous way in the memory
  45 + void SerializeData(float * data_block)
  46 + {
  47 + data_block[0] = m_Area;
  48 + data_block[1] = m_Perimeter;
  49 + data_block[2] = static_cast<float>(m_Avg_Color.size());
  50 + for(unsigned int b =0; b<m_Avg_Color.size(); b++)
  51 + {
  52 + data_block[3+b] = m_Avg_Color[b];
  53 + data_block[3 + m_Avg_Color.size() + b] = m_Avg_Color_Square[b];
  54 + data_block[3 + 2*m_Avg_Color.size() + b] = m_Color_Sum[b];
  55 + data_block[3 + 3*m_Avg_Color.size() + b] = m_Std_Color[b];
  56 + }
  57 + }
  58 +
  59 + // Read from the contiguous block of memory returned
  60 + void InitAttributesFromSerializeData(float * data_block)
  61 + {
  62 + m_Area = data_block[0];
  63 + m_Perimeter = data_block[1];
  64 + unsigned int bands = static_cast<unsigned int>(data_block[2]);
  65 + m_Avg_Color.reserve(bands);
  66 + m_Avg_Color_Square.reserve(bands);
  67 + m_Color_Sum.reserve(bands);
  68 + m_Std_Color.reserve(bands);
  69 + for(unsigned int b=0; b<bands; b++)
  70 + {
  71 + m_Avg_Color.push_back(data_block[3+b]);
  72 + m_Avg_Color_Square.push_back(data_block[3 + bands + b]);
  73 + m_Color_Sum.push_back(data_block[3 + 2*bands + b]);
  74 + m_Std_Color.push_back(data_block[3 + 3*bands + b]);
  75 + }
  76 + }
  77 +};
  78 +
  79 +struct BaatzParameters
  80 +{
  81 + float m_ColorWeight;
  82 + float m_CompactnessWeight;
  83 + float m_Scale;
  84 +};
  85 +
  86 +template<class TInputImage>
  87 +class BaatzAlgorithm
  88 +{
  89 + public:
  90 +
  91 + // Mandatory typedef
  92 + typedef BaatzRegion RegionType;
  93 + typedef BaatzParameters ParameterType;
  94 + typedef std::shared_ptr<BaatzRegion> RegionPointerType;
  95 + typedef std::vector<RegionPointerType> RegionListType;
  96 + typedef TInputImage InputImageType;
  97 + typedef otb::ImageFileReader<InputImageType> ReaderType;
  98 + typedef otb::ImageFileWriter<InputImageType> WriterType;
  99 + typedef itk::ImageRegionIterator<InputImageType> IteratorType;
  100 + typedef itk::ImageRegion<2>::IndexType BboxIndexType;
  101 + typedef itk::ImageRegion<2>::SizeType BboxSizeType;
  102 +
  103 + // Optional: Label Image
  104 + typedef unsigned int LabelPixelType;
  105 + typedef otb::Image<LabelPixelType,2> LabelImageType;
  106 + typedef itk::ImageRegionIterator<LabelImageType> LabelIteratorType;
  107 + typedef otb::ImageFileWriter<LabelImageType> LabelWriterType;
  108 +
  109 + SetMacro(ParameterType, Parameters)
  110 + SetMacro(unsigned int, NumberOfIterations);
  111 + GetMacro(bool, PreviousMerge);
  112 + GetMacro(RegionListType, RegionList);
  113 + SetMacro(RegionListType, RegionList);
  114 + void SetDimensionForEncoder(unsigned int, unsigned int);
  115 +
  116 + void InitFromTiffImage(const std::string& input_file);
  117 + void Segmentation();
  118 + void WriteResultingImage(const std::string& ofname);
  119 + void WriteLabelImage(const std::string& ofname);
  120 +
  121 + private:
  122 +
  123 + // Specific methods to compute the fusion cost
  124 + double ColorComponentCostFusion(RegionPointerType r1, RegionPointerType r2);
  125 + double CompactnessComponentCostFusion(RegionPointerType r1, RegionPointerType r2);
  126 + void UpdateAttribute(RegionPointerType r1, RegionPointerType r2);
  127 +
  128 + bool m_PreviousMerge;
  129 + ParameterType m_Parameters;
  130 + unsigned int m_NumberOfIterations;
  131 + RegionListType m_RegionList;
  132 + lss::GenericLMBFRegionMergingHandler<BaatzRegion> m_RMHandler;
  133 + std::function<double(RegionPointerType, RegionPointerType)> m_ComputeCostFunction;
  134 +};
  135 +
  136 +#include "BaatzAlgorithm.txx"
  137 +
  138 +#endif
... ...
code/src/algo/BaatzAlgorithm.txx 0 → 100644
... ... @@ -0,0 +1,336 @@
  1 +/*=========================================================================
  2 +
  3 + Program: Large Scale Segmentation (LSS)
  4 + Language: C++
  5 + author: Lassalle Pierre
  6 +
  7 +
  8 +
  9 + Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
  10 +
  11 +
  12 + This software is distributed WITHOUT ANY WARRANTY; without even
  13 + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  14 + PURPOSE. See the above copyright notices for more information.
  15 +
  16 +=========================================================================*/
  17 +#ifndef __BaatzAlgorithm_txx
  18 +#define __BaatzAlgorithm_txx
  19 +
  20 +template<class TInputImage>
  21 +void
  22 +BaatzAlgorithm<TInputImage>::SetDimensionForEncoder(unsigned int cols, unsigned int rows)
  23 +{
  24 + m_RMHandler.m_Encoder.SetCols(cols);
  25 + m_RMHandler.m_Encoder.SetRows(rows);
  26 +}
  27 +
  28 +template<class TInputImage>
  29 +void
  30 +BaatzAlgorithm<TInputImage>::InitFromTiffImage(const std::string& input_file)
  31 +{
  32 + auto reader = ReaderType::New();
  33 + reader->SetFileName(input_file);
  34 + reader->Update();
  35 +
  36 + auto regionToProcess = reader->GetOutput()->GetLargestPossibleRegion();
  37 + unsigned int cols = regionToProcess.GetSize()[0];
  38 + unsigned int rows = regionToProcess.GetSize()[1];
  39 + unsigned int bands = reader->GetOutput()->GetNumberOfComponentsPerPixel();
  40 +
  41 + m_RMHandler.m_Encoder.SetRows(rows); // Penser à n'utiliser qu'une seule méthode (SetRegionToProcess)
  42 + m_RMHandler.m_Encoder.SetCols(cols);
  43 +
  44 + m_RegionList.reserve(cols*rows);
  45 +
  46 + IteratorType it(reader->GetOutput(), regionToProcess);
  47 +
  48 + // Create the vertices
  49 + long unsigned int idx = 0;
  50 + for(it.GoToBegin(); !it.IsAtEnd(); ++it)
  51 + {
  52 + auto r = std::make_shared<BaatzRegion>();
  53 + m_RMHandler.InitGenericRegion(r, idx);
  54 + r->m_Area = 1;
  55 + r->m_Perimeter = 4;
  56 + r->m_Avg_Color.reserve(bands);
  57 + r->m_Avg_Color_Square.reserve(bands);
  58 + r->m_Color_Sum.reserve(bands);
  59 + r->m_Std_Color.reserve(bands);
  60 +
  61 + for(unsigned int b = 0; b < bands; b++)
  62 + {
  63 + r->m_Avg_Color.push_back(it.Get()[b]);
  64 + r->m_Avg_Color_Square.push_back(pow(it.Get()[b], 2));
  65 + r->m_Color_Sum.push_back(it.Get()[b]);
  66 + r->m_Std_Color.push_back(0);
  67 + }
  68 +
  69 + m_RegionList.push_back(r);
  70 + ++idx;
  71 + }
  72 + m_RMHandler.InitNeighborhood(m_RegionList);
  73 +}
  74 +
  75 +template<class TInputImage>
  76 +double
  77 +BaatzAlgorithm<TInputImage>::ColorComponentCostFusion(RegionPointerType r1, RegionPointerType r2)
  78 +{
  79 + unsigned int bands = r1->m_Avg_Color.size();
  80 + float mean[bands], colorSum[bands];
  81 + float squarePixels[bands];
  82 + float stddev[bands];
  83 + float stddevNew[bands];
  84 + double color_f[bands];
  85 + double color_h;
  86 + float a_current, a_neighbor, a_sum;
  87 + a_current = r1->m_Area;
  88 + a_neighbor = r2->m_Area;
  89 + a_sum = a_current+a_neighbor;
  90 +
  91 + for (unsigned int b = 0; b < bands; b++)
  92 + {
  93 + mean[b] = ((r1->m_Avg_Color[b]*a_current)+(r2->m_Avg_Color[b]*a_neighbor))/a_sum;
  94 + squarePixels[b] = (r1->m_Avg_Color_Square[b])+(r2->m_Avg_Color_Square[b]);
  95 + colorSum[b] = r1->m_Color_Sum[b] + r2->m_Color_Sum[b];
  96 + stddev[b] = 0;
  97 + stddevNew[b] = 0;
  98 + }
  99 +
  100 + for(unsigned int b = 0; b < bands; b++)
  101 + {
  102 + stddevNew[b] = squarePixels[b] - 2*mean[b]*colorSum[b] + a_sum*mean[b]*mean[b];
  103 + }
  104 +
  105 + /* calculates color factor per band and total */
  106 + color_h = 0;
  107 + for (unsigned int b = 0; b < bands; b++)
  108 + {
  109 + stddev[b] = sqrt(stddevNew[b]/a_sum);
  110 + color_f[b] = (a_current*r1->m_Std_Color[b]) + (a_neighbor*r2->m_Std_Color[b]);
  111 + color_f[b] = (a_sum*stddev[b])- color_f[b];
  112 + color_h += color_f[b];
  113 + }
  114 + return color_h;
  115 +}
  116 +
  117 +template<class TInputImage>
  118 +double
  119 +BaatzAlgorithm<TInputImage>::CompactnessComponentCostFusion(RegionPointerType r1, RegionPointerType r2)
  120 +{
  121 + double spatial_h, smooth_f, compact_f;
  122 + float area[3], perimeter[3], b_box_len[3]; /* 0-current segment; 1-neighbor segment; 2-merged (new) segment */
  123 +
  124 + /* area */
  125 + area[0] = r1->m_Area;
  126 + area[1] = r2->m_Area;
  127 + area[2] = area[0]+area[1];
  128 +
  129 + /* perimeter */
  130 + perimeter[0] = r1->m_Perimeter;
  131 + perimeter[1] = r2->m_Perimeter;
  132 + perimeter[2] = r1->m_Perimeter + r2->m_Perimeter - 2*m_RMHandler.GetConnections(r1, r2);
  133 +
  134 + /* bounding box lenght */
  135 + auto mbbox = m_RMHandler.GetResultingBbox(r1, r2);
  136 + b_box_len[0] = (r1->m_Bbox.GetSize(0))*2 + (r1->m_Bbox.GetSize(1))*2;
  137 + b_box_len[1] = (r2->m_Bbox.GetSize(0))*2 + (r2->m_Bbox.GetSize(1))*2;
  138 + b_box_len[2] = (mbbox.GetSize(0))*2 + (mbbox.GetSize(1))*2;
  139 +
  140 + /* smoothness factor */
  141 + smooth_f = (area[2]*perimeter[2]/b_box_len[2] -
  142 + (area[1]*perimeter[1]/b_box_len[1] + area[0]*perimeter[0]/b_box_len[0]));
  143 +
  144 + /* compactness factor */
  145 + compact_f = (area[2]*perimeter[2]/sqrt(area[2]) -
  146 + (area[1]*perimeter[1]/sqrt(area[1]) + area[0]*perimeter[0]/sqrt(area[0])));
  147 +
  148 + /* spatial heterogeneity */
  149 + spatial_h = m_Parameters.m_CompactnessWeight*compact_f + (1-m_Parameters.m_CompactnessWeight)*smooth_f;
  150 +
  151 + return spatial_h;
  152 +}
  153 +
  154 +template<class TInputImage>
  155 +void
  156 +BaatzAlgorithm<TInputImage>::Segmentation()
  157 +{
  158 + m_PreviousMerge = false;
  159 + unsigned int max_iter = 500;
  160 + if(m_NumberOfIterations > 0)
  161 + max_iter = m_NumberOfIterations;
  162 +
  163 + unsigned int curr_step = 0;
  164 + double cost;
  165 + RegionPointerType bestneigh = nullptr;
  166 + bool prev_merged = true;
  167 +
  168 + m_ComputeCostFunction = [&](RegionPointerType r1, RegionPointerType r2)->double
  169 + {
  170 + double spectral_h;
  171 + double spatial_h;
  172 + double cost = 0;
  173 +
  174 + spectral_h = ColorComponentCostFusion(r1, r2);
  175 + cost += m_Parameters.m_ColorWeight * spectral_h;
  176 +
  177 + if(cost < m_Parameters.m_Scale)
  178 + {
  179 + spatial_h = CompactnessComponentCostFusion(r1, r2);
  180 + cost += (1-m_Parameters.m_ColorWeight)*spatial_h;
  181 + return cost;
  182 + }
  183 + else
  184 + return cost;
  185 + };
  186 +
  187 + while(curr_step < max_iter && prev_merged)
  188 + {
  189 + prev_merged = false;
  190 + std::cout << curr_step << std::endl;
  191 + m_RMHandler.UpdateMergingCost(m_RegionList, m_ComputeCostFunction);
  192 +
  193 + for(auto& r: m_RegionList)
  194 + {
  195 + if(m_RMHandler.IsLMBF(r, m_Parameters.m_Scale))
  196 + {