38 #ifndef ORGANIZED_COMPRESSION_HPP
39 #define ORGANIZED_COMPRESSION_HPP
41 #include <pcl/compression/organized_pointcloud_compression.h>
44 #include <pcl/point_cloud.h>
46 #include <pcl/common/boost.h>
47 #include <pcl/common/eigen.h>
49 #include <pcl/common/io.h>
51 #include <pcl/compression/libpng_wrapper.h>
52 #include <pcl/compression/organized_pointcloud_conversion.h>
64 template<
typename Po
intT>
void
66 std::ostream& compressedDataOut_arg,
69 bool bShowStatistics_arg,
75 float maxDepth, focalLength, disparityShift, disparityScale;
78 disparityScale = 1.0f;
79 disparityShift = 0.0f;
81 analyzeOrganizedCloud (cloud_arg, maxDepth, focalLength);
84 compressedDataOut_arg.write (
reinterpret_cast<const char*
> (frameHeaderIdentifier_), strlen (frameHeaderIdentifier_));
86 compressedDataOut_arg.write (
reinterpret_cast<const char*
> (&cloud_width),
sizeof (cloud_width));
88 compressedDataOut_arg.write (
reinterpret_cast<const char*
> (&cloud_height),
sizeof (cloud_height));
90 compressedDataOut_arg.write (
reinterpret_cast<const char*
> (&maxDepth),
sizeof (maxDepth));
92 compressedDataOut_arg.write (
reinterpret_cast<const char*
> (&focalLength),
sizeof (focalLength));
94 compressedDataOut_arg.write (
reinterpret_cast<const char*
> (&disparityScale),
sizeof (disparityScale));
96 compressedDataOut_arg.write (
reinterpret_cast<const char*
> (&disparityShift),
sizeof (disparityShift));
99 std::vector<std::uint16_t> disparityData;
100 std::vector<std::uint8_t> colorData;
103 std::vector<std::uint8_t> compressedDisparity;
104 std::vector<std::uint8_t> compressedColor;
113 encodeMonoImageToPNG (disparityData, cloud_width, cloud_height, compressedDisparity, pngLevel_arg);
115 compressedDisparitySize =
static_cast<std::uint32_t>(compressedDisparity.size());
117 compressedDataOut_arg.write (
reinterpret_cast<const char*
> (&compressedDisparitySize),
sizeof (compressedDisparitySize));
119 compressedDataOut_arg.write (
reinterpret_cast<const char*
> (&compressedDisparity[0]), compressedDisparity.size () *
sizeof(
std::uint8_t));
133 compressedColorSize =
static_cast<std::uint32_t>(compressedColor.size ());
135 compressedDataOut_arg.write (
reinterpret_cast<const char*
> (&compressedColorSize),
sizeof (compressedColorSize));
137 compressedDataOut_arg.write (
reinterpret_cast<const char*
> (&compressedColor[0]), compressedColor.size () *
sizeof(
std::uint8_t));
139 if (bShowStatistics_arg)
142 float bytesPerPoint =
static_cast<float> (compressedDisparitySize+compressedColorSize) /
static_cast<float> (pointCount);
144 PCL_INFO(
"*** POINTCLOUD ENCODING ***\n");
145 PCL_INFO(
"Number of encoded points: %ld\n", pointCount);
147 PCL_INFO(
"Size of compressed point cloud: %.2f kBytes\n",
static_cast<float> (compressedDisparitySize+compressedColorSize) / 1024.0f);
148 PCL_INFO(
"Total bytes per point: %.4f bytes\n",
static_cast<float> (bytesPerPoint));
154 compressedDataOut_arg.flush();
158 template<
typename Po
intT>
void
160 std::vector<std::uint8_t>& colorImage_arg,
163 std::ostream& compressedDataOut_arg,
164 bool doColorEncoding,
166 bool bShowStatistics_arg,
168 float focalLength_arg,
169 float disparityShift_arg,
170 float disparityScale_arg)
174 std::size_t cloud_size = width_arg*height_arg;
175 assert (disparityMap_arg.size()==cloud_size);
176 if (!colorImage_arg.empty ())
178 assert (colorImage_arg.size()==cloud_size*3);
182 compressedDataOut_arg.write (
reinterpret_cast<const char*
> (frameHeaderIdentifier_), strlen (frameHeaderIdentifier_));
184 compressedDataOut_arg.write (
reinterpret_cast<const char*
> (&width_arg),
sizeof (width_arg));
186 compressedDataOut_arg.write (
reinterpret_cast<const char*
> (&height_arg),
sizeof (height_arg));
188 compressedDataOut_arg.write (
reinterpret_cast<const char*
> (&maxDepth),
sizeof (maxDepth));
190 compressedDataOut_arg.write (
reinterpret_cast<const char*
> (&focalLength_arg),
sizeof (focalLength_arg));
192 compressedDataOut_arg.write (
reinterpret_cast<const char*
> (&disparityScale_arg),
sizeof (disparityScale_arg));
194 compressedDataOut_arg.write (
reinterpret_cast<const char*
> (&disparityShift_arg),
sizeof (disparityShift_arg));
197 std::vector<std::uint8_t> compressedDisparity;
198 std::vector<std::uint8_t> compressedColor;
207 for (std::size_t i = 0; i < cloud_size; ++i, ++depth_ptr, color_ptr +=
sizeof(
std::uint8_t) * 3)
209 if (!(*depth_ptr) || (*depth_ptr==0x7FF))
214 encodeMonoImageToPNG (disparityMap_arg, width_arg, height_arg, compressedDisparity, pngLevel_arg);
216 compressedDisparitySize =
static_cast<std::uint32_t>(compressedDisparity.size());
218 compressedDataOut_arg.write (
reinterpret_cast<const char*
> (&compressedDisparitySize),
sizeof (compressedDisparitySize));
220 compressedDataOut_arg.write (
reinterpret_cast<const char*
> (&compressedDisparity[0]), compressedDisparity.size () *
sizeof(
std::uint8_t));
223 if (!colorImage_arg.empty () && doColorEncoding)
227 std::vector<std::uint8_t> monoImage;
228 std::size_t size = width_arg*height_arg;
230 monoImage.reserve(size);
233 for (std::size_t i = 0; i < size; ++i)
236 0.5870 *
static_cast<float>(colorImage_arg[i*3+1]) +
237 0.1140 *
static_cast<float>(colorImage_arg[i*3+2]));
238 monoImage.push_back(grayvalue);
249 compressedColorSize =
static_cast<std::uint32_t>(compressedColor.size ());
251 compressedDataOut_arg.write (
reinterpret_cast<const char*
> (&compressedColorSize),
sizeof (compressedColorSize));
253 compressedDataOut_arg.write (
reinterpret_cast<const char*
> (&compressedColor[0]), compressedColor.size () *
sizeof(
std::uint8_t));
255 if (bShowStatistics_arg)
258 float bytesPerPoint =
static_cast<float> (compressedDisparitySize+compressedColorSize) /
static_cast<float> (pointCount);
260 PCL_INFO(
"*** POINTCLOUD ENCODING ***\n");
261 PCL_INFO(
"Number of encoded points: %ld\n", pointCount);
262 PCL_INFO(
"Size of uncompressed disparity map+color image: %.2f kBytes\n", (
static_cast<float> (pointCount) * (
sizeof(
std::uint8_t)*3+
sizeof(
std::uint16_t))) / 1024.0f);
263 PCL_INFO(
"Size of compressed point cloud: %.2f kBytes\n",
static_cast<float> (compressedDisparitySize+compressedColorSize) / 1024.0f);
264 PCL_INFO(
"Total bytes per point: %.4f bytes\n",
static_cast<float> (bytesPerPoint));
265 PCL_INFO(
"Total compression percentage: %.4f%%\n", (bytesPerPoint) / (
sizeof(
std::uint8_t)*3+
sizeof(
std::uint16_t)) * 100.0f);
270 compressedDataOut_arg.flush();
274 template<
typename Po
intT>
bool
277 bool bShowStatistics_arg)
283 float disparityShift = 0.0f;
284 float disparityScale;
287 std::vector<std::uint16_t> disparityData;
288 std::vector<std::uint8_t> colorData;
291 std::vector<std::uint8_t> compressedDisparity;
292 std::vector<std::uint8_t> compressedColor;
298 std::size_t png_width = 0;
299 std::size_t png_height = 0;
300 unsigned int png_channels = 1;
303 unsigned int headerIdPos = 0;
304 bool valid_stream =
true;
305 while (valid_stream && (headerIdPos < strlen (frameHeaderIdentifier_)))
308 compressedDataIn_arg.read (
static_cast<char*
> (&readChar),
sizeof (readChar));
309 if (compressedDataIn_arg.gcount()!= sizeof (readChar))
310 valid_stream =
false;
311 if (readChar != frameHeaderIdentifier_[headerIdPos++])
312 headerIdPos = (frameHeaderIdentifier_[0] == readChar) ? 1 : 0;
314 valid_stream &= compressedDataIn_arg.good ();
321 compressedDataIn_arg.read (
reinterpret_cast<char*
> (&cloud_width),
sizeof (cloud_width));
322 compressedDataIn_arg.read (
reinterpret_cast<char*
> (&cloud_height),
sizeof (cloud_height));
323 compressedDataIn_arg.read (
reinterpret_cast<char*
> (&maxDepth),
sizeof (maxDepth));
324 compressedDataIn_arg.read (
reinterpret_cast<char*
> (&focalLength),
sizeof (focalLength));
325 compressedDataIn_arg.read (
reinterpret_cast<char*
> (&disparityScale),
sizeof (disparityScale));
326 compressedDataIn_arg.read (
reinterpret_cast<char*
> (&disparityShift),
sizeof (disparityShift));
329 compressedDataIn_arg.read (
reinterpret_cast<char*
> (&compressedDisparitySize),
sizeof (compressedDisparitySize));
330 compressedDisparity.resize (compressedDisparitySize);
331 compressedDataIn_arg.read (
reinterpret_cast<char*
> (&compressedDisparity[0]), compressedDisparitySize *
sizeof(
std::uint8_t));
334 compressedDataIn_arg.read (
reinterpret_cast<char*
> (&compressedColorSize),
sizeof (compressedColorSize));
335 compressedColor.resize (compressedColorSize);
336 compressedDataIn_arg.read (
reinterpret_cast<char*
> (&compressedColor[0]), compressedColorSize *
sizeof(
std::uint8_t));
339 decodePNGToImage (compressedDisparity, disparityData, png_width, png_height, png_channels);
342 decodePNGToImage (compressedColor, colorData, png_width, png_height, png_channels);
345 if (disparityShift==0.0f)
361 std::size_t size = disparityData.size();
362 std::vector<float> depthData;
363 depthData.resize(size);
366 if (!sd_converter_.isInitialized())
367 sd_converter_.generateLookupTable();
370 for (std::size_t i=0; i<size; ++i)
371 depthData[i] = sd_converter_.shiftToDepth(disparityData[i]);
376 static_cast<bool>(png_channels==1),
383 if (bShowStatistics_arg)
386 float bytesPerPoint =
static_cast<float> (compressedDisparitySize+compressedColorSize) /
static_cast<float> (pointCount);
388 PCL_INFO(
"*** POINTCLOUD DECODING ***\n");
389 PCL_INFO(
"Number of encoded points: %ld\n", pointCount);
391 PCL_INFO(
"Size of compressed point cloud: %.2f kBytes\n",
static_cast<float> (compressedDisparitySize+compressedColorSize) / 1024.0f);
392 PCL_INFO(
"Total bytes per point: %.4f bytes\n",
static_cast<float> (bytesPerPoint));
401 template<
typename Po
intT>
void
404 float& focalLength_arg)
const
406 std::size_t width = cloud_arg->width;
407 std::size_t height = cloud_arg->height;
410 int centerX =
static_cast<int> (width / 2);
411 int centerY =
static_cast<int> (height / 2);
414 assert((width>1) && (height>1));
415 assert(width*height == cloud_arg->points.size());
418 float focalLength = 0;
421 for (
int y = -centerY; y < centerY; ++y )
422 for (
int x = -centerX; x < centerX; ++x )
424 const PointT& point = cloud_arg->points[it++];
428 if (maxDepth < point.z)
434 focalLength = 2.0f / (point.x / (
static_cast<float> (x) * point.z) + point.y / (
static_cast<float> (y) * point.z));
440 maxDepth_arg = maxDepth;
441 focalLength_arg = focalLength;
typename PointCloud::Ptr PointCloudPtr
void encodePointCloud(const PointCloudConstPtr &cloud_arg, std::ostream &compressedDataOut_arg, bool doColorEncoding=false, bool convertToMono=false, bool bShowStatistics_arg=true, int pngLevel_arg=-1)
Encode point cloud to output stream.
bool decodePointCloud(std::istream &compressedDataIn_arg, PointCloudPtr &cloud_arg, bool bShowStatistics_arg=true)
Decode point cloud from input stream.
void analyzeOrganizedCloud(PointCloudConstPtr cloud_arg, float &maxDepth_arg, float &focalLength_arg) const
Analyze input point cloud and calculate the maximum depth and focal length.
typename PointCloud::ConstPtr PointCloudConstPtr
void encodeRawDisparityMapWithColorImage(std::vector< std::uint16_t > &disparityMap_arg, std::vector< std::uint8_t > &colorImage_arg, std::uint32_t width_arg, std::uint32_t height_arg, std::ostream &compressedDataOut_arg, bool doColorEncoding=false, bool convertToMono=false, bool bShowStatistics_arg=true, int pngLevel_arg=-1, float focalLength_arg=525.0f, float disparityShift_arg=174.825f, float disparityScale_arg=-0.161175f)
Encode raw disparity map and color image.
Define standard C methods and C++ classes that are common to all methods.
PCL_EXPORTS void decodePNGToImage(std::vector< std::uint8_t > &pngData_arg, std::vector< std::uint8_t > &imageData_arg, std::size_t &width_arg, std::size_t &heigh_argt, unsigned int &channels_arg)
Decode compressed PNG to 8-bit image.
PCL_EXPORTS void encodeRGBImageToPNG(std::vector< std::uint8_t > &image_arg, std::size_t width_arg, std::size_t height_arg, std::vector< std::uint8_t > &pngData_arg, int png_level_arg=-1)
Encodes 8-bit RGB image to PNG format.
PCL_EXPORTS void encodeMonoImageToPNG(std::vector< std::uint8_t > &image_arg, std::size_t width_arg, std::size_t height_arg, std::vector< std::uint8_t > &pngData_arg, int png_level_arg=-1)
Encodes 8-bit mono image to PNG format.
bool isFinite(const PointT &pt)
Tests if the 3D components of a point are all finite param[in] pt point to be tested return true if f...
Defines all the PCL and non-PCL macros used.
A point structure representing Euclidean xyz coordinates, and the RGB color.