LidarToImageExample.cxx 8.08 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*=========================================================================

  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.

=========================================================================*/
#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif

Emmanuel Christophe's avatar
Emmanuel Christophe committed
22
23
24
//  Software Guide : BeginCommandLineArgs
//    INPUTS: {TO_core_last_zoom.las}
//    OUTPUTS: {lidar-image-4.hdr}, {lidar-image-4.png}
25
//    1.0 5 4
Emmanuel Christophe's avatar
Emmanuel Christophe committed
26
27
28
29
30
//  Software Guide : EndCommandLineArgs

//  Software Guide : BeginCommandLineArgs
//    INPUTS: {TO_core_last_zoom.las}
//    OUTPUTS: {lidar-image-8.hdr}, {lidar-image-8.png}
31
//    1.0 5 8
Emmanuel Christophe's avatar
Emmanuel Christophe committed
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//  Software Guide : EndCommandLineArgs    


// Software Guide : BeginLatex
//
// This example describes how to convert a point set obtained from some lidar data
// to an image file. Lidar produce a point set which is irregular in terms of spatial 
// sampling. To be able to generate an image, an interpolation is required.
//
// The interpolation is done using the
// \doxygen{itk}{BSplineScatteredDataPointSetToImageFilter} which uses BSplines. The 
// method is fully describes in \cite{Tustison2005} and \cite{Lee1997}.
//
// The first step toward the use of these filters is to include the proper header files.
//
// Software Guide : EndLatex
    
    
// Software Guide : BeginCodeSnippet

52
53
54
55
56
#include "itkPointSet.h"
#include "otbImage.h"
#include "otbPointSetFileReader.h"
#include "itkBSplineScatteredDataPointSetToImageFilter.h"
#include "otbImageFileWriter.h"
Emmanuel Christophe's avatar
Emmanuel Christophe committed
57
58
59
60
61

// Software Guide : EndCodeSnippet

#include "itkRescaleIntensityImageFilter.h"

