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[0]), 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[0]), 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[0];
198 std::uint8_t* color_ptr = &colorImage_arg[0];
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 memset(color_ptr, 0,
sizeof(std::uint8_t)*3);
207 encodeMonoImageToPNG (disparityMap_arg, width_arg, height_arg, compressedDisparity, pngLevel_arg);
209 compressedDisparitySize =
static_cast<std::uint32_t
>(compressedDisparity.size());
211 compressedDataOut_arg.write (
reinterpret_cast<const char*
> (&compressedDisparitySize),
sizeof (compressedDisparitySize));
213 compressedDataOut_arg.write (
reinterpret_cast<const char*
> (&compressedDisparity[0]), compressedDisparity.size () *
sizeof(std::uint8_t));
216 if (!colorImage_arg.empty () && doColorEncoding)
220 std::vector<std::uint8_t> monoImage;
221 std::size_t size = width_arg*height_arg;
223 monoImage.reserve(size);
226 for (std::size_t i = 0; i < size; ++i)
228 std::uint8_t grayvalue =
static_cast<std::uint8_t
>(0.2989 *
static_cast<float>(colorImage_arg[i*3+0]) +
229 0.5870 *
static_cast<float>(colorImage_arg[i*3+1]) +
230 0.1140 *
static_cast<float>(colorImage_arg[i*3+2]));
231 monoImage.push_back(grayvalue);
242 compressedColorSize =
static_cast<std::uint32_t
>(compressedColor.size ());
244 compressedDataOut_arg.write (
reinterpret_cast<const char*
> (&compressedColorSize),
sizeof (compressedColorSize));
246 compressedDataOut_arg.write (
reinterpret_cast<const char*
> (&compressedColor[0]), compressedColor.size () *
sizeof(std::uint8_t));
248 if (bShowStatistics_arg)
250 std::uint64_t pointCount = width_arg * height_arg;
251 float bytesPerPoint =
static_cast<float> (compressedDisparitySize+compressedColorSize) /
static_cast<float> (pointCount);
253 PCL_INFO(
"*** POINTCLOUD ENCODING ***\n");
254 PCL_INFO(
"Number of encoded points: %ld\n", pointCount);
255 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);
256 PCL_INFO(
"Size of compressed point cloud: %.2f kBytes\n",
static_cast<float> (compressedDisparitySize+compressedColorSize) / 1024.0f);
257 PCL_INFO(
"Total bytes per point: %.4f bytes\n",
static_cast<float> (bytesPerPoint));
258 PCL_INFO(
"Total compression percentage: %.4f%%\n", (bytesPerPoint) / (
sizeof(std::uint8_t)*3+
sizeof(std::uint16_t)) * 100.0f);
263 compressedDataOut_arg.flush();
267 template<
typename Po
intT>
bool
270 bool bShowStatistics_arg)
272 std::uint32_t cloud_width;
273 std::uint32_t cloud_height;
276 float disparityShift = 0.0f;
277 float disparityScale;
280 std::vector<std::uint16_t> disparityData;
281 std::vector<std::uint8_t> colorData;
284 std::vector<std::uint8_t> compressedDisparity;
285 std::vector<std::uint8_t> compressedColor;
287 std::uint32_t compressedDisparitySize;
288 std::uint32_t compressedColorSize;
291 unsigned int png_channels = 1;
294 unsigned int headerIdPos = 0;
295 bool valid_stream =
true;
296 while (valid_stream && (headerIdPos < strlen (frameHeaderIdentifier_)))
299 compressedDataIn_arg.read (
static_cast<char*
> (&readChar),
sizeof (readChar));
300 if (compressedDataIn_arg.gcount()!= sizeof (readChar))
301 valid_stream =
false;
302 if (readChar != frameHeaderIdentifier_[headerIdPos++])
303 headerIdPos = (frameHeaderIdentifier_[0] == readChar) ? 1 : 0;
305 valid_stream &= compressedDataIn_arg.good ();
312 compressedDataIn_arg.read (
reinterpret_cast<char*
> (&cloud_width),
sizeof (cloud_width));
313 compressedDataIn_arg.read (
reinterpret_cast<char*
> (&cloud_height),
sizeof (cloud_height));
314 compressedDataIn_arg.read (
reinterpret_cast<char*
> (&maxDepth),
sizeof (maxDepth));
315 compressedDataIn_arg.read (
reinterpret_cast<char*
> (&focalLength),
sizeof (focalLength));
316 compressedDataIn_arg.read (
reinterpret_cast<char*
> (&disparityScale),
sizeof (disparityScale));
317 compressedDataIn_arg.read (
reinterpret_cast<char*
> (&disparityShift),
sizeof (disparityShift));
320 compressedDataIn_arg.read (
reinterpret_cast<char*
> (&compressedDisparitySize),
sizeof (compressedDisparitySize));
321 compressedDisparity.resize (compressedDisparitySize);
322 compressedDataIn_arg.read (
reinterpret_cast<char*
> (&compressedDisparity[0]), compressedDisparitySize *
sizeof(std::uint8_t));
325 compressedDataIn_arg.read (
reinterpret_cast<char*
> (&compressedColorSize),
sizeof (compressedColorSize));
326 compressedColor.resize (compressedColorSize);
327 compressedDataIn_arg.read (
reinterpret_cast<char*
> (&compressedColor[0]), compressedColorSize *
sizeof(std::uint8_t));
329 std::size_t png_width = 0;
330 std::size_t png_height = 0;
333 decodePNGToImage (compressedDisparity, disparityData, png_width, png_height, png_channels);
336 decodePNGToImage (compressedColor, colorData, png_width, png_height, png_channels);
339 if (disparityShift==0.0f)
355 std::size_t size = disparityData.size();
356 std::vector<float> depthData;
357 depthData.resize(size);
360 if (!sd_converter_.isInitialized())
361 sd_converter_.generateLookupTable();
364 for (std::size_t i=0; i<size; ++i)
365 depthData[i] = sd_converter_.shiftToDepth(disparityData[i]);
370 static_cast<bool>(png_channels==1),
377 if (bShowStatistics_arg)
379 std::uint64_t pointCount = cloud_width * cloud_height;
380 float bytesPerPoint =
static_cast<float> (compressedDisparitySize+compressedColorSize) /
static_cast<float> (pointCount);
382 PCL_INFO(
"*** POINTCLOUD DECODING ***\n");
383 PCL_INFO(
"Number of encoded points: %ld\n", pointCount);
385 PCL_INFO(
"Size of compressed point cloud: %.2f kBytes\n",
static_cast<float> (compressedDisparitySize+compressedColorSize) / 1024.0f);
386 PCL_INFO(
"Total bytes per point: %.4f bytes\n",
static_cast<float> (bytesPerPoint));
395 template<
typename Po
intT>
void
398 float& focalLength_arg)
const
400 std::size_t width = cloud_arg->width;
401 std::size_t height = cloud_arg->height;
404 int centerX =
static_cast<int> (width / 2);
405 int centerY =
static_cast<int> (height / 2);
408 assert((width>1) && (height>1));
409 assert(width*height == cloud_arg->size());
412 float focalLength = 0;
415 for (
int y = -centerY; y < centerY; ++y )
416 for (
int x = -centerX; x < centerX; ++x )
418 const PointT& point = (*cloud_arg)[it++];
422 if (maxDepth < point.z)
428 focalLength = 2.0f / (point.x / (
static_cast<float> (x) * point.z) + point.y / (
static_cast<float> (y) * point.z));
434 maxDepth_arg = maxDepth;
435 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.