forked from augcog/OpenARK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PMDCamera.cpp
194 lines (159 loc) · 5.2 KB
/
PMDCamera.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "stdafx.h"
#include "PMDCamera.h"
namespace ark {
/***
Private constructor for the PMD depth sensor
***/
PMDCamera::PMDCamera(bool use_live_sensor)
{
if (!use_live_sensor)
{
return;
}
std::cout << "Trying to open pmd\n";
auto res = pmdOpen(&hnd, SOURCE_PLUGIN, SOURCE_PARAM, PROC_PLUGIN, PROC_PARAM); //Open the PMD sensor
if (res != PMD_OK)
{
pmdGetLastError(0, err, 128);
fprintf(stderr, "Could not connect: %s\n", err);
return;
}
printf("opened sensor\n");
// Updating the sensor is necessary before any data can be retrieved
res = pmdUpdate(hnd);
if (res != PMD_OK)
{
pmdGetLastError(hnd, err, 128);
fprintf(stderr, "Couldn't transfer data: %s\n", err);
pmdClose(hnd);
return;
}
printf("acquired image\n");
// res: Structure which contains various meta-information about the data delivered by your Nano.
// It is advisabe to always use the data delivered by this struct (for example the width and height of the imager
// and the image format). Please refer to the PMSDSK documentation for more information
res = pmdGetSourceDataDescription(hnd, &dd);
if (res != PMD_OK)
{
pmdGetLastError(hnd, err, 128);
fprintf(stderr, "Couldn't get data description: %s\n", err);
pmdClose(hnd);
return;
}
printf("retrieved source data description\n");
if (dd.subHeaderType != PMD_IMAGE_DATA)
{
fprintf(stderr, "Source data is not an image!\n");
pmdClose(hnd);
return;
}
numPixels = dd.img.numRows * dd.img.numColumns; // Number of pixels in camera
dists = new float[3 * numPixels]; // Dists contains XYZ values. needs to be 3x the size of numPixels
amps = new float[numPixels];
frame.create(dd.img.numRows, dd.img.numColumns, CV_8UC3);
}
const std::string PMDCamera::getModelName() const {
return "PMD";
}
int PMDCamera::getWidth() const {
return 176;
}
int PMDCamera::getHeight() const {
return 120;
}
float PMDCamera::flagMapConfidenceThreshold() const {
return (60.0f / 255.0f*500.0f);
}
int PMDCamera::ampMapInvalidFlagValue() const {
return PMD_FLAG_INVALID;
}
bool PMDCamera::hasAmpMap() const
{
return true;
}
bool PMDCamera::hasFlagMap() const
{
return true;
}
/***
Public deconstructor for the PMD depth sensor
***/
PMDCamera::~PMDCamera()
{
printf("closing sensor\n");
pmdClose(hnd);
printf("sensor closed\n");
}
/***
Create xyzMap, zMap, ampMap, and flagMap from sensor input
***/
void PMDCamera::update(cv::Mat & xyz_map, cv::Mat & rgb_map, cv::Mat & ir_map, cv::Mat & fisheye_map,
cv::Mat & amp_map, cv::Mat & flag_map)
{
// fill in amp map
auto res = pmdGetAmplitudes(hnd, amps, numPixels * sizeof(float));
if (res != PMD_OK)
{
pmdGetLastError(hnd, err, 128);
fprintf(stderr, "Couldn't get amplitudes: %s\n", err);
pmdClose(hnd);
return;
}
amp_map.data = reinterpret_cast<uchar *>(amps);
// fill in Z coordinates
auto res = pmdGet3DCoordinates(hnd, dists, 3 * numPixels * sizeof(float)); //store x,y,z coordinates dists (type: float*)
//float * zCoords = new float[1]; //store z-Coordinates of dists in zCoords
if (res != PMD_OK)
{
pmdGetLastError(hnd, err, 128);
fprintf(stderr, "Couldn't get 3D coordinates: %s\n", err);
pmdClose(hnd);
return;
}
xyz_map = cv::Mat(xyzMap.size(), xyzMap.type(), dists);
// Flags. Helps with denoising.
auto flags = new unsigned[ampMap.cols*ampMap.rows];
auto res = pmdGetFlags(hnd, flags, numPixels * sizeof(unsigned));
if (res != PMD_OK)
{
pmdGetLastError(hnd, err, 128);
fprintf(stderr, "Couldn't get the flags: %s\n", err);
pmdClose(hnd);
return;
}
flag_map.data = reinterpret_cast<uchar *>(flags);
res = pmdUpdate(hnd);
if (res != PMD_OK)
{
pmdGetLastError(hnd, err, 128);
fprintf(stderr, "Couldn't update the PMD camera: %s\n", err);
pmdClose(hnd);
return;
}
delete flags;
}
/***
Returns the X value at (i, j)
***/
float PMDCamera::getX(int i, int j) const
{
int flat = j * dd.img.numColumns * 3 + i * 3;
return dists[flat];
}
/***
Returns the Y value at (i, j)
***/
float PMDCamera::getY(int i, int j) const
{
int flat = j * dd.img.numColumns * 3 + i * 3;
return dists[flat + 1];
}
/***
Returns the Z value at (i, j)
***/
float PMDCamera::getZ(int i, int j) const
{
int flat = j * dd.img.numColumns * 3 + i * 3;
return dists[flat + 2];
}
}