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

//  Software Guide : BeginCommandLineArgs
Manuel Grizonnet's avatar
Manuel Grizonnet committed
21 22
//    INPUTS: {MSLabeledOutput.tif}
//    OUTPUTS: {OBIAI2L2IOutput.tif}, {OBIAI2L2IInputPretty.png}, {OBIAI2L2IOutputPretty.png}
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
//  This example shows the basic approach for the transformation of a
29
//  segmented (labeled) image into a LabelObjectMap and then back to
Jordi Inglada's avatar
Jordi Inglada committed
30 31
//  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
    exit(1);
    }

57 58 59 60 61 62
  //  Software Guide : BeginLatex
  //
  // The image types are defined using pixel types and
  // dimension. The input image is defined as an \doxygen{otb}{Image}.
  //
  // Software Guide : EndLatex
Jordi Inglada's avatar
Jordi Inglada committed
63

64
  // Software Guide : BeginCodeSnippet
Manuel Grizonnet's avatar
Manuel Grizonnet committed
65 66 67
  const int dim                              = 2;
  typedef unsigned short                     PixelType;
  typedef otb::Image< PixelType, dim >       ImageType;
Manuel Grizonnet's avatar
Manuel Grizonnet committed
68
  
Jordi Inglada's avatar
Jordi Inglada committed
69
  typedef itk::LabelObject< PixelType, dim > LabelObjectType;
Manuel Grizonnet's avatar
Manuel Grizonnet committed
70
  typedef itk::LabelMap< LabelObjectType >   LabelMapType;
Manuel Grizonnet's avatar
Manuel Grizonnet committed
71
  // Software Guide : EndCodeSnippet
72

73 74
  //  Software Guide : BeginLatex
  //
Manuel Grizonnet's avatar
Manuel Grizonnet committed
75
  // As usual, the reader is instantiated and 
76 77 78 79 80
  // the input image is set. 
  //
  // Software Guide : EndLatex

  // Software Guide : BeginCodeSnippet
81 82 83
  typedef itk::ImageFileReader< ImageType > ReaderType;
  ReaderType::Pointer reader = ReaderType::New();
  reader->SetFileName( argv[1] );
84

85 86 87 88
  // Software Guide : EndCodeSnippet
  
  //  Software Guide : BeginLatex
  //
89
  // Then the binary image is transformed to a collection 
90 91 92 93 94
  // of label objects. Arguments are:
  // \begin{itemize}
  // \item \code{FullyConnected}: Set whether the connected 
  // components are defined strictly by face connectivity or by 
  // face+edge+vertex connectivity. Default is FullyConnectedOff.  
95
  // \item \code{InputForegroundValue/OutputBackgroundValue}: Specify the
96 97 98 99 100 101
  // pixel value of input/output of the foreground/background.   
  // \end{itemize} 
  //
  // Software Guide : EndLatex

  // Software Guide : BeginCodeSnippet
102 103 104
  typedef itk::BinaryImageToLabelMapFilter< ImageType, LabelMapType> I2LType;
  I2LType::Pointer i2l = I2LType::New();
  i2l->SetInput( reader->GetOutput() );
105
  i2l->SetFullyConnected( atoi(argv[5]) );
106 107
  i2l->SetInputForegroundValue( atoi(argv[6]) );
  i2l->SetOutputBackgroundValue( atoi(argv[7]) );
108 109 110 111
  // Software Guide : EndCodeSnippet

  //  Software Guide : BeginLatex
  //
112
  // Then the inverse process is used to recreate a image of labels.
113 114 115 116
  // The \doxygen{itk}{LabelMapToLabelImageFilter} converts a 
  // LabelMap to a labeled image. 
  //
  // Software Guide : EndLatex
117

118
  // Software Guide : BeginCodeSnippet
119 120 121
  typedef itk::LabelMapToLabelImageFilter< LabelMapType, ImageType> L2IType;
  L2IType::Pointer l2i = L2IType::New();
  l2i->SetInput( i2l->GetOutput() );
122 123 124 125
  // Software Guide : EndCodeSnippet

  //  Software Guide : BeginLatex
  //
126
  //  The output can be passed to a writer. The invocation 
127 128 129 130 131 132
  //  of the \code{Update()} method on the writer triggers the
  //  execution of the pipeline.
  //
  //  Software Guide : EndLatex
  
  // Software Guide : BeginCodeSnippet
133 134 135 136 137
  typedef itk::ImageFileWriter< ImageType > WriterType;
  WriterType::Pointer writer = WriterType::New();
  writer->SetInput( l2i->GetOutput() );
  writer->SetFileName( argv[2] );
  writer->Update();
138
  // Software Guide : EndCodeSnippet
139

140
  // Pretty image creation for the printing
Manuel Grizonnet's avatar
Manuel Grizonnet committed
141 142 143
  typedef otb::Image<unsigned char, dim>                                      OutputPrettyImageType;
  typedef otb::ImageFileWriter<OutputPrettyImageType>                         WriterPrettyType;
  typedef itk::RescaleIntensityImageFilter< ImageType, OutputPrettyImageType> RescalerType;
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159


  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();

160 161
  // Software Guide : BeginLatex
  //
162
  // Figure~\ref{fig:ImageToLabel} shows the effect of transforming an
163 164 165 166 167 168
  // image into a label object map and back to an image
  //
  // \begin{figure}
  // \center
  // \includegraphics[width=0.44\textwidth]{OBIAI2L2IInputPretty.eps}
  // \includegraphics[width=0.44\textwidth]{OBIAI2L2IOutputPretty.eps}
169
  // \itkcaption[Image to Label Object Map]{Transforming an
170 171 172 173 174
  // image (left) into a label object map and back to an image (right).}
  // \label{fig:ImageToLabel}
  // \end{figure}
  //  Software Guide : EndLatex

175 176
  return 0;
}