98 const float fW = 65536.0f;
99 const float fNP = 512.0f;
100 const int nH264CompressionLevel = 10;
108 for (
int nY = 0; nY < cvDepthMeasure.
rows; ++nY)
110 for (
int nX = 0; nX < cvDepthMeasure.
cols; ++nX)
119 for (
int nI = 0; nI < 10; ++nI)
121 int nCenterX = RNG.
uniform(50, cvDepthMeasure.
cols - 50);
122 int nCenterY = RNG.
uniform(50, cvDepthMeasure.
rows - 50);
123 int nRadius = RNG.
uniform(30, 100);
126 for (
int nY = nCenterY - nRadius; nY <= nCenterY + nRadius; ++nY)
128 if (nY < 0 || nY >= cvDepthMeasure.
rows)
131 for (
int nX = nCenterX - nRadius; nX <= nCenterX + nRadius; ++nX)
133 if (nX < 0 || nX >= cvDepthMeasure.
cols)
136 if ((nX - nCenterX) * (nX - nCenterX) + (nY - nCenterY) * (nY - nCenterY) <= nRadius * nRadius)
138 cvDepthMeasure.
at<
uint16_t>(nY, nX) = unDepth;
144 for (
int nI = 0; nI < 8; ++nI)
146 int nCenterX = RNG.
uniform(50, cvDepthMeasure.
cols - 50);
147 int nCenterY = RNG.
uniform(50, cvDepthMeasure.
rows - 50);
148 int nSize = RNG.
uniform(30, 100);
151 for (
int nY = nCenterY - nSize / 2; nY <= nCenterY + nSize / 2; ++nY)
153 if (nY < 0 || nY >= cvDepthMeasure.
rows)
156 for (
int nX = nCenterX - nSize / 2; nX <= nCenterX + nSize / 2; ++nX)
158 if (nX < 0 || nX >= cvDepthMeasure.
cols)
160 cvDepthMeasure.
at<
uint16_t>(nY, nX) = unDepth;
165 for (
int nI = 0; nI < 6; ++nI)
167 int nCenterX = RNG.
uniform(50, cvDepthMeasure.
cols - 50);
168 int nCenterY = RNG.
uniform(50, cvDepthMeasure.
rows - 50);
169 int nSize = RNG.
uniform(40, 120);
174 cv::Point(nCenterX - nSize / 2, nCenterY + nSize / 2),
175 cv::Point(nCenterX + nSize / 2, nCenterY + nSize / 2)};
187 for (
int nY = 0; nY < cvDepthMeasure.
rows; ++nY)
189 for (
int nX = 0; nX < cvDepthMeasure.
cols; ++nX)
196 float fNormalizedDepth = (unDepthVal + 0.5f) / fW;
202 float fL = fNormalizedDepth;
205 float fHa = fmod(fL / (fP / 2.0f), 2.0f);
206 fHa = fHa <= 1.0f ? fHa : 2.0f - fHa;
209 float fHb = fmod((fL - (fP / 4.0f)) / (fP / 2.0f), 2.0f);
210 fHb = fHb <= 1.0f ? fHb : 2.0f - fHb;
224 std::string szInputFilename =
"temp_encoded_depth.png";
225 std::string szH264Filename =
"temp_h264.mp4";
226 std::string szOutputFilename =
"temp_decoded_depth.png";
231 std::cerr <<
"Failed to save encoded depth image to " << szInputFilename << std::endl;
233 std::cout <<
"Simulating H264 encoding/decoding with ffmpeg..." << std::endl;
240 std::string szEncoderCMD =
"ffmpeg -y -i " + szInputFilename +
" -c:v libx264 -preset fast -tune zerolatency -crf " + std::to_string(nH264CompressionLevel) +
" " +
241 szH264Filename +
" 2>/dev/null";
243 std::string szDecoderCMD =
"ffmpeg -y -i " + szH264Filename +
" -frames:v 1 " + szOutputFilename +
" 2>/dev/null";
246 int nEncoderResult = system(szEncoderCMD.c_str());
247 if (nEncoderResult != 0)
249 std::cerr <<
"Error running ffmpeg encoder. Make sure ffmpeg is installed." << std::endl;
252 int nDecoderResult = system(szDecoderCMD.c_str());
253 if (nDecoderResult != 0)
255 std::cerr <<
"Error running ffmpeg decoder." << std::endl;
260 if (cvH264ProcessedDepth.
empty())
262 std::cerr <<
"Error reading H264-processed depth image" << std::endl;
267 cv::imshow(
"Pre-H264 Encoded Depth", cvPreH264Depth);
268 cv::imshow(
"Post-H264 Encoded Depth", cvH264ProcessedDepth);
272 cvEncodedDepth = cvH264ProcessedDepth;
274 std::cout <<
"H264 encoding/decoding simulation complete" << std::endl;
279 system((
"del " + szInputFilename +
" " + szH264Filename +
" " + szOutputFilename).c_str());
281 system((
"rm -f " + szInputFilename +
" " + szH264Filename +
" " + szOutputFilename).c_str());
290 for (
int nY = 0; nY < cvEncodedDepth.
rows; ++nY)
292 for (
int nX = 0; nX < cvEncodedDepth.
cols; ++nX)
298 float fL = cvPixel[0] / 255.0f;
299 float fHa = cvPixel[1] / 255.0f;
300 float fHb = cvPixel[2] / 255.0f;
306 int fM =
static_cast<int>(std::floor((4.0f * (fL / fP)) - 0.5f)) % 4;
307 float fL0 = fL - fmod(fL - (fP / 8.0f), fP) + ((fP / 4.0f) * fM) - (fP / 8.0f);
311 fDelta = (fP / 2.0f) * fHa;
313 fDelta = (fP / 2.0f) * fHb;
315 fDelta = (fP / 2.0f) * (1.0f - fHa);
317 fDelta = (fP / 2.0f) * (1.0f - fHb);
320 float fDepth = fW * (fL0 + fDelta);
325 else if (fDepth > fW)
334 cv::imshow(
"Original Depth Measure", cvDepthMeasure);
335 cv::imshow(
"Decoded Depth Measure", cvDecodedDepth);
340 std::cout <<
"Click on images to see depth values. Press 'q' or ESC to exit." << std::endl;
347 if (nKey ==
'q' || nKey == 27)
355 double dHorizontalFOV = 90.0;
356 double dVerticalFOV = 60.0;
358 const double dRadPerDeg = M_PI / 180.0;
359 const double dFx = (cvDecodedDepth.
cols / 2.0) /
tan(dHorizontalFOV * dRadPerDeg / 2.0);
360 const double dFy = (cvDecodedDepth.
rows / 2.0) /
tan(dVerticalFOV * dRadPerDeg / 2.0);
363 const double dCx = cvDecodedDepth.
cols / 2.0;
364 const double dCy = cvDecodedDepth.
rows / 2.0;
367 for (
int nY = 0; nY < cvDecodedDepth.
rows; ++nY)
369 for (
int nX = 0; nX < cvDecodedDepth.
cols; ++nX)
372 float fDepth =
static_cast<float>(cvDecodedDepth.
at<
uint16_t>(nY, nX));
382 float fX =
static_cast<float>((nX - dCx) * fDepth / dFx);
383 float fY =
static_cast<float>((nY - dCy) * fDepth / dFy);
390 std::cout <<
"Point cloud generated with " << (cvDecodedDepth.
rows * cvDecodedDepth.
cols) <<
" points." << std::endl;
393 pcl::PointCloud<pcl::PointXYZ>::Ptr pclCloud(
new pcl::PointCloud<pcl::PointXYZ>());
395 pclCloud->width = cvPointCloud.
cols;
396 pclCloud->height = cvPointCloud.
rows;
397 pclCloud->is_dense =
false;
398 pclCloud->points.resize(pclCloud->width * pclCloud->height);
401 for (
int nY = 0; nY < cvPointCloud.
rows; ++nY)
403 for (
int nX = 0; nX < cvPointCloud.
cols; ++nX)
406 size_t idx = nY * cvPointCloud.
cols + nX;
411 pclCloud->points[idx].x = point[0];
412 pclCloud->points[idx].y = point[1];
413 pclCloud->points[idx].z = point[2];
418 pclCloud->points[idx].x = std::numeric_limits<float>::quiet_NaN();
419 pclCloud->points[idx].y = std::numeric_limits<float>::quiet_NaN();
420 pclCloud->points[idx].z = std::numeric_limits<float>::quiet_NaN();
426 pcl::PointCloud<pcl::PointXYZ>::Ptr pclFilteredPointCloud(
new pcl::PointCloud<pcl::PointXYZ>);
427 std::vector<int> vIndices;
428 pcl::removeNaNFromPointCloud(*pclCloud, *pclFilteredPointCloud, vIndices);
429 std::cout <<
"Point cloud filtered: " << pclFilteredPointCloud->points.size() <<
" valid points." << std::endl;
432 pcl::PointCloud<pcl::PointXYZ>::Ptr pclDownsampledPointCloud(
new pcl::PointCloud<pcl::PointXYZ>);
433 pcl::VoxelGrid<pcl::PointXYZ> pclVoxelGrid;
434 pclVoxelGrid.setInputCloud(pclFilteredPointCloud);
435 pclVoxelGrid.setLeafSize(5.0f, 5.0f, 5.0f);
436 pclVoxelGrid.filter(*pclDownsampledPointCloud);
437 std::cout <<
"Point cloud downsampled to " << pclDownsampledPointCloud->points.size() <<
" points." << std::endl;
440 pcl::visualization::PCLVisualizer::Ptr pclViewer(
new pcl::visualization::PCLVisualizer(
"Depth Point Cloud"));
441 pclViewer->setBackgroundColor(0, 0, 0);
442 pclViewer->addPointCloud<pcl::PointXYZ>(pclDownsampledPointCloud,
"depth_cloud");
443 pclViewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2,
"depth_cloud");
444 pclViewer->addCoordinateSystem(1000.0);
445 pclViewer->initCameraParameters();
447 std::cout <<
"Visualizing point cloud. Close the pclViewer window to continue." << std::endl;
450 while (!pclViewer->wasStopped())
452 pclViewer->spinOnce(100);