00001 #include <iostream>
00002 #include <cstdlib>
00003 #include <cstdio>
00004 #include <vector>
00005 #include "pyra/tpimageutil.h"
00006
00007 #include "fgbgsegment.h"
00008 #include "timercpu.h"
00009
00010
00011 #include <ros/ros.h>
00012 #include <sensor_msgs/Image.h>
00013 #include <stereo_msgs/DisparityImage.h>
00014
00021
00022 class ActiveRealTimeSegmenter
00023 {
00024 public:
00026 ActiveRealTimeSegmenter():
00027 nh_("")
00028 {
00029 }
00030
00032 ~ ActiveRealTimeSegmenter()
00033 {
00034 }
00035
00037
00040 void execute( int loops );
00041
00042 private:
00043
00045
00050 bool assembleSensorData(sensor_msgs::Image::ConstPtr &recent_image,
00051 stereo_msgs::DisparityImage::ConstPtr &recent_disparity_image,
00052 ros::Duration time_out);
00053
00055 ros::NodeHandle nh_;
00056 };
00057
00058 bool ActiveRealTimeSegmenter::assembleSensorData(sensor_msgs::Image::ConstPtr &recent_image,
00059 stereo_msgs::DisparityImage::ConstPtr &recent_disparity_image,
00060 ros::Duration time_out)
00061 {
00062 ROS_INFO("Segmentation through User Interaction: waiting for messages...");
00063 std::string image_topic("/narrow_stereo/left/image_rect");
00064 std::string disparity_image_topic("/narrow_stereo_textured/disparity");
00065
00066 ros::Time start_time = ros::Time::now();
00067 while (!recent_image || !recent_disparity_image )
00068 {
00069 if (!recent_image)
00070 {
00071 ROS_INFO_STREAM(" Waiting for message on topic " << image_topic);
00072 recent_image = ros::topic::waitForMessage<sensor_msgs::Image>(image_topic, nh_, ros::Duration(0.5));
00073 }
00074 if (!recent_disparity_image)
00075 {
00076 ROS_INFO_STREAM(" Waiting for message on topic " << disparity_image_topic);
00077 recent_disparity_image = ros::topic::waitForMessage<stereo_msgs::DisparityImage>
00078 (disparity_image_topic, nh_, ros::Duration(0.5));
00079 }
00080
00081 ros::Time current_time = ros::Time::now();
00082 if (time_out >= ros::Duration(0) && current_time - start_time >= time_out)
00083 {
00084 ROS_INFO("Timed out");
00085 return false;
00086 }
00087 }
00088 return true;
00089 }
00090
00091 void ActiveRealTimeSegmenter::execute( int loops )
00092 {
00093
00094 int fstidx = 0;
00095 int lstidx = loops;
00096
00097
00098
00099 ros::NodeHandle nh_;
00100
00101
00102 sensor_msgs::Image::ConstPtr ros_image;
00103 stereo_msgs::DisparityImage::ConstPtr disparity_image;
00104
00105 if(assembleSensorData(ros_image, disparity_image,ros::Duration(5.0)))
00106 ROS_INFO("All required messages received");
00107 else
00108 ROS_INFO("Could not gather all required messages.");
00109
00110 int w = ros_image->width;
00111 int h = ros_image->height;
00112
00113 ROS_ASSERT(w==disparity_image->image.width && h==disparity_image->image.height);
00114
00115 int drange = (int)(disparity_image->max_disparity-disparity_image->min_disparity);
00116 std::cout << "Disparity Range " << drange << std::endl;
00117
00118 FgBgSegment fgbgsegment(w, h, drange, 50.0);
00119 #ifdef USE_CUDA
00120 fgbgsegment.UseGPU(true);
00121 #else
00122 fgbgsegment.UseGPU(false);
00123 #endif //USE_CUDA
00124 Image<uint8_t> image(3*w, h);
00125 Image<float> disparities(w, h);
00126
00127
00128
00129
00130
00131
00132 image.SetDataAlign(*ros_image, 3*w, h, true);
00133 disparities.SetDataAlign(disparity_image->image, w, h);
00134
00135 image.StoreRGB("CurrentImage.pgm");
00136 disparities.Store("CurrentDisparityImage.pgm");
00137
00138 for (int i=fstidx;i<=lstidx;i++) {
00139 char name[80];
00140
00141 TimerCPU timer(2800);
00142 int n_iter = 5;
00143 fgbgsegment.Execute(image, disparities, (i==fstidx), n_iter);
00144 if (i==fstidx) {
00145
00146
00147 fgbgsegment.SetNewForeground(400, 100, disparities, drange);
00148
00149 }
00150 std::cout << "Loop time: " << timer.read() << " ms" << std::endl;
00151 Image<uint8_t> segm(w, h);
00152 fgbgsegment.MakeSegmentImage(segm);
00153 sprintf(name, "segm%04dx.pgm", i);
00154 segm.Store(name, true, false);
00155 Image<uint8_t> border(3*w, h);
00156 border = image;
00157 fgbgsegment.MakeBorderImage(border);
00158 sprintf(name, "segm%04dx.ppm", i);
00159 border.StoreRGB(name);
00160 Image<uint8_t> mask(w, h);
00161 fgbgsegment.MakeMaskImage(mask, 255, 1);
00162 sprintf(name, "mask%04dx.pgm", i);
00163 mask.Store(name);
00164
00165 }
00166
00167 }
00168
00169 int main(int argc, char **argv)
00170 {
00171 if (argc<2) {
00172 std::cerr << "Usage: " << argv[0] << " <loops>" << std::endl;
00173 std::cout << " Ex:" << argv[0] << " 10" << std::endl;
00174 return 1;
00175 }
00176
00177 int loops = atoi(argv[1]);
00178
00179 ros::init(argc, argv, "active_realtime_segmentation_node");
00180 ActiveRealTimeSegmenter node;
00181 node.execute(loops);
00182
00183 return 0;
00184 }