forked from PointCloudLibrary/pcl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pcd2png.cpp
311 lines (288 loc) · 12.1 KB
/
pcd2png.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
* Software License Agreement (BSD License)
*
* Point Cloud Library (PCL) - www.pointclouds.org
* Copyright (c) 2013-, Open Perception, Inc.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of the copyright holder(s) nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
/** \brief PCD 2 PNG converter
*
* This converter takes three inputs: names of the input PCD and output PNG files, and the name of the field.
*
* \author Sergey Alexandrov
*
*/
#include <pcl/console/time.h>
#include <pcl/console/print.h>
#include <pcl/console/parse.h>
#include <pcl/io/pcd_io.h>
#include <pcl/io/png_io.h>
#include <pcl/conversions.h>
#include <boost/lexical_cast.hpp> // for lexical_cast
using namespace pcl;
using namespace pcl::io;
using namespace pcl::console;
void
printHelp (int, char **argv)
{
std::cout << std::endl;
std::cout << "****************************************************************************" << std::endl;
std::cout << "* *" << std::endl;
std::cout << "* PCD 2 PNG CONVERTER - Usage Guide *" << std::endl;
std::cout << "* *" << std::endl;
std::cout << "****************************************************************************" << std::endl;
std::cout << std::endl;
std::cout << "Usage: " << argv[0] << " [Options] input.pcd output.png" << std::endl;
std::cout << std::endl;
std::cout << "Options:" << std::endl;
std::cout << std::endl;
std::cout << " --help : Show this help" << std::endl;
std::cout << " --no-nan : Paint NaN (infinite) points with black color regardless of" << std::endl;
std::cout << " field contents" << std::endl;
std::cout << " --field : Set the field to extract data from. Supported fields:" << std::endl;
std::cout << " - normal" << std::endl;
std::cout << " * rgb (default)" << std::endl;
std::cout << " - label" << std::endl;
std::cout << " - z" << std::endl;
std::cout << " - curvature" << std::endl;
std::cout << " - intensity" << std::endl;
std::cout << " --scale : Apply scaling to extracted data (only for z, curvature, and" << std::endl;
std::cout << " intensity fields). Supported options:" << std::endl;
std::cout << " - <float> : Scale by a fixed number" << std::endl;
std::cout << " - auto : Auto-scale to the full range" << std::endl;
std::cout << " - no : No scaling" << std::endl;
std::cout << " If the option is omitted then default scaling (depends on" << std::endl;
std::cout << " the field type) will be used." << std::endl;
std::cout << " --colors : Choose color mapping mode for labels (only for label field)." << std::endl;
std::cout << " Supported options:" << std::endl;
std::cout << " - mono : Shades of gray" << std::endl;
std::cout << " - rgb : Randomly generated RGB colors" << std::endl;
std::cout << " * glasbey : Fixed colors from the Glasbey table¹ (default)" << std::endl;
std::cout << std::endl;
std::cout << "Notes:" << std::endl;
std::cout << std::endl;
std::cout << "¹) The Glasbey lookup table is a color table structured in a maximally" << std::endl;
std::cout << " discontinuous manner. Adjacent color bins are chosen to be as distinct" << std::endl;
std::cout << " from one another as possible (see https://github.com/taketwo/glasbey)." << std::endl;
std::cout << " The label with the smallest id will be assigned the first color from the" << std::endl;
std::cout << " table, the second smallest will have the second color, and so on. Thus," << std::endl;
std::cout << " if you have several clouds with the same labels, you will get repetitive" << std::endl;
std::cout << " consistently colored PNG images." << std::endl;
}
bool
loadCloud (const std::string &filename, pcl::PCLPointCloud2 &cloud)
{
TicToc tt;
print_highlight ("Loading "); print_value ("%s ", filename.c_str ());
tt.tic ();
if (loadPCDFile (filename, cloud) < 0)
return (false);
print_info ("[done, "); print_value ("%g", tt.toc ()); print_info (" ms : "); print_value ("%d", cloud.width * cloud.height); print_info (" points]\n");
print_info ("Available dimensions: "); print_value ("%s\n", pcl::getFieldsList (cloud).c_str ());
return (true);
}
void
saveImage (const std::string &filename, const pcl::PCLImage& image)
{
TicToc tt;
tt.tic ();
print_highlight ("Saving "); print_value ("%s ", filename.c_str ());
savePNGFile (filename, image);
print_info ("[done, "); print_value ("%g", tt.toc ()); print_info (" ms : "); print_value ("%d", image.width * image.height); print_info (" points]\n");
}
template<typename T> bool
parseScaleOption (int argc, char** argv, T& pcie)
{
std::string scaling = "default";
pcl::console::parse_argument (argc, argv, "--scale", scaling);
print_info ("Scaling: "); print_value ("%s\n", scaling.c_str());
if (scaling == "default")
{
// scaling option omitted, use whatever defaults image extractor has
}
else if (scaling == "no")
{
pcie.setScalingMethod(pcie.SCALING_NO);
}
else if (scaling == "auto")
{
pcie.setScalingMethod(pcie.SCALING_FULL_RANGE);
}
else
{
try
{
float factor = boost::lexical_cast<float> (scaling);
pcie.setScalingMethod(pcie.SCALING_FIXED_FACTOR);
pcie.setScalingFactor(factor);
}
catch (const boost::bad_lexical_cast&)
{
print_error ("The value of --scale option should be \"no\", \"auto\", or a floating point number.\n");
return false;
}
}
return true;
}
template<typename T> bool
parseColorsOption (int argc, char** argv, T& pcie)
{
std::string colors = "glasbey";
pcl::console::parse_argument (argc, argv, "--colors", colors);
print_info ("Colors: "); print_value ("%s\n", colors.c_str());
if (colors == "mono")
{
pcie.setColorMode(pcie.COLORS_MONO);
}
else if (colors == "rgb")
{
pcie.setColorMode(pcie.COLORS_RGB_RANDOM);
}
else if (colors == "glasbey")
{
pcie.setColorMode(pcie.COLORS_RGB_GLASBEY);
}
else
{
return false;
}
return true;
}
/* ---[ */
int
main (int argc, char** argv)
{
print_info ("Convert a PCD file to PNG format.\nFor more information, use: %s --help\n", argv[0]);
if (argc < 3 || pcl::console::find_switch (argc, argv, "--help"))
{
printHelp (argc, argv);
return (-1);
}
// Parse the command line arguments for .pcd and .png files
std::vector<int> pcd_file_index = parse_file_extension_argument (argc, argv, ".pcd");
std::vector<int> png_file_index = parse_file_extension_argument (argc, argv, ".png");
if (pcd_file_index.size () != 1 || png_file_index.size () != 1)
{
print_error ("Need one input PCD file and one output PNG file.\n");
return (-1);
}
std::string pcd_filename = argv[pcd_file_index[0]];
std::string png_filename = argv[png_file_index[0]];
// Load the input file
pcl::PCLPointCloud2::Ptr blob (new pcl::PCLPointCloud2);
if (!loadCloud (pcd_filename, *blob))
{
print_error ("Unable to load PCD file.\n");
return (-1);
}
// Check if the cloud is organized
if (blob->height == 1)
{
print_error ("Input cloud is not organized.\n");
return (-1);
}
bool paint_nans_with_black = pcl::console::find_switch (argc, argv, "--no-nan");
print_info ("Paint infinite points with black: "); print_value ("%s\n", paint_nans_with_black ? "YES" : "NO");
std::string field_name = "rgb";
parse_argument (argc, argv, "--field", field_name);
print_info ("Field name: "); print_value ("%s\n", field_name.c_str());
pcl::PCLImage image;
bool extracted;
if (field_name == "normal")
{
PointCloud<PointNormal> cloud;
fromPCLPointCloud2 (*blob, cloud);
PointCloudImageExtractorFromNormalField<PointNormal> pcie;
pcie.setPaintNaNsWithBlack (paint_nans_with_black);
extracted = pcie.extract(cloud, image);
}
else if (field_name == "rgb")
{
PointCloud<PointXYZRGB> cloud;
fromPCLPointCloud2 (*blob, cloud);
PointCloudImageExtractorFromRGBField<PointXYZRGB> pcie;
pcie.setPaintNaNsWithBlack (paint_nans_with_black);
extracted = pcie.extract(cloud, image);
}
else if (field_name == "label")
{
PointCloud<PointXYZL> cloud;
fromPCLPointCloud2 (*blob, cloud);
PointCloudImageExtractorFromLabelField<PointXYZL> pcie;
pcie.setPaintNaNsWithBlack (paint_nans_with_black);
if (!parseColorsOption(argc, argv, pcie))
return (-1);
extracted = pcie.extract(cloud, image);
}
else if (field_name == "z")
{
PointCloud<PointXYZ> cloud;
fromPCLPointCloud2 (*blob, cloud);
PointCloudImageExtractorFromZField<PointXYZ> pcie;
pcie.setPaintNaNsWithBlack (paint_nans_with_black);
if (!parseScaleOption(argc, argv, pcie))
return (-1);
extracted = pcie.extract(cloud, image);
}
else if (field_name == "curvature")
{
PointCloud<PointNormal> cloud;
fromPCLPointCloud2 (*blob, cloud);
PointCloudImageExtractorFromCurvatureField<PointNormal> pcie;
pcie.setPaintNaNsWithBlack (paint_nans_with_black);
if (!parseScaleOption(argc, argv, pcie))
return (-1);
extracted = pcie.extract(cloud, image);
}
else if (field_name == "intensity")
{
PointCloud<PointXYZI> cloud;
fromPCLPointCloud2 (*blob, cloud);
PointCloudImageExtractorFromIntensityField<PointXYZI> pcie;
pcie.setPaintNaNsWithBlack (paint_nans_with_black);
if (!parseScaleOption(argc, argv, pcie))
return (-1);
extracted = pcie.extract(cloud, image);
}
else
{
print_error ("Unsupported field \"%s\".\n", field_name.c_str());
return (-1);
}
if (!extracted)
{
print_error ("Failed to extract an image from field \"%s\".\n", field_name.c_str());
return (-1);
}
saveImage (png_filename, image);
return (0);
}