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