11#include " eod_node.h"
2-
2+ #include < cstdint>
3+ #include < sensor_msgs/image_encodings.h>
34
45EOD_ROS::EOD_ROS (ros::NodeHandle nh, ros::NodeHandle nh_p){
56 nh_ = nh;
67 nh_p_ = nh_p;
78
9+ frame_sequence = 0 ;
10+
811 rgb_it_ = new image_transport::ImageTransport (nh_);
912
1013 sub_rgb_.subscribe (*rgb_it_, " camera/image_raw" , 10 );
1114 sub_info_.subscribe (nh_, " camera/info" , 10 );
1215
16+ // get params
1317 nh_p_.param (" subscribe_depth" , subscribe_depth, false );
18+ nh_p_.param (" rate_limit_sec" , rate_limit_sec, 0.1 );
19+
20+ // set up message filters
1421 if ( !subscribe_depth){
1522 ROS_INFO (" Configuring filter on rgb image and info..." );
1623
1724 rgb_sync_.reset ( new RGBSynchronizer (RGBInfoSyncPolicy (10 ), sub_rgb_, sub_info_) );
18- rgb_sync_->registerCallback (boost::bind (&EOD_ROS::rgb_info_cb, this , boost::placeholders::_1, boost::placeholders::_2));
19-
25+ rgb_sync_->registerCallback (boost::bind (&EOD_ROS::rgb_info_cb, this , boost::placeholders::_1, boost::placeholders::_2));
2026 }
2127 else {
2228 ROS_INFO (" Configuring filter on rgbd images and infos..." );
@@ -25,18 +31,76 @@ EOD_ROS::EOD_ROS(ros::NodeHandle nh, ros::NodeHandle nh_p){
2531 sub_depth_info_.subscribe (nh_, " depth/info" , 10 );
2632
2733 rgbd_sync_.reset ( new RGBDSynchronizer (RGBDInfoSyncPolicy (10 ), sub_rgb_, sub_info_, sub_depth_, sub_depth_info_) );
28- rgbd_sync_->registerCallback (boost::bind (&EOD_ROS::rgbd_info_cb, this , boost::placeholders::_1, boost::placeholders::_2, boost::placeholders::_3, boost::placeholders::_4));
29-
34+ rgbd_sync_->registerCallback (boost::bind (&EOD_ROS::rgbd_info_cb, this , boost::placeholders::_1, boost::placeholders::_2, boost::placeholders::_3, boost::placeholders::_4));
3035 }
3136 ROS_INFO (" Configured!" );
3237}
3338
34- void EOD_ROS::rgb_info_cb (const sensor_msgs::ImageConstPtr& image, const sensor_msgs::CameraInfoConstPtr& info){
39+ bool EOD_ROS::check_time (ros::Time stamp){
40+ if ( frame_sequence == 0 )
41+ return true ;
42+ return (stamp - prev_detected_time).toSec () > rate_limit_sec;
43+ }
44+
45+ void EOD_ROS::rgb_info_cb (const sensor_msgs::ImageConstPtr& rgb_image, const sensor_msgs::CameraInfoConstPtr& rgb_info){
3546 ROS_INFO (" Get Image!" );
47+
48+ // CHECK RATE
49+ // if( !check_time(rgb_image->header.stamp) ) {
50+ if ( !check_time (ros::Time::now ()) ) {
51+ ROS_WARN (" Skipped frame" );
52+ return ;
53+ }
54+
55+ cv::Mat rgb;
56+ try {
57+ rgb = cv_bridge::toCvCopy (rgb_image, " bgr8" )->image ;
58+ }
59+ catch (cv_bridge::Exception& e){
60+ ROS_ERROR (" Could not convert from '%s' to 'bgr8'." , rgb_image->encoding .c_str ());
61+ }
62+
63+ detect (rgb, cv::Mat ());
3664}
3765
3866void EOD_ROS::rgbd_info_cb (const sensor_msgs::ImageConstPtr& rgb_image, const sensor_msgs::CameraInfoConstPtr& rgb_info, const sensor_msgs::ImageConstPtr& depth_image, const sensor_msgs::CameraInfoConstPtr& depth_info){
3967 ROS_INFO (" Got RGBD!" );
68+
69+ // CHECK RATE
70+ // if( !check_time(rgb_image->header.stamp) ) {
71+ if ( !check_time (ros::Time::now ()) ) {
72+ ROS_WARN (" Skipped frame" );
73+ return ;
74+ }
75+
76+ cv::Mat rgb;
77+ try {
78+ rgb = cv_bridge::toCvCopy (rgb_image, " bgr8" )->image ;
79+ }
80+ catch (cv_bridge::Exception& e){
81+ ROS_ERROR (" Could not convert from '%s' to 'bgr8'." , rgb_image->encoding .c_str ());
82+ }
83+
84+ cv::Mat depth;
85+ if (depth_image->encoding == sensor_msgs::image_encodings::TYPE_16UC1){
86+ depth = cv_bridge::toCvCopy (depth_image, sensor_msgs::image_encodings::TYPE_16UC1)->image ;
87+ }
88+ else if (depth_image->encoding == sensor_msgs::image_encodings::TYPE_32FC1){
89+ depth = cv_bridge::toCvCopy (depth_image, sensor_msgs::image_encodings::TYPE_32FC1)->image ;
90+ }
91+ else {
92+ ROS_ERROR_THROTTLE (5 , " Depth image has unsupported encoding [%s]" , depth_image->encoding .c_str ());
93+ }
94+
95+ detect (rgb, depth);
96+ }
97+
98+ void EOD_ROS::detect (const cv::Mat& rgb, const cv::Mat& depth){
99+ prev_detected_time = ros::Time::now ();
100+
101+
102+ frame_sequence++;
103+
40104}
41105
42106int main (int argc, char **argv)
0 commit comments