Skip to content

Commit cda9ea3

Browse files
committed
Merge pull request #226 from floe/registration
add convenience method & sample code for registration
2 parents 21d88a2 + 769fa80 commit cda9ea3

File tree

3 files changed

+61
-7
lines changed

3 files changed

+61
-7
lines changed

examples/protonect/Protonect.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <libfreenect2/libfreenect2.hpp>
3434
#include <libfreenect2/frame_listener_impl.h>
3535
#include <libfreenect2/threading.h>
36+
#include <libfreenect2/registration.h>
3637

3738
bool protonect_shutdown = false;
3839

@@ -76,6 +77,9 @@ int main(int argc, char *argv[])
7677
std::cout << "device serial: " << dev->getSerialNumber() << std::endl;
7778
std::cout << "device firmware: " << dev->getFirmwareVersion() << std::endl;
7879

80+
libfreenect2::Registration* registration = new libfreenect2::Registration(dev->getIrCameraParams(), dev->getColorCameraParams());
81+
unsigned char* registered = NULL;
82+
7983
while(!protonect_shutdown)
8084
{
8185
listener.waitForNewFrame(frames);
@@ -87,6 +91,10 @@ int main(int argc, char *argv[])
8791
cv::imshow("ir", cv::Mat(ir->height, ir->width, CV_32FC1, ir->data) / 20000.0f);
8892
cv::imshow("depth", cv::Mat(depth->height, depth->width, CV_32FC1, depth->data) / 4500.0f);
8993

94+
if (!registered) registered = new unsigned char[depth->height*depth->width*rgb->bytes_per_pixel];
95+
registration->apply(rgb,depth,registered);
96+
cv::imshow("registered", cv::Mat(depth->height, depth->width, CV_8UC3, registered));
97+
9098
int key = cv::waitKey(1);
9199
protonect_shutdown = protonect_shutdown || (key > 0 && ((key & 0xFF) == 27)); // shutdown on escape
92100

@@ -99,5 +107,8 @@ int main(int argc, char *argv[])
99107
dev->stop();
100108
dev->close();
101109

110+
delete[] registered;
111+
delete registration;
112+
102113
return 0;
103114
}

examples/protonect/include/libfreenect2/registration.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,22 @@
3030
#include <string>
3131
#include <libfreenect2/config.h>
3232
#include <libfreenect2/libfreenect2.hpp>
33+
#include <libfreenect2/frame_listener.hpp>
3334

3435
namespace libfreenect2
3536
{
3637

3738
class LIBFREENECT2_API Registration
3839
{
3940
public:
40-
Registration(Freenect2Device::IrCameraParams *depth_p, Freenect2Device::ColorCameraParams *rgb_p);
41+
Registration(Freenect2Device::IrCameraParams depth_p, Freenect2Device::ColorCameraParams rgb_p);
4142

43+
// undistort/register a single depth data point
4244
void apply( int dx, int dy, float dz, float& cx, float &cy);
4345

46+
// undistort/register a whole image
47+
void apply(Frame* rgb, Frame* depth, unsigned char* registered);
48+
4449
private:
4550
void undistort_depth(int dx, int dy, float& mx, float& my);
4651
void depth_to_color(float mx, float my, float& rx, float& ry);

examples/protonect/src/registration.cpp

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,22 +87,60 @@ void Registration::apply( int dx, int dy, float dz, float& cx, float &cy)
8787
cy = ry * color.fy + color.cy;
8888
}
8989

90-
Registration::Registration(Freenect2Device::IrCameraParams *depth_p, Freenect2Device::ColorCameraParams *rgb_p):
91-
depth(*depth_p), color(*rgb_p)
90+
void Registration::apply(Frame* rgb, Frame* depth, unsigned char* registered)
91+
{
92+
if (!depth || !rgb || !registered)
93+
return;
94+
95+
float* depth_raw = (float*)depth->data;
96+
float cx, cy;
97+
int c_off, d_off, r_off;
98+
99+
for (int x = 0; x < depth->width; x++) {
100+
for (int y = 0; y < depth->height; y++) {
101+
102+
d_off = y*depth->width + x;
103+
r_off = d_off*rgb->bytes_per_pixel;
104+
105+
float z_raw = depth_raw[d_off];
106+
if (z_raw == 0.0) {
107+
registered[r_off+0] = 0;
108+
registered[r_off+1] = 0;
109+
registered[r_off+2] = 0;
110+
continue;
111+
}
112+
113+
apply(x,y,z_raw,cx,cy);
114+
115+
c_off = (round(cx) + round(cy) * rgb->width) * rgb->bytes_per_pixel;
116+
if ((c_off < 0) || (c_off > rgb->width*rgb->height*rgb->bytes_per_pixel)) {
117+
registered[r_off+0] = 0;
118+
registered[r_off+1] = 0;
119+
registered[r_off+2] = 0;
120+
continue;
121+
}
122+
123+
registered[r_off+0] = rgb->data[c_off+0];
124+
registered[r_off+1] = rgb->data[c_off+1];
125+
registered[r_off+2] = rgb->data[c_off+2];
126+
}
127+
}
128+
129+
}
130+
131+
Registration::Registration(Freenect2Device::IrCameraParams depth_p, Freenect2Device::ColorCameraParams rgb_p):
132+
depth(depth_p), color(rgb_p)
92133
{
93134
float mx, my;
94135
float rx, ry;
95136

96137
for (int x = 0; x < 512; x++)
97138
for (int y = 0; y < 424; y++) {
139+
98140
undistort_depth(x,y,mx,my);
99141
undistort_map[x][y][0] = mx;
100142
undistort_map[x][y][1] = my;
101-
}
102143

103-
for (int x = 0; x < 512; x++)
104-
for (int y = 0; y < 424; y++) {
105-
undistort_depth(x,y,mx,my);
106144
depth_to_color(mx,my,rx,ry);
107145
depth_to_color_map[x][y][0] = rx;
108146
depth_to_color_map[x][y][1] = ry;

0 commit comments

Comments
 (0)