DepthImageUtils.cpp
Go to the documentation of this file.
1 #include <cmath>
2 
3 #include <Image/ByteImage.h>
4 #include <Image/FloatImage.h>
5 
6 
7 
8 #include "DepthImageUtils.h"
9 
10 
11 using namespace armarx;
12 
13 
14 DepthImageUtils::DepthImageUtils(float fovH, float fovV)
15 {
16  setFieldOfView(fovH, fovV);
17 }
18 
19 void DepthImageUtils::setFieldOfView(float fovH, float fovV)
20 {
21  scaleX = std::tan(fovH * M_PI / 180.0 / 2.0) * 2.0;
22  scaleY = std::tan(fovV * M_PI / 180.0 / 2.0) * 2.0;
23 }
24 
25 void DepthImageUtils::updateCameraParameters(float fx, float fy, int width, int height)
26 {
27  const float fovH = 180.0 / M_PI * 2.0 * std::atan(width / (2.0 * fx));
28  const float fovV = 180.0 / M_PI * 2.0 * std::atan(height / (2.0 * fy));
29  setFieldOfView(fovH, fovV);
30 }
31 
32 void DepthImageUtils::convertDepthImageToPointCloud(CByteImage** images, armarx::MetaInfoSizeBasePtr imageMetaInfo, size_t numImages, pcl::PointCloud<pcl::PointXYZRGBL>::Ptr& result)
33 {
34 
35  result->width = images[0]->width;
36  result->height = images[0]->height;
37  result->header.stamp = imageMetaInfo->timeProvided;
38  result->points.resize(images[0]->width * images[0]->height);
39  result->is_dense = true;
40 
41 
42 
43  const size_t width = static_cast<size_t>(images[0]->width);
44  const size_t height = static_cast<size_t>(images[0]->height);
45  const float halfWidth = (width / 2.0);
46  const float halfHeight = (height / 2.0);
47  const auto& image0Data = images[0]->pixels;
48  const auto& image1Data = images[1]->pixels;
49 
50  for (size_t j = 0; j < height; j++)
51  {
52  for (size_t i = 0; i < width; i++)
53  {
54  auto coord = j * width + i;
55  // unsigned short value = images[1]->pixels[3 * coord + 0]
56  // + (images[1]->pixels[3 * coord + 1] << 8);
57  auto value = visionx::tools::rgbToDepthValue(image1Data[3 * coord + 0], image1Data[3 * coord + 1], image1Data[3 * coord + 2]);
58 
59  PointL& p = result->points.at(coord);
60  auto index = 3 * (coord);
61  p.r = image0Data[index + 0];
62  p.g = image0Data[index + 1];
63  p.b = image0Data[index + 2];
64 
65  if (value == 0)
66  {
67  p.x = p.y = p.z = std::numeric_limits<float>::quiet_NaN();
68  }
69  else
70  {
71  p.z = static_cast<float>(value);
72  //p.z /= 1000.0;
73  p.x = -1.0 * (i - halfWidth) / width * p.z * scaleX;
74  p.y = (halfHeight - j) / height * p.z * scaleY;
75  }
76 
77  if (numImages > 2)
78  {
79  auto& image2Data = images[2]->pixels;
80 
81  if (images[2]->bytesPerPixel == 3)
82  {
83  p.label = static_cast<unsigned int>(image2Data[index + 0] + (image2Data[index + 1] << 8) + (image2Data[index + 2] << 16));
84  }
85  else
86  {
87  p.label = static_cast<unsigned int>(image2Data[coord]);
88  }
89  }
90  else
91  {
92  p.label = 0;
93  }
94 
95  }
96  }
97 
98 
99 }
100 
101 
102 
armarx::DepthImageUtils::DepthImageUtils
DepthImageUtils(float fovH=43.0f, float fovV=57.0f)
Definition: DepthImageUtils.cpp:14
armarx::DepthImageUtils::convertDepthImageToPointCloud
void convertDepthImageToPointCloud(CByteImage **images, armarx::MetaInfoSizeBasePtr imageMetaInfo, size_t numImages, pcl::PointCloud< pcl::PointXYZRGBL >::Ptr &result)
Definition: DepthImageUtils.cpp:32
armarx::DepthImageUtils::updateCameraParameters
void updateCameraParameters(float fx, float fy, int width, int height)
Definition: DepthImageUtils.cpp:25
index
uint8_t index
Definition: EtherCATFrame.h:59
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
M_PI
#define M_PI
Definition: MathTools.h:17
armarx::DepthImageUtils::setFieldOfView
void setFieldOfView(float fovH, float fovV)
Definition: DepthImageUtils.cpp:19
DepthImageUtils.h
visionx::tools::rgbToDepthValue
float rgbToDepthValue(unsigned char r, unsigned char g, unsigned char b, bool noiseResistant=false)
Definition: ImageUtil.h:142
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28