62
63
64
65
int main( int argc, char* argv[] )
{


Emmanuel Christophe's avatar
Emmanuel Christophe committed
66
  if(argc!=7)
67
  {
Emmanuel Christophe's avatar
Emmanuel Christophe committed
68
69
    std::cout << argv[0] <<" <input_lidar_filename> <output_image_filename(double)>"
              << " <output_image_filename(unsigned char)>"
70
71
	      << " <output_resolution> <spline_order>"
              << " <number_of_level>" 
72
73
74
75
	      << std::endl;
    return EXIT_FAILURE;
  }
  
Emmanuel Christophe's avatar
Emmanuel Christophe committed
76
77
78
79
80
81
82
83
84
  // Software Guide : BeginLatex
  //
  // Then, we declare the type of the data that we are going to use. As
  // lidar decribes the altitude of the point (often to centimeter accuracy),
  // it is required to use real type to represent it.
  //
  // Software Guide : EndLatex
  
  // Software Guide : BeginCodeSnippet
85
86
87
  typedef double RealType;
  typedef itk::Vector<RealType, 1> VectorType;
  typedef itk::PointSet <VectorType, 2> PointSetType;
Emmanuel Christophe's avatar
Emmanuel Christophe committed
88
89
90
91
92
93
94
95
96
97
98
99
100
  typedef otb::Image<RealType, 2> ImageType;
  typedef itk::Image<VectorType, 2> VectorImageType;
  // Software Guide : EndCodeSnippet
  
  // Software Guide : BeginLatex
  //
  // Lidar data are read into a point set using the 
  // \doxygen{otb}{PointSetFileReader}. Its usage is very similar to
  // the \doxygen{otb}{ImagePointSetFileReader}:
  //
  // Software Guide : EndLatex
  
  // Software Guide : BeginCodeSnippet
101
102
103
104
105
  typedef otb::PointSetFileReader<PointSetType> PointSetFileReaderType;
  PointSetFileReaderType::Pointer reader = PointSetFileReaderType::New();

  reader->SetFileName(argv[1]);
  reader->Update();
Emmanuel Christophe's avatar
Emmanuel Christophe committed
106
  // Software Guide : EndCodeSnippet
107
  
Emmanuel Christophe's avatar
Emmanuel Christophe committed
108
109
110
111
112
113
114
  // Software Guide : BeginLatex
  //
  // We can now prepare the parameters to pass to the interpolation filter:
  // you have to be aware that the origin of the image is on the upper left
  // corner and thus correspond to the minimun easting but the maximum northing.
  //
  // Software Guide : EndLatex
115
  
Emmanuel Christophe's avatar
Emmanuel Christophe committed
116
117
118
119
120
  // Software Guide : BeginCodeSnippet
  double resolution = atof(argv[4]);
  int splineOrder = atoi(argv[5]);
  int level = atoi(argv[6]);
  // Software Guide : EndCodeSnippet
121
  
Emmanuel Christophe's avatar
Emmanuel Christophe committed
122
123
124
125
126
127
128
  std::cout << "*** Data area *** " << std::endl;
  std::cout << std::setprecision(15);
  std::cout << " - Easting min:  " << reader->GetMinX() << std::endl;
  std::cout << " - Easting max:  " << reader->GetMaxX() << std::endl;
  std::cout << " - Northing min: " << reader->GetMinY() << std::endl;
  std::cout << " - Northing max: " << reader->GetMaxY() << std::endl;
  std::cout << "Data points: " << reader->GetNumberOfPoints() << std::endl;
129
  
Emmanuel Christophe's avatar
Emmanuel Christophe committed
130
  // Software Guide : BeginCodeSnippet
131
132
133
134
135
136
  ImageType::IndexType start;
  start[0] =  0;
  start[1] =  0; 
  
  ImageType::SizeType  size;
  size[0]  = static_cast<long int >(
137
         ceil((reader->GetMaxX()-reader->GetMinX()+1)/resolution))+1; 
138
  size[1]  = static_cast<long int >(
139
         ceil((reader->GetMaxY()-reader->GetMinY()+1)/resolution))+1; 
140
141

  ImageType::PointType origin;
Emmanuel Christophe's avatar
Emmanuel Christophe committed
142
143
  origin[0] = reader->GetMinX();
  origin[1] = reader->GetMaxY();
144
145
146
147
  
  ImageType::SpacingType spacing;
  spacing[0] = resolution;
  spacing[1] = -resolution;
Emmanuel Christophe's avatar
Emmanuel Christophe committed
148
  // Software Guide : EndCodeSnippet
149
  
150
151
152
153
//   std::cout << "Size: " << size << std::endl;
//   std::cout << "Origin: " << origin << std::endl;
//   std::cout << "Spacing: " << spacing << std::endl;
  
Emmanuel Christophe's avatar
Emmanuel Christophe committed
154
155
156
157
158
159
160
  // Software Guide : BeginLatex
  //
  // All these parameters are passed to the interpolation filter:
  //
  // Software Guide : EndLatex
  
  // Software Guide : BeginCodeSnippet
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
   typedef itk::BSplineScatteredDataPointSetToImageFilter
      <PointSetType, VectorImageType> FilterType;

  FilterType::Pointer filter = FilterType::New();
  
  filter->SetSplineOrder( splineOrder );  
  FilterType::ArrayType ncps;  
  ncps.Fill( 6 );  
  filter->SetNumberOfControlPoints( ncps );
  filter->SetNumberOfLevels( level );

  filter->SetOrigin( origin );
  filter->SetSpacing( spacing );
  filter->SetSize( size );

  filter->SetInput( reader->GetOutput() );

  filter->Update();
Emmanuel Christophe's avatar
Emmanuel Christophe committed
179
180
181
182
183
184
185
186
187
188
189
  // Software Guide : EndCodeSnippet
  
  
  // Software Guide : BeginLatex
  //
  // The result of this filter is an image in which every pixel
  // is a vector (with only one element). For now, the otb writer
  // does not know how to process that (hopefully soon!). So we
  // have to manually copy the element in a standard image:
  //
  // Software Guide : EndLatex
190
  
Emmanuel Christophe's avatar
Emmanuel Christophe committed
191
  //TODO ImageOfVectorsToVectorImage needed !
192
    // Write the output to an image.
Emmanuel Christophe's avatar
Emmanuel Christophe committed
193
194
  
   // Software Guide : BeginCodeSnippet
195
196
197
198
199
200
201
202
203
204
205
206
207
208
  typedef otb::Image<RealType, 2> RealImageType;
  RealImageType::Pointer image = RealImageType::New();
  ImageType::RegionType region;
  region.SetSize( size );
  region.SetIndex( start );
  image->SetRegions( region );
  image->Allocate();
  itk::ImageRegionIteratorWithIndex<RealImageType> 
      Itt( image, image->GetLargestPossibleRegion() );
  
  for ( Itt.GoToBegin(); !Itt.IsAtEnd(); ++Itt )
  {
    Itt.Set( filter->GetOutput()->GetPixel( Itt.GetIndex() )[0] );
  }
Emmanuel Christophe's avatar
Emmanuel Christophe committed
209
210
  // Software Guide : EndCodeSnippet
  
211
  
Emmanuel Christophe's avatar
Emmanuel Christophe committed
212
213
214
215
216
217
218
  // Software Guide : BeginLatex
  //
  // Everything is ready so we can just write the image:
  //
  // Software Guide : EndLatex
  
  // Software Guide : BeginCodeSnippet
219
220
221
222
223
  typedef otb::ImageFileWriter<ImageType> WriterType;
  WriterType::Pointer writer = WriterType::New();
  writer->SetInput( image );
  writer->SetFileName( argv[2] );
  writer->Update();
Emmanuel Christophe's avatar
Emmanuel Christophe committed
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
  // Software Guide : EndCodeSnippet
  
  typedef otb::Image<unsigned char, 2> UCharImageType;
  typedef itk::RescaleIntensityImageFilter<ImageType,
               UCharImageType> RescalerType;
  RescalerType::Pointer rescaler = RescalerType::New();
  rescaler->SetInput(image);
  rescaler->SetOutputMaximum(255);
  rescaler->SetOutputMinimum(0);
  
  typedef otb::ImageFileWriter<UCharImageType> WriterUCharType;
  WriterUCharType::Pointer writerUChar = WriterUCharType::New();
  writerUChar->SetInput(rescaler->GetOutput());
  writerUChar->SetFileName(argv[3]);
  writerUChar->Update();
239

Emmanuel Christophe's avatar
Emmanuel Christophe committed
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
  
  
  
  
    // Software Guide : BeginLatex
  //
  // Figure~\ref{fig:LIDARTOIMAGEEXAMPLE} shows the output images with two sets of parameters
  //
  //
  // \begin{figure} 
  // \center
  // \includegraphics[width=0.40\textwidth]{lidar-image-4.png}
  // \includegraphics[width=0.40\textwidth]{lidar-image-8.png}
  // \itkcaption[Image from lidar data]{Image obtained with 4 level spline interpolation (left) and 8 levels (right)}
  // \label{fig:LIDARTOIMAGEEXAMPLE}
  // \end{figure}
  //
  // Software Guide : EndLatex
  
  
  
  
  
  
  
265
266
267
268
  return EXIT_SUCCESS;

}