ImageToLabelToImage.cxx 3.48 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.

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

Jordi Inglada's avatar
Jordi Inglada committed
19 20 21

//  Software Guide : BeginCommandLineArgs
//    INPUTS: {MSLabeledOutput.tif}
22
//    OUTPUTS: {OBIAI2L2IOutput.tif}, {OBIAI2L2IInputPretty.tif}, {OBIAI2L2IOutputPretty.tif}
Jordi Inglada's avatar
Jordi Inglada committed
23 24 25
//    1 1108 0
//  Software Guide : EndCommandLineArgs

26 27
//  Software Guide : BeginLatex
//
Jordi Inglada's avatar
Jordi Inglada committed
28 29 30 31
//  This example shows the basic approach for the transformation of a
//  segmenred (labeled) image into a LabelObjectMap and then back to
//  an image. For this matter we will need the following header files
//  which contain the basic classes.
32 33 34 35
//
//  Software Guide : EndLatex

// Software Guide : BeginCodeSnippet
Jordi Inglada's avatar
Jordi Inglada committed
36 37 38 39
#include "itkLabelObject.h"
#include "itkLabelMap.h"
#include "itkBinaryImageToLabelMapFilter.h"
#include "itkLabelMapToLabelImageFilter.h"
40 41

// Software Guide : EndCodeSnippet
42
#include "otbImage.h"
43 44
#include "otbImageFileReader.h"
#include "otbImageFileWriter.h"
45
#include "itkRescaleIntensityImageFilter.h"
46 47 48 49 50


int main(int argc, char * argv[])
{

51
  if( argc != 8 )
52
    {
53
    std::cerr << "usage: " << argv[0] << " input output inPretty outPretty conn fg bg" << std::endl;
54 55 56 57
    exit(1);
    }

  const int dim = 2;
Jordi Inglada's avatar
Jordi Inglada committed
58 59

  typedef unsigned short PixelType;
60

61
  typedef otb::Image< PixelType, dim > ImageType;
62

Jordi Inglada's avatar
Jordi Inglada committed
63
  typedef itk::LabelObject< PixelType, dim > LabelObjectType;
64
  typedef itk::LabelMap< LabelObjectType > LabelMapType;
65

66 67 68
  typedef itk::ImageFileReader< ImageType > ReaderType;
  ReaderType::Pointer reader = ReaderType::New();
  reader->SetFileName( argv[1] );
69

70 71 72
  typedef itk::BinaryImageToLabelMapFilter< ImageType, LabelMapType> I2LType;
  I2LType::Pointer i2l = I2LType::New();
  i2l->SetInput( reader->GetOutput() );
73
  i2l->SetFullyConnected( atoi(argv[5]) );
74 75
  i2l->SetInputForegroundValue( atoi(argv[6]) );
  i2l->SetOutputBackgroundValue( atoi(argv[7]) );
76 77 78 79 80 81 82 83 84 85 86

  typedef itk::LabelMapToLabelImageFilter< LabelMapType, ImageType> L2IType;
  L2IType::Pointer l2i = L2IType::New();
  l2i->SetInput( i2l->GetOutput() );

  typedef itk::ImageFileWriter< ImageType > WriterType;
  WriterType::Pointer writer = WriterType::New();
  writer->SetInput( l2i->GetOutput() );
  writer->SetFileName( argv[2] );
  writer->Update();

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
  // Pretty image creation for the printing
  typedef otb::Image<unsigned char, dim>                                        OutputPrettyImageType;
  typedef otb::ImageFileWriter<OutputPrettyImageType>                                 WriterPrettyType;
  typedef itk::RescaleIntensityImageFilter< ImageType, OutputPrettyImageType>   RescalerType;


  RescalerType::Pointer     rescaler     = RescalerType::New();
  WriterPrettyType::Pointer prettyWriter = WriterPrettyType::New();
  rescaler->SetInput( reader->GetOutput() );
  rescaler->SetOutputMinimum(0);
  rescaler->SetOutputMaximum(255);
  prettyWriter->SetFileName( argv[3] );
  prettyWriter->SetInput( rescaler->GetOutput() );
  prettyWriter->Update();

  rescaler->SetInput( l2i->GetOutput() );
  prettyWriter->SetFileName( argv[4] );
  prettyWriter->SetInput( rescaler->GetOutput() );
  prettyWriter->Update();

107 108 109 110 111 112 113 114 115
  return 0;
}