45
45
#include < libfreenect2/frame_listener_impl.h>
46
46
#include < libfreenect2/packet_pipeline.h>
47
47
#include < libfreenect2/config.h>
48
+ #include < libfreenect2/registration.h>
48
49
49
50
#include < kinect2_definitions.h>
50
51
#include < depth_registration.h>
@@ -73,6 +74,10 @@ class Kinect2Bridge
73
74
libfreenect2::Freenect2Device *device;
74
75
libfreenect2::SyncMultiFrameListener *listenerColor, *listenerIrDepth;
75
76
libfreenect2::PacketPipeline *packetPipeline;
77
+ libfreenect2::Registration *registration;
78
+ libfreenect2::Freenect2Device::ColorCameraParams colorParams;
79
+ libfreenect2::Freenect2Device::IrCameraParams irParams;
80
+ std::shared_ptr<libfreenect2::Frame> irFrame, depthFrame, colorFrame;
76
81
77
82
ros::NodeHandle nh, priv_nh;
78
83
@@ -87,14 +92,15 @@ class Kinect2Bridge
87
92
88
93
enum Image
89
94
{
90
- IR = 0 ,
91
- IR_RECT,
92
- DEPTH,
93
- DEPTH_RECT,
95
+ IR_SD = 0 ,
96
+ IR_SD_RECT,
94
97
98
+ DEPTH_SD,
99
+ DEPTH_SD_RECT,
95
100
DEPTH_HD,
96
101
DEPTH_QHD,
97
102
103
+ COLOR_SD_RECT,
98
104
COLOR_HD,
99
105
COLOR_HD_RECT,
100
106
COLOR_QHD,
@@ -123,8 +129,9 @@ class Kinect2Bridge
123
129
124
130
public:
125
131
Kinect2Bridge (const ros::NodeHandle &nh = ros::NodeHandle(), const ros::NodeHandle &priv_nh = ros::NodeHandle(" ~" ))
126
- : sizeColor(1920 , 1080 ), sizeIr(512 , 424 ), sizeLowRes(sizeColor.width / 2 , sizeColor.height / 2 ), nh(nh), priv_nh(priv_nh), frameColor(0 ), frameIrDepth(0 ),
127
- pubFrameColor (0 ), pubFrameIrDepth(0 ), lastColor(0 , 0 ), lastDepth(0 , 0 ), nextColor(false ), nextIrDepth(false ), depthShift(0 ), running(false ), deviceActive(false ), clientConnected(false )
132
+ : sizeColor(1920 , 1080 ), sizeIr(512 , 424 ), sizeLowRes(sizeColor.width / 2 , sizeColor.height / 2 ), nh(nh), priv_nh(priv_nh),
133
+ frameColor (0 ), frameIrDepth(0 ), pubFrameColor(0 ), pubFrameIrDepth(0 ), lastColor(0 , 0 ), lastDepth(0 , 0 ), nextColor(false ),
134
+ nextIrDepth(false ), depthShift(0 ), running(false ), deviceActive(false ), clientConnected(false )
128
135
{
129
136
color = cv::Mat::zeros (sizeColor, CV_8UC3);
130
137
ir = cv::Mat::zeros (sizeIr, CV_32F);
@@ -174,6 +181,7 @@ class Kinect2Bridge
174
181
device->close ();
175
182
delete listenerIrDepth;
176
183
delete listenerColor;
184
+ delete registration;
177
185
178
186
for (size_t i = 0 ; i < COUNT; ++i)
179
187
{
@@ -320,6 +328,8 @@ class Kinect2Bridge
320
328
ret = ret && depthRegLowRes->init (cameraMatrixLowRes, sizeLowRes, cameraMatrixIr, sizeIr, distortionIr, rotation, translation, 0 .5f , maxDepth, device);
321
329
ret = ret && depthRegHighRes->init (cameraMatrixColor, sizeColor, cameraMatrixIr, sizeIr, distortionIr, rotation, translation, 0 .5f , maxDepth, device);
322
330
331
+ registration = new libfreenect2::Registration (irParams, colorParams);
332
+
323
333
return ret;
324
334
}
325
335
@@ -386,26 +396,28 @@ class Kinect2Bridge
386
396
if (use_png)
387
397
{
388
398
compression16BitExt = " .png" ;
389
- compression16BitString = sensor_msgs::image_encodings::MONO16 + " ; png compressed" ;
399
+ compression16BitString = sensor_msgs::image_encodings::TYPE_16UC1 + " ; png compressed" ;
390
400
}
391
401
else
392
402
{
393
403
compression16BitExt = " .tif" ;
394
- compression16BitString = sensor_msgs::image_encodings::MONO16 + " ; tiff compressed" ;
404
+ compression16BitString = sensor_msgs::image_encodings::TYPE_16UC1 + " ; tiff compressed" ;
395
405
}
396
406
}
397
407
398
408
void initTopics (const int32_t queueSize, const std::string &base_name)
399
409
{
400
410
std::vector<std::string> topics (COUNT);
401
- topics[IR ] = K2_TOPIC_IR K2_TOPIC_IMAGE_IR;
402
- topics[IR_RECT ] = K2_TOPIC_IR K2_TOPIC_IMAGE_IR K2_TOPIC_IMAGE_RECT;
411
+ topics[IR_SD ] = K2_TOPIC_SD K2_TOPIC_IMAGE_IR;
412
+ topics[IR_SD_RECT ] = K2_TOPIC_SD K2_TOPIC_IMAGE_IR K2_TOPIC_IMAGE_RECT;
403
413
404
- topics[DEPTH ] = K2_TOPIC_IR K2_TOPIC_IMAGE_DEPTH;
405
- topics[DEPTH_RECT ] = K2_TOPIC_IR K2_TOPIC_IMAGE_DEPTH K2_TOPIC_IMAGE_RECT;
414
+ topics[DEPTH_SD ] = K2_TOPIC_SD K2_TOPIC_IMAGE_DEPTH;
415
+ topics[DEPTH_SD_RECT ] = K2_TOPIC_SD K2_TOPIC_IMAGE_DEPTH K2_TOPIC_IMAGE_RECT;
406
416
topics[DEPTH_HD] = K2_TOPIC_HD K2_TOPIC_IMAGE_DEPTH K2_TOPIC_IMAGE_RECT;
407
417
topics[DEPTH_QHD] = K2_TOPIC_QHD K2_TOPIC_IMAGE_DEPTH K2_TOPIC_IMAGE_RECT;
408
418
419
+ topics[COLOR_SD_RECT] = K2_TOPIC_SD K2_TOPIC_IMAGE_COLOR K2_TOPIC_IMAGE_RECT;
420
+
409
421
topics[COLOR_HD] = K2_TOPIC_HD K2_TOPIC_IMAGE_COLOR;
410
422
topics[COLOR_HD_RECT] = K2_TOPIC_HD K2_TOPIC_IMAGE_COLOR K2_TOPIC_IMAGE_RECT;
411
423
topics[COLOR_QHD] = K2_TOPIC_QHD K2_TOPIC_IMAGE_COLOR;
@@ -427,7 +439,7 @@ class Kinect2Bridge
427
439
}
428
440
infoHDPub = nh.advertise <sensor_msgs::CameraInfo>(base_name + K2_TOPIC_HD + K2_TOPIC_INFO, queueSize, cb, cb);
429
441
infoQHDPub = nh.advertise <sensor_msgs::CameraInfo>(base_name + K2_TOPIC_QHD + K2_TOPIC_INFO, queueSize, cb, cb);
430
- infoIRPub = nh.advertise <sensor_msgs::CameraInfo>(base_name + K2_TOPIC_IR + K2_TOPIC_INFO, queueSize, cb, cb);
442
+ infoIRPub = nh.advertise <sensor_msgs::CameraInfo>(base_name + K2_TOPIC_SD + K2_TOPIC_INFO, queueSize, cb, cb);
431
443
}
432
444
433
445
bool initDevice (std::string &sensor)
@@ -480,8 +492,8 @@ class Kinect2Bridge
480
492
std::cout << std::endl << " device serial: " << sensor << std::endl;
481
493
std::cout << " device firmware: " << device->getFirmwareVersion () << std::endl;
482
494
483
- libfreenect2::Freenect2Device::ColorCameraParams colorParams = device->getColorCameraParams ();
484
- libfreenect2::Freenect2Device::IrCameraParams irParams = device->getIrCameraParams ();
495
+ colorParams = device->getColorCameraParams ();
496
+ irParams = device->getIrCameraParams ();
485
497
486
498
device->stop ();
487
499
@@ -555,6 +567,23 @@ class Kinect2Bridge
555
567
cameraMatrixLowRes.at <double >(0 , 2 ) /= 2 ;
556
568
cameraMatrixLowRes.at <double >(1 , 2 ) /= 2 ;
557
569
570
+ colorParams.fx = cameraMatrixColor.at <double >(0 , 0 );
571
+ colorParams.fy = cameraMatrixColor.at <double >(1 , 1 );
572
+ colorParams.cx = cameraMatrixColor.at <double >(0 , 2 );
573
+ colorParams.cy = cameraMatrixColor.at <double >(1 , 2 );
574
+
575
+ irParams.fx = cameraMatrixIr.at <double >(0 , 0 );
576
+ irParams.fy = cameraMatrixIr.at <double >(1 , 1 );
577
+ irParams.cx = cameraMatrixIr.at <double >(0 , 2 );
578
+ irParams.cy = cameraMatrixIr.at <double >(1 , 2 );
579
+
580
+ irParams.k1 = distortionIr.at <double >(0 , 0 );
581
+ irParams.k2 = distortionIr.at <double >(0 , 1 );
582
+ irParams.p1 = distortionIr.at <double >(0 , 2 );
583
+ irParams.p2 = distortionIr.at <double >(0 , 3 );
584
+ irParams.k3 = distortionIr.at <double >(0 , 4 );
585
+
586
+
558
587
const int mapType = CV_16SC2;
559
588
cv::initUndistortRectifyMap (cameraMatrixColor, distortionColor, cv::Mat (), cameraMatrixColor, sizeColor, mapType, map1Color, map2Color);
560
589
cv::initUndistortRectifyMap (cameraMatrixIr, distortionIr, cv::Mat (), cameraMatrixIr, sizeIr, mapType, map1Ir, map2Ir);
@@ -814,7 +843,6 @@ class Kinect2Bridge
814
843
void receiveIrDepth ()
815
844
{
816
845
libfreenect2::FrameMap frames;
817
- libfreenect2::Frame *irFrame, *depthFrame;
818
846
cv::Mat depth, ir;
819
847
std_msgs::Header header;
820
848
std::vector<cv::Mat> images (COUNT);
@@ -830,8 +858,8 @@ class Kinect2Bridge
830
858
831
859
header = createHeader (lastDepth, lastColor);
832
860
833
- irFrame = frames[libfreenect2::Frame::Ir];
834
- depthFrame = frames[libfreenect2::Frame::Depth];
861
+ irFrame = std::shared_ptr<libfreenect2::Frame>( frames[libfreenect2::Frame::Ir]) ;
862
+ depthFrame = std::shared_ptr<libfreenect2::Frame>( frames[libfreenect2::Frame::Depth]) ;
835
863
836
864
ir = cv::Mat (irFrame->height , irFrame->width , CV_32FC1, irFrame->data );
837
865
depth = cv::Mat (depthFrame->height , depthFrame->width , CV_32FC1, depthFrame->data );
@@ -840,9 +868,8 @@ class Kinect2Bridge
840
868
lockIrDepth.unlock ();
841
869
842
870
processIrDepth (ir, depth, images, status);
843
- listenerIrDepth->release (frames);
844
871
845
- publishImages (images, header, status, frame, pubFrameIrDepth, IR , COLOR_HD);
872
+ publishImages (images, header, status, frame, pubFrameIrDepth, IR_SD , COLOR_HD);
846
873
847
874
double elapsed = ros::Time::now ().toSec () - now;
848
875
lockTime.lock ();
@@ -853,7 +880,6 @@ class Kinect2Bridge
853
880
void receiveColor ()
854
881
{
855
882
libfreenect2::FrameMap frames;
856
- libfreenect2::Frame *colorFrame;
857
883
cv::Mat color;
858
884
std_msgs::Header header;
859
885
std::vector<cv::Mat> images (COUNT);
@@ -869,14 +895,14 @@ class Kinect2Bridge
869
895
870
896
header = createHeader (lastColor, lastDepth);
871
897
872
- colorFrame = frames[libfreenect2::Frame::Color];
898
+ colorFrame = std::shared_ptr<libfreenect2::Frame>(frames[libfreenect2::Frame::Color]);
899
+
873
900
color = cv::Mat (colorFrame->height , colorFrame->width , CV_8UC3, colorFrame->data );
874
901
875
902
frame = frameColor++;
876
903
lockColor.unlock ();
877
904
878
905
processColor (color, images, status);
879
- listenerColor->release (frames);
880
906
881
907
publishImages (images, header, status, frame, pubFrameColor, COLOR_HD, COUNT);
882
908
@@ -933,33 +959,50 @@ class Kinect2Bridge
933
959
934
960
void processIrDepth (const cv::Mat &ir, const cv::Mat &depth, std::vector<cv::Mat> &images, const std::vector<Status> &status)
935
961
{
962
+ // COLOR registered to depth
963
+ if (status[COLOR_SD_RECT])
964
+ {
965
+ if (!colorFrame)
966
+ {
967
+ images[COLOR_SD_RECT] = cv::Mat::zeros (sizeIr, CV_8UC3);
968
+ }
969
+ else
970
+ {
971
+ std::shared_ptr<libfreenect2::Frame> tmpColor, tmpDepth;
972
+ tmpColor = colorFrame;
973
+ tmpDepth = depthFrame;
974
+ cv::Mat tmp = cv::Mat::zeros (sizeIr, CV_8UC3);
975
+ registration->apply (tmpColor.get (), tmpDepth.get (), tmp.data );
976
+ cv::flip (tmp, images[COLOR_SD_RECT], 1 );
977
+ }
978
+ }
936
979
937
980
// IR
938
- if (status[IR ] || status[IR_RECT ])
981
+ if (status[IR_SD ] || status[IR_SD_RECT ])
939
982
{
940
- ir.convertTo (images[IR ], CV_16U);
941
- cv::flip (images[IR ], images[IR ], 1 );
983
+ ir.convertTo (images[IR_SD ], CV_16U);
984
+ cv::flip (images[IR_SD ], images[IR_SD ], 1 );
942
985
}
943
- if (status[IR_RECT ])
986
+ if (status[IR_SD_RECT ])
944
987
{
945
- cv::remap (images[IR ], images[IR_RECT ], map1Ir, map2Ir, cv::INTER_AREA);
988
+ cv::remap (images[IR_SD ], images[IR_SD_RECT ], map1Ir, map2Ir, cv::INTER_AREA);
946
989
}
947
990
948
991
// DEPTH
949
992
cv::Mat depthShifted;
950
- if (status[DEPTH ])
993
+ if (status[DEPTH_SD ])
951
994
{
952
- depth.convertTo (images[DEPTH ], CV_16U, 1 );
953
- cv::flip (images[DEPTH ], images[DEPTH ], 1 );
995
+ depth.convertTo (images[DEPTH_SD ], CV_16U, 1 );
996
+ cv::flip (images[DEPTH_SD ], images[DEPTH_SD ], 1 );
954
997
}
955
- if (status[DEPTH_RECT ] || status[DEPTH_QHD] || status[DEPTH_HD])
998
+ if (status[DEPTH_SD_RECT ] || status[DEPTH_QHD] || status[DEPTH_HD])
956
999
{
957
1000
depth.convertTo (depthShifted, CV_16U, 1 , depthShift);
958
1001
cv::flip (depthShifted, depthShifted, 1 );
959
1002
}
960
- if (status[DEPTH_RECT ])
1003
+ if (status[DEPTH_SD_RECT ])
961
1004
{
962
- cv::remap (depthShifted, images[DEPTH_RECT ], map1Ir, map2Ir, cv::INTER_NEAREST);
1005
+ cv::remap (depthShifted, images[DEPTH_SD_RECT ], map1Ir, map2Ir, cv::INTER_NEAREST);
963
1006
}
964
1007
if (status[DEPTH_QHD])
965
1008
{
@@ -1046,6 +1089,15 @@ class Kinect2Bridge
1046
1089
1047
1090
for (size_t i = begin; i < end; ++i)
1048
1091
{
1092
+ if (i < DEPTH_HD || i == COLOR_SD_RECT)
1093
+ {
1094
+ _header.frame_id = baseNameTF + K2_TF_IR_OPT_FRAME;
1095
+ }
1096
+ else
1097
+ {
1098
+ _header.frame_id = baseNameTF + K2_TF_RGB_OPT_FRAME;
1099
+ }
1100
+
1049
1101
switch (status[i])
1050
1102
{
1051
1103
case UNSUBCRIBED:
@@ -1122,14 +1174,15 @@ class Kinect2Bridge
1122
1174
1123
1175
switch (type)
1124
1176
{
1125
- case IR :
1126
- case IR_RECT :
1127
- case DEPTH :
1128
- case DEPTH_RECT :
1177
+ case IR_SD :
1178
+ case IR_SD_RECT :
1179
+ case DEPTH_SD :
1180
+ case DEPTH_SD_RECT :
1129
1181
case DEPTH_HD:
1130
1182
case DEPTH_QHD:
1131
- msgImage.encoding = sensor_msgs::image_encodings::MONO16 ;
1183
+ msgImage.encoding = sensor_msgs::image_encodings::TYPE_16UC1 ;
1132
1184
break ;
1185
+ case COLOR_SD_RECT:
1133
1186
case COLOR_HD:
1134
1187
case COLOR_HD_RECT:
1135
1188
case COLOR_QHD:
@@ -1140,7 +1193,7 @@ class Kinect2Bridge
1140
1193
case MONO_HD_RECT:
1141
1194
case MONO_QHD:
1142
1195
case MONO_QHD_RECT:
1143
- msgImage.encoding = sensor_msgs::image_encodings::MONO8 ;
1196
+ msgImage.encoding = sensor_msgs::image_encodings::TYPE_8UC1 ;
1144
1197
break ;
1145
1198
case COUNT:
1146
1199
return ;
@@ -1161,15 +1214,16 @@ class Kinect2Bridge
1161
1214
1162
1215
switch (type)
1163
1216
{
1164
- case IR :
1165
- case IR_RECT :
1166
- case DEPTH :
1167
- case DEPTH_RECT :
1217
+ case IR_SD :
1218
+ case IR_SD_RECT :
1219
+ case DEPTH_SD :
1220
+ case DEPTH_SD_RECT :
1168
1221
case DEPTH_HD:
1169
1222
case DEPTH_QHD:
1170
1223
msgImage.format = compression16BitString;
1171
1224
cv::imencode (compression16BitExt, image, msgImage.data , compressionParams);
1172
1225
break ;
1226
+ case COLOR_SD_RECT:
1173
1227
case COLOR_HD:
1174
1228
case COLOR_HD_RECT:
1175
1229
case COLOR_QHD:
@@ -1181,7 +1235,7 @@ class Kinect2Bridge
1181
1235
case MONO_HD_RECT:
1182
1236
case MONO_QHD:
1183
1237
case MONO_QHD_RECT:
1184
- msgImage.format = sensor_msgs::image_encodings::MONO8 + " ; jpeg compressed " ;
1238
+ msgImage.format = sensor_msgs::image_encodings::TYPE_8UC1 + " ; jpeg compressed " ;
1185
1239
cv::imencode (" .jpg" , image, msgImage.data , compressionParams);
1186
1240
break ;
1187
1241
case COUNT:
0 commit comments