ModelToImage.cpp
Go to the documentation of this file.
1 
28 /*
29  * ModelToImage.cpp
30  *
31  * Created on: Jan 25, 2017
32  * Author: Thomas Wiemann (twiemann@uos.de)
33  */
34 
37 #include "lvr2/io/Progress.hpp"
38 #include "lvr2/io/Timestamp.hpp"
40 
41 #include <iostream>
42 #include <fstream>
43 #include <algorithm>
44 #include <list>
45 using namespace std;
46 
47 namespace lvr2
48 {
49 
50 using Vec = BaseVector<float>;
51 
52 ModelToImage::ModelToImage(
53  PointBufferPtr buffer,
55  int width, int height,
56  float minZ, float maxZ,
57  int minHorizontenAngle, int maxHorizontalAngle,
58  int minVerticalAngle, int maxVerticalAngle,
59  bool imageOptimization,
60  CoordinateSystem system)
61 {
62 
63  // Initialize global members
64  m_width = width;
65  m_height = height;
66  m_coordinateSystem = system;
67  m_minZ = minZ;
68  m_maxZ = maxZ;
69  m_minHAngle = minHorizontenAngle;
70  m_maxHAngle = maxHorizontalAngle;
71  m_minVAngle = minVerticalAngle;
72  m_maxVAngle = maxVerticalAngle;
73  m_points = buffer;
74 
75  // Create the projection representation
76  m_projection = new EquirectangularProjection(
77  m_width, m_height,
78  minHorizontenAngle, maxHorizontalAngle,
79  minVerticalAngle, maxVerticalAngle,
80  imageOptimization, system);
81 
82 }
83 
84 
85 
86 ModelToImage::~ModelToImage()
87 {
88  // TODO Auto-generated destructor stub
89 }
90 
91 void ModelToImage::computeDepthListMatrix(DepthListMatrix& mat)
92 {
93  cout << timestamp << "Initializting DepthListMatrix with dimensions " << m_width << " x " << m_height << endl;
94  // Set correct image width and height
95  for(int i = 0; i < m_height; i++)
96  {
97  mat.pixels.emplace_back(vector<vector<PanoramaPoint> >());
98  for(int j = 0; j < m_width; j++)
99  {
100  mat.pixels[i].push_back(vector<PanoramaPoint>());
101  }
102  }
103 
104  // Get point array and size from buffer
105  size_t n_points = m_points->numPoints();
106  floatArr points = m_points->getPointArray();
107 
108  // Create progress output
109  string comment = timestamp.getElapsedTime() + "Projecting points ";
110  ProgressBar progress(n_points, comment);
111 
112  float range;
113  int img_x, img_y;
114  for(int i = 0; i < n_points; i++)
115  {
116  Vec ppt(points[3 * i], points[3 * i + 1], points[3 * i + 2]);
117 
118  m_projection->project(
119  img_x, img_y, range,
120  ppt.x, ppt.y, ppt.z);
121 
122  // Update min and max ranges
123  if(range > mat.maxRange)
124  {
125  mat.maxRange = range;
126  }
127 
128  if(range < mat.minRange)
129  {
130  mat.minRange = range;
131  }
132 
133  if(range < m_maxZ)
134  {
135  // Add point index to image pixel
136  mat.pixels[img_y][img_x].emplace_back(PanoramaPoint(i));
137  }
138  ++progress;
139  }
140  cout << endl;
141 }
142 
143 void ModelToImage::computeDepthImage(ModelToImage::DepthImage& img, ModelToImage::ProjectionPolicy policy)
144 {
145  cout << timestamp << "Computing depth image. Image dimensions: " << m_width << " x " << m_height << endl;
146 
147  // Set correct image width and height
148  for(int i = 0; i < m_height; i++)
149  {
150  img.pixels.emplace_back(vector<float>());
151  for(int j = 0; j < m_width; j++)
152  {
153  img.pixels[i].push_back(0.0f);
154  }
155  }
156 
157 
158  // Get point array and size from buffer
159  size_t n_points = m_points->numPoints();
160  floatArr points = m_points->getPointArray();
161 
162  // Create progress output
163  string comment = timestamp.getElapsedTime() + "Projecting points ";
164  ProgressBar progress(n_points, comment);
165 
166  float range;
167  int img_x, img_y;
168  for(int i = 0; i < n_points; i++)
169  {
170  m_projection->project(
171  img_x, img_y, range,
172  points[3 * i], points[3 * i + 1], points[3 * i + 2]);
173 
174  // Update min and max ranges
175  if(range > img.maxRange)
176  {
177  img.maxRange = range;
178  }
179 
180  if(range < img.minRange)
181  {
182  img.minRange = range;
183  }
184 
185  img.pixels[img_y][img_x] = range;
186  ++progress;
187  }
188  cout << endl;
189  cout << timestamp << "Min / Max range: " << img.minRange << " / " << img.maxRange << endl;
190 }
191 
192 void ModelToImage::writePGM(string filename, float cutoff)
193 {
194  // Compute panorama image
196  computeDepthImage(img);
197 
198  // Clamp range values
199  float min_r = std::min(m_minZ, img.minRange);
200  float max_r = std::min(m_maxZ, img.maxRange);
201  float interval = max_r - min_r;
202 
203  cout << min_r << " " << max_r << " " << interval << endl;
204 
205  // Open file, write header and pixel values
206  std::ofstream out(filename);
207  out << "P2" << endl;
208  out << img.pixels[0].size() << " " << img.pixels.size() << " 255" << endl;
209 
210  for(int i = 0; i < img.pixels.size(); i++)
211  {
212  for(int j = 0; j < img.pixels[i].size(); j++)
213  {
214  int val = img.pixels[i][j];
215 
216  // Image was initialized with zeros. Fix that to
217  // the measured min value
218  if(val < min_r)
219  {
220  val = min_r;
221  }
222 
223  val = (int)((float)(val - min_r) / interval * 255);
224  out << val << " ";
225  }
226  }
227 }
228 
229 } /* namespace lvr */
ProjectionType
The ProjectionType enum.
Image with list of projected points at each pixel.
static Timestamp timestamp
A global time stamp object for program runtime measurement.
Definition: Timestamp.hpp:116
std::shared_ptr< PointBuffer > PointBufferPtr
vector< vector< float > > pixels
boost::shared_array< float > floatArr
Definition: DataStruct.hpp:133
vector< vector< vector< PanoramaPoint > > > pixels
string getElapsedTime() const
Returns a string representation of the current timer value.
Definition: Timestamp.cpp:136
BaseVector< float > Vec
Image with single depth information.


lvr2
Author(s): Thomas Wiemann , Sebastian Pütz , Alexander Mock , Lars Kiesow , Lukas Kalbertodt , Tristan Igelbrink , Johan M. von Behren , Dominik Feldschnieders , Alexander Löhr
autogenerated on Mon Feb 28 2022 22:46:08