00001
00002
00003
00004
00005
00006
00007
00008 #include <iostream>
00009
00010 #include <ros/ros.h>
00011
00012 #include <message_filters/subscriber.h>
00013 #include <message_filters/time_synchronizer.h>
00014 #include <message_filters/sync_policies/approximate_time.h>
00015
00016 #include <image_transport/image_transport.h>
00017 #include <image_transport/subscriber_filter.h>
00018
00019 #include <image_geometry/stereo_camera_model.h>
00020
00021 #include <sensor_msgs/Image.h>
00022 #include <sensor_msgs/image_encodings.h>
00023 #include <cv_bridge/cv_bridge.h>
00024
00025 #include "moving_object_filter.h"
00026
00027 #include <nav_msgs/Odometry.h>
00028
00029
00030 class DynamicObjectFilter: public MovingObjectFilter{
00031 public:
00032 DynamicObjectFilter(int argc, char**argv):
00033 MovingObjectFilter( argc, argv ),
00034 sync_(0),
00035 rate_(1.0),
00036 time_(ros::Time::now())
00037 {
00038 ros::NodeHandle nh ;
00039 ros::NodeHandle pnh("~") ;
00040 int queueSize = 5 ;
00041 ros::NodeHandle rgb_nh(nh, "rgb") ;
00042 ros::NodeHandle depth_nh(nh, "depth") ;
00043 ros::NodeHandle rgb_pnh(pnh, "rgb") ;
00044 ros::NodeHandle depth_pnh(pnh, "depth" ) ;
00045
00046 image_transport::ImageTransport rgb_it(rgb_nh) ;
00047 image_transport::ImageTransport depth_it(depth_nh) ;
00048
00049 image_transport::TransportHints hintsRgb("raw", ros::TransportHints(), rgb_pnh) ;
00050 image_transport::TransportHints hintsDepth("raw", ros::TransportHints(), depth_pnh) ;
00051
00052
00053 image_mono_sub_.subscribe(rgb_it, rgb_nh.resolveName("image"), 1, hintsRgb) ;
00054
00055 image_depth_sub_.subscribe(depth_it, depth_nh.resolveName("image"), 1, hintsDepth) ;
00056
00057 info_sub_.subscribe(rgb_nh, "camera_info", 1) ;
00058 sync_ = new message_filters::Synchronizer<MySyncPolicy>(MySyncPolicy(queueSize), image_mono_sub_, image_depth_sub_, info_sub_) ;
00059 sync_->registerCallback(boost::bind(&DynamicObjectFilter::callback, this, _1, _2, _3)) ;
00060
00061 }
00062 ~DynamicObjectFilter(){
00063 delete sync_ ;
00064 }
00065
00066
00067 void callback( const sensor_msgs::ImageConstPtr& image,
00068 const sensor_msgs::ImageConstPtr& depth,
00069 const sensor_msgs::CameraInfoConstPtr& cameraInfo){
00070
00071
00072 if(!(image->encoding.compare(sensor_msgs::image_encodings::MONO8) ==0 ||
00073 image->encoding.compare(sensor_msgs::image_encodings::MONO16) ==0 ||
00074 image->encoding.compare(sensor_msgs::image_encodings::BGR8) == 0 ||
00075 image->encoding.compare(sensor_msgs::image_encodings::RGB8) == 0) ||
00076 !(depth->encoding.compare(sensor_msgs::image_encodings::TYPE_16UC1)==0 ||
00077 depth->encoding.compare(sensor_msgs::image_encodings::TYPE_32FC1)==0)){
00078 ROS_ERROR("Input type must be image=mono8,mono16,rgb8,bgr8 (mono8 recommended) and image_depth=16UC1");
00079 return;
00080 }else if(depth->encoding.compare(sensor_msgs::image_encodings::TYPE_32FC1)==0){
00081 static bool warned = false;
00082 if(!warned){
00083 ROS_WARN("Input depth type is 32FC1, please use type 16UC1 for depth. The depth images "
00084 "will be processed anyway but with a conversion. This warning is only be printed once...") ;
00085 warned = true ;
00086 }
00087 }
00088
00089
00090 if(rate_>0.0f)
00091 {
00092 std::cout << "time_ = " << time_ << std::endl ;
00093 if(ros::Time::now() - time_ < ros::Duration(1.0f/rate_))
00094 {
00095 std::cout << "Don't process Data" << std::endl ;
00096 return ;
00097 }
00098 }
00099 time_ = ros::Time::now();
00100
00101 if( image->data.size() && depth->data.size() && cameraInfo->K[4] != 0 ){
00102
00103 image_geometry::PinholeCameraModel model ;
00104 model.fromCameraInfo(*cameraInfo) ;
00105 float cx = model.cx() ;
00106 float cy = model.cy() ;
00107 float fx = model.fx() ;
00108 float fy = model.fy() ;
00109
00110
00111 cv_bridge::CvImageConstPtr ptrImage = cv_bridge::toCvShare(image, "bgr8");
00112 cv_bridge::CvImageConstPtr ptrDepth = cv_bridge::toCvShare(depth);
00113
00114 std::cout << "Process the data" << std::endl ;
00115 setFrameId(image->header.frame_id, depth->header.frame_id);
00116 this->processData( ptrImage->image, ptrDepth->image, cx, cy, fx, fy);
00117 }
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140 }
00141
00142 private:
00143
00144 image_transport::SubscriberFilter image_mono_sub_ ;
00145 image_transport::SubscriberFilter image_depth_sub_ ;
00146 message_filters::Subscriber<sensor_msgs::CameraInfo> info_sub_ ;
00147 typedef message_filters::sync_policies::ApproximateTime<sensor_msgs::Image, sensor_msgs::Image, sensor_msgs::CameraInfo> MySyncPolicy;
00148 message_filters::Synchronizer<MySyncPolicy> * sync_;
00149
00150 float rate_ ;
00151 ros::Time time_ ;
00152
00153
00154 };
00155 int main( int argc, char**argv ){
00156 ros::init( argc,argv,"filter_node");
00157
00158 DynamicObjectFilter filter( argc, argv ) ;
00159
00160
00161 ros::spin() ;
00162
00163 return 0 ;
00164 }