LabelMapToVectorData.cxx 6.39 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*=========================================================================

  Program:   ORFEO Toolbox
  Language:  C++
  Date:      $Date$
  Version:   $Revision$


  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
  See OTBCopyright.txt for details.


     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/

Manuel Grizonnet's avatar
Manuel Grizonnet committed
19 20 21 22 23 24 25

//  Software Guide : BeginCommandLineArgs
//    INPUTS: {rcc8_mire1.png}
//    OUTPUTS: {rcc8_mire2_vectorizer.shp}
//  Software Guide : EndCommandLineArgs


26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
// Software Guide : BeginLatex
//
// \index{otb::LabelMapToVectorDataFilter}
//
//
// This class vectorizes a LabelObject to a VectorData.
//
// LabelMapToVectorDataFilter converts a LabelMap to a
// VectorData where all the pixels get the attribute value of the label object they belong.
// It uses the class otbLabelObjectToPolygonFunctor wich follows a finite states machine described in:
//
// "An algorithm for the rapid computation of boundaries of run-length
// encoded regions", Francis K. H. Queck, in Pattern Recognition 33
// (2000), p 1637-1649.
//
41
//  Only polygon conversion is available yet.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
// Software Guide : EndLatex

#include "otbImageFileReader.h"
#include "otbVectorDataFileWriter.h"
#include "otbVectorData.h"
#include "otbVectorDataProjectionFilter.h"

#include <fstream>
#include <iostream>

#include "otbImage.h"
#include "otbLabelMapToVectorDataFilter.h"
#include "otbAttributesMapLabelObject.h"
#include "itkLabelImageToLabelMapFilter.h"

