This will implement the H264 encoding and decoding of depth measures discussed in this paper: 2011-Adapting-Standard-Video-Codecs-for-Depth-Streaming.
96{
97
98 const float fW = 65536.0f;
99 const float fNP = 512.0f;
100 const int nH264CompressionLevel = 10;
101
103
105
107
108 for (int nY = 0; nY < cvDepthMeasure.rows; ++nY)
109 {
110 for (int nX = 0; nX < cvDepthMeasure.cols; ++nX)
111 {
113 }
114 }
115
116
118
119 for (int nI = 0; nI < 10; ++nI)
120 {
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);
125
126 for (int nY = nCenterY - nRadius; nY <= nCenterY + nRadius; ++nY)
127 {
128 if (nY < 0 || nY >= cvDepthMeasure.rows)
129 continue;
130
131 for (int nX = nCenterX - nRadius; nX <= nCenterX + nRadius; ++nX)
132 {
133 if (nX < 0 || nX >= cvDepthMeasure.cols)
134 continue;
135
136 if ((nX - nCenterX) * (nX - nCenterX) + (nY - nCenterY) * (nY - nCenterY) <= nRadius * nRadius)
137 {
138 cvDepthMeasure.at<
uint16_t>(nY, nX) = unDepth;
139 }
140 }
141 }
142 }
143
144 for (int nI = 0; nI < 8; ++nI)
145 {
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);
150
151 for (int nY = nCenterY - nSize / 2; nY <= nCenterY + nSize / 2; ++nY)
152 {
153 if (nY < 0 || nY >= cvDepthMeasure.rows)
154 continue;
155
156 for (int nX = nCenterX - nSize / 2; nX <= nCenterX + nSize / 2; ++nX)
157 {
158 if (nX < 0 || nX >= cvDepthMeasure.cols)
159 continue;
160 cvDepthMeasure.at<
uint16_t>(nY, nX) = unDepth;
161 }
162 }
163 }
164
165 for (int nI = 0; nI < 6; ++nI)
166 {
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);
171
172
174 cv::Point(nCenterX - nSize / 2, nCenterY + nSize / 2),
175 cv::Point(nCenterX + nSize / 2, nCenterY + nSize / 2)};
176
177
179 }
180
182
184
186
187 for (int nY = 0; nY < cvDepthMeasure.rows; ++nY)
188 {
189 for (int nX = 0; nX < cvDepthMeasure.cols; ++nX)
190 {
191
192
194
195
196 float fNormalizedDepth = (unDepthVal + 0.5f) / fW;
197
198
199 float fP = fNP / fW;
200
201
202 float fL = fNormalizedDepth;
203
204
205 float fHa = fmod(fL / (fP / 2.0f), 2.0f);
206 fHa = fHa <= 1.0f ? fHa : 2.0f - fHa;
207
208
209 float fHb = fmod((fL - (fP / 4.0f)) / (fP / 2.0f), 2.0f);
210 fHb = fHb <= 1.0f ? fHb : 2.0f - fHb;
211
212
216
217
219 }
220 }
221
222
223
224 std::string szInputFilename = "temp_encoded_depth.png";
225 std::string szH264Filename = "temp_h264.mp4";
226 std::string szOutputFilename = "temp_decoded_depth.png";
227
228
230 {
231 std::cerr << "Failed to save encoded depth image to " << szInputFilename << std::endl;
232 }
233 std::cout << "Simulating H264 encoding/decoding with ffmpeg..." << std::endl;
234
235
237
238
239
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";
242
243 std::string szDecoderCMD = "ffmpeg -y -i " + szH264Filename + " -frames:v 1 " + szOutputFilename + " 2>/dev/null";
244
245
246 int nEncoderResult = system(szEncoderCMD.c_str());
247 if (nEncoderResult != 0)
248 {
249 std::cerr << "Error running ffmpeg encoder. Make sure ffmpeg is installed." << std::endl;
250 }
251
252 int nDecoderResult = system(szDecoderCMD.c_str());
253 if (nDecoderResult != 0)
254 {
255 std::cerr << "Error running ffmpeg decoder." << std::endl;
256 }
257
258
260 if (cvH264ProcessedDepth.
empty())
261 {
262 std::cerr << "Error reading H264-processed depth image" << std::endl;
263 }
264 else
265 {
266
267 cv::imshow(
"Pre-H264 Encoded Depth", cvPreH264Depth);
268 cv::imshow(
"Post-H264 Encoded Depth", cvH264ProcessedDepth);
270
271
272 cvEncodedDepth = cvH264ProcessedDepth;
273
274 std::cout << "H264 encoding/decoding simulation complete" << std::endl;
275 }
276
277
278#ifdef _WIN32
279 system(("del " + szInputFilename + " " + szH264Filename + " " + szOutputFilename).c_str());
280#else
281 system(("rm -f " + szInputFilename + " " + szH264Filename + " " + szOutputFilename).c_str());
282#endif
283
285
287
289
290 for (int nY = 0; nY < cvEncodedDepth.rows; ++nY)
291 {
292 for (int nX = 0; nX < cvEncodedDepth.cols; ++nX)
293 {
294
296
297
298 float fL = cvPixel[0] / 255.0f;
299 float fHa = cvPixel[1] / 255.0f;
300 float fHb = cvPixel[2] / 255.0f;
301
302
303 float fP = fNP / fW;
304
305
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);
308
309 float fDelta = 0.0f;
310 if (fM == 0)
311 fDelta = (fP / 2.0f) * fHa;
312 else if (fM == 1)
313 fDelta = (fP / 2.0f) * fHb;
314 else if (fM == 2)
315 fDelta = (fP / 2.0f) * (1.0f - fHa);
316 else if (fM == 3)
317 fDelta = (fP / 2.0f) * (1.0f - fHb);
318
319
320 float fDepth = fW * (fL0 + fDelta);
321
322
323 if (fDepth < 0.0f)
324 fDepth = 0.0f;
325 else if (fDepth > fW)
326 fDepth = fW;
327
328
330 }
331 }
332
333
334 cv::imshow(
"Original Depth Measure", cvDepthMeasure);
335 cv::imshow(
"Decoded Depth Measure", cvDecodedDepth);
336
339
340 std::cout << "Click on images to see depth values. Press 'q' or ESC to exit." << std::endl;
341
342
343 int nKey;
344 while (true)
345 {
347 if (nKey == 'q' || nKey == 27)
348 break;
349 }
351
352
354
355 double dHorizontalFOV = 90.0;
356 double dVerticalFOV = 60.0;
357
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);
361
362
363 const double dCx = cvDecodedDepth.cols / 2.0;
364 const double dCy = cvDecodedDepth.rows / 2.0;
365
366
367 for (
int nY = 0; nY < cvDecodedDepth.
rows; ++nY)
368 {
369 for (int nX = 0; nX < cvDecodedDepth.cols; ++nX)
370 {
371
372 float fDepth =
static_cast<float>(cvDecodedDepth.at<
uint16_t>(nY, nX));
373
374
375 if (fDepth <= 0)
376 {
378 continue;
379 }
380
381
382 float fX = static_cast<float>((nX - dCx) * fDepth / dFx);
383 float fY = static_cast<float>((nY - dCy) * fDepth / dFy);
384 float fZ = fDepth;
385
386
388 }
389 }
390 std::cout << "Point cloud generated with " << (cvDecodedDepth.rows * cvDecodedDepth.cols) << " points." << std::endl;
391
392
393 pcl::PointCloud<pcl::PointXYZ>::Ptr pclCloud(new pcl::PointCloud<pcl::PointXYZ>());
394
395 pclCloud->width = cvPointCloud.cols;
396 pclCloud->height = cvPointCloud.rows;
397 pclCloud->is_dense = false;
398 pclCloud->points.resize(pclCloud->width * pclCloud->height);
399
400
401 for (
int nY = 0; nY < cvPointCloud.
rows; ++nY)
402 {
403 for (int nX = 0; nX < cvPointCloud.cols; ++nX)
404 {
406 size_t idx = nY * cvPointCloud.
cols + nX;
407
408
409 if (point[2] > 0)
410 {
411 pclCloud->points[idx].x = point[0];
412 pclCloud->points[idx].y = point[1];
413 pclCloud->points[idx].z = point[2];
414 }
415 else
416 {
417
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();
421 }
422 }
423 }
424
425
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;
430
431
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;
438
439
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();
446
447 std::cout << "Visualizing point cloud. Close the pclViewer window to continue." << std::endl;
448
449
450 while (!pclViewer->wasStopped())
451 {
452 pclViewer->spinOnce(100);
453 }
454}
void DecodedDepthMouseCallback(int nEvent, int nX, int nY, int nFlags, void *pUserData)
This is a mouse callback function that will be called when the user clicks on the decoded depth image...
Definition DepthMeasureEncodeDecode.hpp:72
void OriginalDepthMouseCallback(int nEvent, int nX, int nY, int nFlags, void *pUserData)
This is a mouse callback function that will be called when the user clicks on the original depth imag...
Definition DepthMeasureEncodeDecode.hpp:44
CV_NODISCARD_STD Mat clone() const
__device__ __forceinline__ float3 tan(const uchar3 &a)
void imshow(const String &winname, InputArray mat)
void setMouseCallback(const String &winname, MouseCallback onMouse, void *userdata=0)
Mat imread(const String &filename, int flags=IMREAD_COLOR)
bool imwrite(const String &filename, InputArray img, const std::vector< int > ¶ms=std::vector< int >())
void fillConvexPoly(InputOutputArray img, InputArray points, const Scalar &color, int lineType=LINE_8, int shift=0)