filter_node.cpp
Go to the documentation of this file.
00001 /*************************************************************************
00002         > File Name: fliter_node.cpp
00003         > Author: yincanben
00004         > Mail: yincanben@163.com
00005         > Created Time: Thu 05 Feb 2015 09:15:56 PM CST
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 //using namespace std;
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             // subscribe rgb image
00053             image_mono_sub_.subscribe(rgb_it, rgb_nh.resolveName("image"), 1, hintsRgb) ;
00054             // subscribe depth image
00055             image_depth_sub_.subscribe(depth_it, depth_nh.resolveName("image"), 1, hintsDepth) ;
00056             // subscribe camera_info
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         // inspired from RTABMap  rtabmap_ros/src/RGBDOdometryNode.cpp
00067         void callback( const sensor_msgs::ImageConstPtr& image,
00068                     const sensor_msgs::ImageConstPtr& depth,
00069                     const sensor_msgs::CameraInfoConstPtr& cameraInfo){
00070 
00071             //std::cout << "Entering the callback" << std::endl ;
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                 }// for if
00087             }// for else
00088 
00089             //Process data every second
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             //std::cout << "rgb frame_id" << image->header.frame_id << std::endl ;
00101             if( image->data.size() && depth->data.size() && cameraInfo->K[4] != 0 ){
00102                 // obtain the camera's parameters
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                 // convert the ros message to Mat in OpenCV
00111                 cv_bridge::CvImageConstPtr ptrImage = cv_bridge::toCvShare(image, "bgr8");  //mono8
00112                 cv_bridge::CvImageConstPtr ptrDepth = cv_bridge::toCvShare(depth);
00113                 // process the data
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 //            if(rate_ < 10){
00120 //               rate_ ++ ;
00121 //            }else{
00122 //                if( image->data.size() && depth->data.size() && cameraInfo->K[4] != 0 ){
00123 //                    // obtain the camera's parameters
00124 //                    image_geometry::PinholeCameraModel model ;
00125 //                    model.fromCameraInfo(*cameraInfo) ;
00126 //                    float cx = model.cx() ;
00127 //                    float cy = model.cy() ;
00128 //                    float fx = model.fx() ;
00129 //                    float fy = model.fy() ;
00130 
00131 //                    // convert the ros message to Mat in OpenCV
00132 //                    cv_bridge::CvImageConstPtr ptrImage = cv_bridge::toCvShare(image, "bgr8");  //mono8
00133 //                    cv_bridge::CvImageConstPtr ptrDepth = cv_bridge::toCvShare(depth);
00134 //                    // process the data
00135 //                    std::cout << "Process the data" << std::endl ;
00136 //                    this->processData( ptrImage->image, ptrDepth->image, cx, cy, fx, fy);
00137 //               }
00138 //               rate_ = 0 ;
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 }


micros_dynamic_objects_filter
Author(s):
autogenerated on Thu Jun 6 2019 17:55:18