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/compression/libpng_wrapper.h>
47 #include <pcl/compression/organized_pointcloud_conversion.h>
57 template<
typename Po
intT>
void
59 std::ostream& compressedDataOut_arg,
62 bool bShowStatistics_arg,
65 std::uint32_t cloud_width = cloud_arg->width;
66 std::uint32_t cloud_height = cloud_arg->height;
68 float maxDepth, focalLength, disparityShift, disparityScale;
71 disparityScale = 1.0f;
72 disparityShift = 0.0f;
74 analyzeOrganizedCloud (cloud_arg, maxDepth, focalLength);
77 compressedDataOut_arg.write (
reinterpret_cast<const char*
> (frameHeaderIdentifier_), strlen (frameHeaderIdentifier_));
79 compressedDataOut_arg.write (
reinterpret_cast<const char*
> (&cloud_width),
sizeof (cloud_width));
81 compressedDataOut_arg.write (
reinterpret_cast<const char*
> (&cloud_height),
sizeof (cloud_height));
83 compressedDataOut_arg.write (
reinterpret_cast<const char*
> (&maxDepth),
sizeof (maxDepth));
85 compressedDataOut_arg.write (
reinterpret_cast<const char*
> (&focalLength),
sizeof (focalLength));
87 compressedDataOut_arg.write (
reinterpret_cast<const char*
> (&disparityScale),
sizeof (disparityScale));
89 compressedDataOut_arg.write (
reinterpret_cast<const char*
> (&disparityShift),
sizeof (disparityShift));
92 std::vector<std::uint16_t> disparityData;
93 std::vector<std::uint8_t> colorData;
96 std::vector<std::uint8_t> compressedDisparity;
97 std::vector<std::uint8_t> compressedColor;
99 std::uint32_t compressedDisparitySize = 0;
100 std::uint32_t compressedColorSize = 0;
106 encodeMonoImageToPNG (disparityData, cloud_width, cloud_height, compressedDisparity, pngLevel_arg);
108 compressedDisparitySize =
static_cast<std::uint32_t
>(compressedDisparity.size());
110 compressedDataOut_arg.write (
reinterpret_cast<const char*
> (&compressedDisparitySize),
sizeof (compressedDisparitySize));
112 compressedDataOut_arg.write (
reinterpret_cast<const char*
> (compressedDisparity.data()), compressedDisparity.size () *
sizeof(std::uint8_t));
126 compressedColorSize =
static_cast<std::uint32_t
>(compressedColor.size ());
128 compressedDataOut_arg.write (
reinterpret_cast<const char*
> (&compressedColorSize),
sizeof (compressedColorSize));
130 compressedDataOut_arg.write (
reinterpret_cast<const char*
> (compressedColor.data()), compressedColor.size () *
sizeof(std::uint8_t));
132 if (bShowStatistics_arg)
134 std::uint64_t pointCount = cloud_width * cloud_height;
135 float bytesPerPoint =
static_cast<float> (compressedDisparitySize+compressedColorSize) /
static_cast<float> (pointCount);
137 PCL_INFO(
"*** POINTCLOUD ENCODING ***\n");
138 PCL_INFO(
"Number of encoded points: %ld\n", pointCount);
140 PCL_INFO(
"Size of compressed point cloud: %.2f kBytes\n",
static_cast<float> (compressedDisparitySize+compressedColorSize) / 1024.0f);
141 PCL_INFO(
"Total bytes per point: %.4f bytes\n",
static_cast<float> (bytesPerPoint));
147 compressedDataOut_arg.flush();
151 template<
typename Po
intT>
void
153 std::vector<std::uint8_t>& colorImage_arg,
154 std::uint32_t width_arg,
155 std::uint32_t height_arg,
156 std::ostream& compressedDataOut_arg,
157 bool doColorEncoding,
159 bool bShowStatistics_arg,
161 float focalLength_arg,
162 float disparityShift_arg,
163 float disparityScale_arg)
167 std::size_t cloud_size = width_arg*height_arg;
168 assert (disparityMap_arg.size()==cloud_size);
169 if (!colorImage_arg.empty ())
171 assert (colorImage_arg.size()==cloud_size*3);
175 compressedDataOut_arg.write (
reinterpret_cast<const char*
> (frameHeaderIdentifier_), strlen (frameHeaderIdentifier_));
177 compressedDataOut_arg.write (
reinterpret_cast<const char*
> (&width_arg),
sizeof (width_arg));
179 compressedDataOut_arg.write (
reinterpret_cast<const char*
> (&height_arg),
sizeof (height_arg));
181 compressedDataOut_arg.write (
reinterpret_cast<const char*
> (&maxDepth),
sizeof (maxDepth));
183 compressedDataOut_arg.write (
reinterpret_cast<const char*
> (&focalLength_arg),
sizeof (focalLength_arg));
185 compressedDataOut_arg.write (
reinterpret_cast<const char*
> (&disparityScale_arg),
sizeof (disparityScale_arg));
187 compressedDataOut_arg.write (
reinterpret_cast<const char*
> (&disparityShift_arg),
sizeof (disparityShift_arg));
190 std::vector<std::uint8_t> compressedDisparity;
191 std::vector<std::uint8_t> compressedColor;
193 std::uint32_t compressedDisparitySize = 0;
194 std::uint32_t compressedColorSize = 0;
197 std::uint16_t* depth_ptr = disparityMap_arg.data();
198 std::uint8_t* color_ptr = colorImage_arg.data();
200 for (std::size_t i = 0; i < cloud_size; ++i, ++depth_ptr, color_ptr +=
sizeof(std::uint8_t) * 3)
202 if (!(*depth_ptr) || (*depth_ptr==0x7FF)) {
203 std::fill_n(color_ptr, 3, 0);
208 encodeMonoImageToPNG (disparityMap_arg, width_arg, height_arg, compressedDisparity, pngLevel_arg);
210 compressedDisparitySize =
static_cast<std::uint32_t
>(compressedDisparity.size());
212 compressedDataOut_arg.write (
reinterpret_cast<const char*
> (&compressedDisparitySize),
sizeof (compressedDisparitySize));
214 compressedDataOut_arg.write (
reinterpret_cast<const char*
> (compressedDisparity.data()), compressedDisparity.size () *
sizeof(std::uint8_t));
217 if (!colorImage_arg.empty () && doColorEncoding)
221 std::vector<std::uint8_t> monoImage;
222 std::size_t size = width_arg*height_arg;
224 monoImage.reserve(size);
227 for (std::size_t i = 0; i < size; ++i)
229 auto grayvalue =
static_cast<std::uint8_t
>(0.2989 *
static_cast<float>(colorImage_arg[i*3+0]) +
230 0.5870 *
static_cast<float>(colorImage_arg[i*3+1]) +
231 0.1140 *
static_cast<float>(colorImage_arg[i*3+2]));
232 monoImage.push_back(grayvalue);
243 compressedColorSize =
static_cast<std::uint32_t
>(compressedColor.size ());
245 compressedDataOut_arg.write (
reinterpret_cast<const char*
> (&compressedColorSize),
sizeof (compressedColorSize));
247 compressedDataOut_arg.write (
reinterpret_cast<const char*
> (compressedColor.data()), compressedColor.size () *
sizeof(std::uint8_t));
249 if (bShowStatistics_arg)
251 std::uint64_t pointCount = width_arg * height_arg;
252 float bytesPerPoint =
static_cast<float> (compressedDisparitySize+compressedColorSize) /
static_cast<float> (pointCount);
254 PCL_INFO(
"*** POINTCLOUD ENCODING ***\n");
255 PCL_INFO(
"Number of encoded points: %ld\n", pointCount);
256 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);
257 PCL_INFO(
"Size of compressed point cloud: %.2f kBytes\n",
static_cast<float> (compressedDisparitySize+compressedColorSize) / 1024.0f);
258 PCL_INFO(
"Total bytes per point: %.4f bytes\n",
static_cast<float> (bytesPerPoint));
259 PCL_INFO(
"Total compression percentage: %.4f%%\n", (bytesPerPoint) / (
sizeof(std::uint8_t)*3+
sizeof(std::uint16_t)) * 100.0f);
264 compressedDataOut_arg.flush();
268 template<
typename Po
intT>
bool
271 bool bShowStatistics_arg)
273 std::uint32_t cloud_width;
274 std::uint32_t cloud_height;
277 float disparityShift = 0.0f;
278 float disparityScale;
281 std::vector<std::uint16_t> disparityData;
282 std::vector<std::uint8_t> colorData;
285 std::vector<std::uint8_t> compressedDisparity;
286 std::vector<std::uint8_t> compressedColor;
288 std::uint32_t compressedDisparitySize;
289 std::uint32_t compressedColorSize;
292 unsigned int png_channels = 1;
295 unsigned int headerIdPos = 0;
296 bool valid_stream =
true;
297 while (valid_stream && (headerIdPos < strlen (frameHeaderIdentifier_)))
300 compressedDataIn_arg.read (
static_cast<char*
> (&readChar),
sizeof (readChar));
301 if (compressedDataIn_arg.gcount()!= sizeof (readChar))
302 valid_stream =
false;
303 if (readChar != frameHeaderIdentifier_[headerIdPos++])
304 headerIdPos = (frameHeaderIdentifier_[0] == readChar) ? 1 : 0;
306 valid_stream &= compressedDataIn_arg.good ();
313 compressedDataIn_arg.read (
reinterpret_cast<char*
> (&cloud_width),
sizeof (cloud_width));
314 compressedDataIn_arg.read (
reinterpret_cast<char*
> (&cloud_height),
sizeof (cloud_height));
315 compressedDataIn_arg.read (
reinterpret_cast<char*
> (&maxDepth),
sizeof (maxDepth));
316 compressedDataIn_arg.read (
reinterpret_cast<char*
> (&focalLength),
sizeof (focalLength));
317 compressedDataIn_arg.read (
reinterpret_cast<char*
> (&disparityScale),
sizeof (disparityScale));
318 compressedDataIn_arg.read (
reinterpret_cast<char*
> (&disparityShift),
sizeof (disparityShift));
321 compressedDataIn_arg.read (
reinterpret_cast<char*
> (&compressedDisparitySize),
sizeof (compressedDisparitySize));
322 compressedDisparity.resize (compressedDisparitySize);
323 compressedDataIn_arg.read (
reinterpret_cast<char*
> (compressedDisparity.data()), compressedDisparitySize *
sizeof(std::uint8_t));
326 compressedDataIn_arg.read (
reinterpret_cast<char*
> (&compressedColorSize),
sizeof (compressedColorSize));
327 compressedColor.resize (compressedColorSize);
328 compressedDataIn_arg.read (
reinterpret_cast<char*
> (compressedColor.data()), compressedColorSize *
sizeof(std::uint8_t));
330 std::size_t png_width = 0;
331 std::size_t png_height = 0;
334 decodePNGToImage (compressedDisparity, disparityData, png_width, png_height, png_channels);
337 decodePNGToImage (compressedColor, colorData, png_width, png_height, png_channels);
339 PCL_ERROR(
"[OrganizedPointCloudCompression::decodePointCloud] Unable to find an encoded point cloud in the input stream!\n");
343 if (disparityShift==0.0f)
359 std::size_t size = disparityData.size();
360 std::vector<float> depthData;
361 depthData.resize(size);
364 if (!sd_converter_.isInitialized())
365 sd_converter_.generateLookupTable();
368 for (std::size_t i=0; i<size; ++i)
369 depthData[i] = sd_converter_.shiftToDepth(disparityData[i]);
374 static_cast<bool>(png_channels==1),
381 if (bShowStatistics_arg)
383 std::uint64_t pointCount = cloud_width * cloud_height;
384 float bytesPerPoint =
static_cast<float> (compressedDisparitySize+compressedColorSize) /
static_cast<float> (pointCount);
386 PCL_INFO(
"*** POINTCLOUD DECODING ***\n");
387 PCL_INFO(
"Number of encoded points: %ld\n", pointCount);
389 PCL_INFO(
"Size of compressed point cloud: %.2f kBytes\n",
static_cast<float> (compressedDisparitySize+compressedColorSize) / 1024.0f);
390 PCL_INFO(
"Total bytes per point: %.4f bytes\n",
static_cast<float> (bytesPerPoint));
399 template<
typename Po
intT>
void
402 float& focalLength_arg)
const
404 std::size_t width = cloud_arg->width;
405 std::size_t height = cloud_arg->height;
408 int centerX =
static_cast<int> (width / 2);
409 int centerY =
static_cast<int> (height / 2);
412 assert((width>1) && (height>1));
413 assert(width*height == cloud_arg->size());
416 float focalLength = 0;
419 for (
int y = -centerY; y < centerY; ++y )
420 for (
int x = -centerX; x < centerX; ++x )
422 const PointT& point = (*cloud_arg)[it++];
426 if (maxDepth < point.z)
432 focalLength = 2.0f / (point.x / (
static_cast<float> (x) * point.z) + point.y / (
static_cast<float> (y) * point.z));
438 maxDepth_arg = maxDepth;
439 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.
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.