ImageToLabelToImage.cxx 5.84 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
//  Software Guide : BeginCommandLineArgs
Manuel Grizonnet's avatar
Manuel Grizonnet committed
20 21
//    INPUTS: {MSLabeledOutput.tif}
//    OUTPUTS: {OBIAI2L2IOutput.tif}, {OBIAI2L2IInputPretty.png}, {OBIAI2L2IOutputPretty.png}
Jordi Inglada's avatar
Jordi Inglada committed
22 23 24
//    1 1108 0
//  Software Guide : EndCommandLineArgs

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

// Software Guide : BeginCodeSnippet
Jordi Inglada's avatar
Jordi Inglada committed
35 36 37 38
#include "itkLabelObject.h"
#include "itkLabelMap.h"
#include "itkBinaryImageToLabelMapFilter.h"
#include "itkLabelMapToLabelImageFilter.h"
39
// Software Guide : EndCodeSnippet
40
#include "otbImage.h"
41 42
#include "otbImageFileReader.h"
#include "otbImageFileWriter.h"
43
#include "itkRescaleIntensityImageFilter.h"
44 45 46 47

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

OTB Bot's avatar
STYLE  
OTB Bot committed
48
  if (argc != 8)
49
    {
OTB Bot's avatar
STYLE  
OTB Bot committed
50 51
    std::cerr << "usage: " << argv[0] <<
    " input output inPretty outPretty conn fg bg" << std::endl;
52 53 54
    exit(1);
    }

55 56 57 58 59 60
  //  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
61

62
  // Software Guide : BeginCodeSnippet
Manuel Grizonnet's avatar
Manuel Grizonnet committed
63
  const int dim                              = 2;
OTB Bot's avatar
STYLE  
OTB Bot committed
64 65 66 67 68
  typedef unsigned short             PixelType;
  typedef otb::Image<PixelType, dim> ImageType;

  typedef itk::LabelObject<PixelType, dim> LabelObjectType;
  typedef itk::LabelMap<LabelObjectType>   LabelMapType;
Manuel Grizonnet's avatar
Manuel Grizonnet committed
69
  // Software Guide : EndCodeSnippet
70

71 72
  //  Software Guide : BeginLatex
  //
OTB Bot's avatar
STYLE  
OTB Bot committed
73 74
  // As usual, the reader is instantiated and
  // the input image is set.
75 76 77 78
  //
  // Software Guide : EndLatex

  // Software Guide : BeginCodeSnippet
OTB Bot's avatar
STYLE  
OTB Bot committed
79
  typedef itk::ImageFileReader<ImageType> ReaderType;
80
  ReaderType::Pointer reader = ReaderType::New();
OTB Bot's avatar
STYLE  
OTB Bot committed
81
  reader->SetFileName(argv[1]);
82
  // Software Guide : EndCodeSnippet
OTB Bot's avatar
STYLE  
OTB Bot committed
83

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

  // Software Guide : BeginCodeSnippet
OTB Bot's avatar
STYLE  
OTB Bot committed
99
  typedef itk::BinaryImageToLabelMapFilter<ImageType, LabelMapType> I2LType;
100
  I2LType::Pointer i2l = I2LType::New();
OTB Bot's avatar
STYLE  
OTB Bot committed
101 102 103 104
  i2l->SetInput(reader->GetOutput());
  i2l->SetFullyConnected(atoi(argv[5]));
  i2l->SetInputForegroundValue(atoi(argv[6]));
  i2l->SetOutputBackgroundValue(atoi(argv[7]));
105 106 107 108
  // Software Guide : EndCodeSnippet

  //  Software Guide : BeginLatex
  //
109
  // Then the inverse process is used to recreate a image of labels.
OTB Bot's avatar
STYLE  
OTB Bot committed
110 111
  // The \doxygen{itk}{LabelMapToLabelImageFilter} converts a
  // LabelMap to a labeled image.
112 113
  //
  // Software Guide : EndLatex
114

115
  // Software Guide : BeginCodeSnippet
OTB Bot's avatar
STYLE  
OTB Bot committed
116
  typedef itk::LabelMapToLabelImageFilter<LabelMapType, ImageType> L2IType;
117
  L2IType::Pointer l2i = L2IType::New();
OTB Bot's avatar
STYLE  
OTB Bot committed
118
  l2i->SetInput(i2l->GetOutput());
119 120 121 122
  // Software Guide : EndCodeSnippet

  //  Software Guide : BeginLatex
  //
OTB Bot's avatar
STYLE  
OTB Bot committed
123
  //  The output can be passed to a writer. The invocation
124 125 126 127
  //  of the \code{Update()} method on the writer triggers the
  //  execution of the pipeline.
  //
  //  Software Guide : EndLatex
OTB Bot's avatar
STYLE  
OTB Bot committed
128

129
  // Software Guide : BeginCodeSnippet
OTB Bot's avatar
STYLE  
OTB Bot committed
130
  typedef itk::ImageFileWriter<ImageType> WriterType;
131
  WriterType::Pointer writer = WriterType::New();
OTB Bot's avatar
STYLE  
OTB Bot committed
132 133
  writer->SetInput(l2i->GetOutput());
  writer->SetFileName(argv[2]);
134
  writer->Update();
135
  // Software Guide : EndCodeSnippet
136

137
  // Pretty image creation for the printing
OTB Bot's avatar
STYLE  
OTB Bot committed
138 139 140 141 142 143 144
  typedef otb::Image<unsigned char,
                     dim>
                                                                  OutputPrettyImageType;
  typedef otb::ImageFileWriter<OutputPrettyImageType>
                                                                  WriterPrettyType;
  typedef itk::RescaleIntensityImageFilter<ImageType,
                                           OutputPrettyImageType> RescalerType;
145 146 147

  RescalerType::Pointer     rescaler     = RescalerType::New();
  WriterPrettyType::Pointer prettyWriter = WriterPrettyType::New();
OTB Bot's avatar
STYLE  
OTB Bot committed
148
  rescaler->SetInput(reader->GetOutput());
149 150
  rescaler->SetOutputMinimum(0);
  rescaler->SetOutputMaximum(255);
OTB Bot's avatar
STYLE  
OTB Bot committed
151 152
  prettyWriter->SetFileName(argv[3]);
  prettyWriter->SetInput(rescaler->GetOutput());
153 154
  prettyWriter->Update();

OTB Bot's avatar
STYLE  
OTB Bot committed
155 156 157
  rescaler->SetInput(l2i->GetOutput());
  prettyWriter->SetFileName(argv[4]);
  prettyWriter->SetInput(rescaler->GetOutput());
158 159
  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;
}