int main(int argc, char * argv[])
{
  /** Use the labelObjecttopolygon functor (not thread safe) only polygon conversion is available yet*/
  if ( argc != 3 )
  {
    std::cerr << "Usage: " << argv[0];
    std::cerr << " inputImageFile outputVectorfile(shp)" << std::endl;
    return EXIT_FAILURE;
  }
    const char * infname = argv[1];
    const char * outfname = argv[2];
 
Manuel Grizonnet's avatar
Manuel Grizonnet committed
69 70
  //  Software Guide : BeginLatex
  //
71
  // The image types are defined using pixel types and
72 73 74 75 76 77
  // dimension. The input image is defined as an \doxygen{itk}{Image},
  // the output is a \doxygen{otb}{VectorData}.
  //
  // Software Guide : EndLatex

  // Software Guide : BeginCodeSnippet
Manuel Grizonnet's avatar
Manuel Grizonnet committed
78 79 80 81
    const unsigned int Dimension               = 2;
    typedef unsigned short                    LabelType;
    typedef otb::Image<LabelType,Dimension>   LabeledImageType;
    typedef otb::VectorData<double,2>         VectorDataType;
82 83 84
  // Software Guide : EndCodeSnippet

    // We instantiate reader and writer types
85
    typedef otb::ImageFileReader<LabeledImageType>    LabeledReaderType;
86 87 88 89 90 91 92
    typedef otb::VectorDataFileWriter<VectorDataType> WriterType;

    // Label map typedef
  //  Software Guide : BeginLatex
  //
  // The Attribute Label Map is
  // instantiated using the image pixel types as template parameters.
93
  // The LabelObjectToPolygonFunctor is instantiated with LabelObjectType and PolygonType.
94 95 96
  //  Software Guide : EndLatex

  // Software Guide : BeginCodeSnippet
Manuel Grizonnet's avatar
Manuel Grizonnet committed
97 98 99 100
    typedef otb::AttributesMapLabelObject<LabelType,Dimension,double>              LabelObjectType;
    typedef itk::LabelMap<LabelObjectType>                                         LabelMapType;
    typedef itk::LabelImageToLabelMapFilter<LabeledImageType,LabelMapType>         LabelMapFilterType;
    typedef otb::Polygon<double>                                                   PolygonType;
101
    typedef otb::Functor::LabelObjectToPolygonFunctor<LabelObjectType,PolygonType> FunctorType;
102
    // Software Guide : EndCodeSnippet
Manuel Grizonnet's avatar
Manuel Grizonnet committed
103
    typedef VectorDataType::DataNodeType 					   DataNodeType;
104
    
Manuel Grizonnet's avatar
Manuel Grizonnet committed
105
    typedef otb::VectorDataProjectionFilter<VectorDataType,VectorDataType>         VectorDataFilterType;
106 107 108 109 110 111


    LabeledReaderType::Pointer lreader = LabeledReaderType::New();
    WriterType::Pointer writer = WriterType::New();

  //  Software Guide : BeginLatex
112 113
  //  Now the reader and writer are instantiated and 
  //  the input image is set and a name is given to the output image.
114 115
  //
  //  Software Guide : EndLatex
Manuel Grizonnet's avatar
Manuel Grizonnet committed
116

117 118 119 120 121 122 123
     // Software Guide : BeginCodeSnippet
    lreader->SetFileName(infname);
    writer->SetFileName(outfname);
     // Software Guide : EndCodeSnippet

  //  Software Guide : BeginLatex
  //
Manuel Grizonnet's avatar
Manuel Grizonnet committed
124
  //  Then, the input image is converted to a map of label objects.
125
  //  Here each white region connected regions are converted. So the background is define all zero pixels. 
126 127 128 129 130 131 132 133 134 135 136 137 138
  //  Software Guide : EndLatex


  // Software Guide : BeginCodeSnippet
    LabelMapFilterType::Pointer labelMapFilter = LabelMapFilterType::New();
    labelMapFilter->SetInput(lreader->GetOutput());
    labelMapFilter->SetBackgroundValue(itk::NumericTraits<LabelType>::min());
    labelMapFilter->Update();
  // Software Guide : EndCodeSnippet  


    //  Software Guide : BeginLatex
    //
139 140
    //  Then, the \doxygen{otb}{LabelMapToVectorDataFilter} is instantiated. This is
    // the main filter which proceed the vectorization. 
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
    //  Software Guide : EndLatex
    
    // Software Guide : BeginCodeSnippet
    typedef otb::LabelMapToVectorDataFilter< LabelMapType , VectorDataType > LabelMapToVectorDataFilterType;

    LabelMapToVectorDataFilterType::Pointer MyFilter = LabelMapToVectorDataFilterType::New();
    
    MyFilter->SetInput(labelMapFilter->GetOutput());
    MyFilter->Update();
    MyFilter->GetOutput()->SetProjectionRef(lreader->GetOutput()->GetProjectionRef());
    // Software Guide : EndCodeSnippet  
    
  //  Software Guide : BeginLatex
  //
  //  The output can be passed to a writer.
  //
  //  Software Guide : EndLatex

  // Software Guide : BeginCodeSnippet
      writer->SetInput( MyFilter->GetOutput());
161
  // Software Guide : EndCodeSnippet
162 163 164 165

  //  Software Guide : BeginLatex
  //
  //  The invocation of the \code{Update()} method on the writer triggers the
166
  //  execution of the pipeline.  As usualn, it is recommended to place update calls in a
167 168 169
  //  \code{try/catch} block in case errors occur and exceptions are thrown.
  //
  //  Software Guide : EndLatex
170
  // Software Guide : BeginCodeSnippet
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
  try
  {
    writer->Update();
    return EXIT_SUCCESS;
  }
  catch ( itk::ExceptionObject & excep )
  {
    std::cerr << "Exception caught !" << std::endl;
    std::cerr << excep << std::endl;
  }
  // Software Guide : EndCodeSnippet
  catch ( ... )
  {
    std::cout << "Unknown exception !" << std::endl;
    return EXIT_FAILURE;
  }
Otmane Lahlou's avatar
Otmane Lahlou committed
187 188

  return EXIT_SUCCESS;
189 190
  
}