ShapeAttributeComputation.cxx 3.55 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*=========================================================================

  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.

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


//  Software Guide : BeginCommandLineArgs
//    INPUTS: {MSLabeledOutput.tif}
//    OUTPUTS: {OBIAShapeAttribute.txt}
//  Software Guide : EndCommandLineArgs

//  Software Guide : BeginLatex
//
Emmanuel Christophe's avatar
Emmanuel Christophe committed
27
//  This example shows the
28 29 30 31 32 33 34 35 36 37 38
//
//  Software Guide : EndLatex

// Software Guide : BeginCodeSnippet
#include "itkShapeLabelObject.h"
#include "itkLabelMap.h"
#include "itkLabelImageToLabelMapFilter.h"
#include "itkShapeLabelMapFilter.h"

// Software Guide : EndCodeSnippet
#include "otbImageFileReader.h"
39
#include <fstream>
40 41 42 43 44 45 46 47 48

int main(int argc, char * argv[])
{
  const int dim = 2;
  typedef unsigned long PixelType;
  typedef itk::Image< PixelType, dim >    ImageType;
  
  if( argc != 3)
    {
49 50
    std::cerr << "usage: " << argv[0] << " input outputcentroidlist" << std::endl;
    return EXIT_FAILURE;
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    }

  // read the input image
  typedef itk::ImageFileReader< ImageType > ReaderType;
  ReaderType::Pointer reader = ReaderType::New();
  reader->SetFileName( argv[1] );
  
  // define the object type. Here the ShapeLabelObject type
  // is chosen in order to read some attribute related to the shape
  // of the objects (by opposition to the content of the object, with
  // the StatisticsLabelObejct).
  typedef unsigned long LabelType;
  typedef itk::ShapeLabelObject< LabelType, dim > LabelObjectType;
  typedef itk::LabelMap< LabelObjectType > LabelMapType;

  // convert the image in a collection of objects
  typedef itk::LabelImageToLabelMapFilter< ImageType, LabelMapType > ConverterType;
  ConverterType::Pointer converter = ConverterType::New();
  converter->SetInput( reader->GetOutput() );
//  converter->SetForegroundValue( atoi(argv[2]) );

  typedef itk::ShapeLabelMapFilter< LabelMapType > ShapeFilterType;
  ShapeFilterType::Pointer shape = ShapeFilterType::New();

  shape->SetInput( converter->GetOutput() );
  // update the shape filter, so its output will be up to date
  shape->Update();

  std::cout << "Nb. objects conv. " << converter->GetOutput()->GetNumberOfLabelObjects() << std::endl;

    std::cout << "Nb. objects shape " << shape->GetOutput()->GetNumberOfLabelObjects() << std::endl;
  // then we can read the attribute values we're interested in. The BinaryImageToShapeLabelMapFilter
  // produce consecutive labels, so we can use a for loop and GetLabelObject() method to retrieve
  // the label objects. If the labels are not consecutive, the GetNthLabelObject() method must be
85 86
  // use instead of GetLabelObject(), or an iterator on the label
  // object container of the label map.
87
    std::ofstream outfile( argv[2] );
88 89


90 91 92 93 94 95
  LabelMapType::Pointer labelMap = shape->GetOutput();
  for( unsigned long label=1; label<=labelMap->GetNumberOfLabelObjects(); label++ )
    {
    // we don't need a SmartPointer of the label object here, because the reference is kept in
    // in the label map.
    const LabelObjectType * labelObject = labelMap->GetLabelObject( label );
96
    outfile << label << "\t" << labelObject->GetPhysicalSize() << "\t" << labelObject->GetCentroid() << std::endl;
97
    }
98 99

  outfile.close();
100
  return EXIT_SUCCESS;
101 102
}