$search
00001 00005 #include "extractor.hpp" 00006 00007 #include <rosbag/bag.h> 00008 #include <rosbag/view.h> 00009 00010 #include <message_filters/subscriber.h> 00011 #include <message_filters/synchronizer.h> 00012 #include <message_filters/sync_policies/approximate_time.h> 00013 00014 #include <opencv/cv.h> 00015 #include <opencv/highgui.h> 00016 00017 #include <cv_bridge/cv_bridge.h> 00018 #include <image_transport/image_transport.h> 00019 00020 #include <boost/foreach.hpp> 00021 #include <time.h> 00022 #include <math.h> 00023 00024 #define SAVEPLY false 00025 #define FILTER_DISTANCE 1 00026 #define SIGMA_P 15 00027 #define SIGMA_R .05 00028 00029 typedef message_filters::sync_policies::ApproximateTime< sensor_msgs::Image, sensor_msgs::Image, sensor_msgs::Image, sensor_msgs::CameraInfo, sensor_msgs::CameraInfo, sensor_msgs::CameraInfo > SyncPolicy; 00030 00032 template <class M> 00033 class BagSubscriber : public message_filters::SimpleFilter<M> 00034 { 00035 public: 00036 void newMessage(const boost::shared_ptr<M const> &msg) 00037 { 00038 signalMessage(msg); 00039 } 00040 }; 00041 00042 extractor::extractor(int skipFrames, int maxFrames): 00043 dataFile(NULL), 00044 timeFile(NULL), 00045 count(0), 00046 filenum(0), 00047 skipFrames(skipFrames), 00048 maxFrames(maxFrames) 00049 { 00050 cout.precision(15); 00051 00052 //synchronizer.registerCallback(boost::bind(&DepthListener::cameraCallback,this, _1, _2)); 00053 } 00054 00055 extractor::~extractor() 00056 { 00057 if(dataFile) 00058 fclose(dataFile); 00059 if(timeFile) 00060 fclose(timeFile); 00061 } 00062 00063 void extractor::extractBagFile(const string &bagFileName, const string &saveDirName) 00064 { 00065 rosbag::Bag bag; 00066 bag.open(bagFileName, rosbag::bagmode::Read); 00067 resultsDir = saveDirName; 00068 00069 char buffer[100]; 00070 sprintf(buffer,"mkdir -p %s/%s",saveDirName.c_str(),"img/"); 00071 system(buffer); 00072 00073 string imgTopic = "/thermalvis/streamer/image_raw"; 00074 string infoTopic = "/thermalvis/streamer/camera_info"; 00075 00076 stringstream imgPath; 00077 char *outputFilename; 00078 outputFilename = (char*) malloc(256); 00079 00080 stringstream timePath; 00081 timePath << resultsDir << "/" << "time.txt"; 00082 00083 timeFile = fopen(timePath.str().c_str(), "w"); 00084 00085 // Image topics to load 00086 std::vector<std::string> topics; 00087 topics.push_back(imgTopic); 00088 topics.push_back(infoTopic); 00089 00090 rosbag::View view(bag, rosbag::TopicQuery(topics)); 00091 00092 uint32_t msg_size = view.size() ; 00093 ROS_INFO("the msg size is %d",msg_size); 00094 00095 // Set up fake subscribers to capture images 00096 //BagSubscriber<sensor_msgs::Image> subRgb, subDepth, subThermal; 00097 //BagSubscriber<sensor_msgs::CameraInfo> subRgbInfo, subDepthInfo, subThermalInfo; 00098 // Use time synchronizer to make sure we get properly synchronized images 00099 //message_filters::Synchronizer<SyncPolicy> sync(SyncPolicy(35), subRgb, subDepth, subThermal, subRgbInfo, subDepthInfo, subThermalInfo); 00100 //boost::bind(&PointExtractor::cameraCallback,this, _1, _2, _3); 00101 //sync.registerCallback(boost::bind(&PointExtractor::cameraCallback,this, _1, _2, _3, _4, _5, _6)); // Steve 00102 00103 int count_img = 0 ; 00104 int count_info = 0 ; 00105 00106 char bagName[256]; 00107 int idx = bagFileName.find_last_of("/\\"); 00108 00109 sprintf(bagName, "%s", (bagFileName.substr(idx+1, bagFileName.length())).c_str()); 00110 00111 fprintf(timeFile,"# images\n"); 00112 fflush(timeFile); 00113 fprintf(timeFile,"# file: '%s'\n", bagName); 00114 fflush(timeFile); 00115 fprintf(timeFile,"# timestamp filename\n"); 00116 fflush(timeFile); 00117 00118 BOOST_FOREACH(rosbag::MessageInstance const m, view) 00119 { 00120 00121 00122 if (m.getTopic() == imgTopic){ 00123 sensor_msgs::Image::ConstPtr img = m.instantiate<sensor_msgs::Image>(); 00124 00125 sprintf(outputFilename, "%s/thermal/frame%06d.%s", resultsDir.c_str(), count, "png"); 00126 00127 ROS_INFO("Image goes to (%s)", outputFilename); 00128 00129 // copy sensor msg Image to opencv 00130 cv_bridge::CvImagePtr cv_ptr_img; 00131 try { 00132 cv_ptr_img = cv_bridge::toCvCopy(img, "bgr8"); 00133 } catch (cv_bridge::Exception& e) { 00134 ROS_ERROR("cv_bridge exception: %s", e.what()); 00135 return; 00136 } 00137 00138 cv::Mat rawThermal, grayThermal, dispThermal; 00139 rawThermal = cv::Mat(cv_ptr_img->image); 00140 00141 if (rawThermal.type() == CV_16UC3) { 00142 cv::cvtColor(rawThermal, grayThermal, CV_RGB2GRAY); 00143 } else { 00144 grayThermal = cv::Mat(rawThermal); 00145 } 00146 cv::imwrite(outputFilename,grayThermal); 00147 //fprintf(thermaltimeFile,"%ld \n",thermalImg->header.stamp.toNSec()); 00148 //fflush(thermaltimeFile); 00149 ++count; 00150 } 00151 00152 00153 if (m.getTopic() == infoTopic){ 00154 sensor_msgs::CameraInfo::ConstPtr info = m.instantiate<sensor_msgs::CameraInfo>(); 00155 00156 //fprintf(thermaltimeFile,"%ld ",thermalInfo->header.stamp.toNSec()); 00157 fprintf(timeFile,"%010d.%06d ",info->header.stamp.sec, info->header.stamp.nsec/1000); 00158 00159 /* 00160 if (thermalInfo->binning_x!=0) { 00161 unsigned long internalimg_time = (((unsigned long) thermalInfo->binning_y) << 32) | thermalInfo->binning_x; 00162 fprintf(thermaltimeFile,"%ld \n",internalimg_time); 00163 } 00164 */ 00165 00166 fprintf(timeFile,"thermal/frame%06d.png\n", count_info); 00167 00168 fflush(timeFile); 00169 00170 ++count_info; 00171 } 00172 00173 00174 } 00175 bag.close(); 00176 } 00177 /* 00178 void PointExtractor::cameraCallback(const sensor_msgs::Image::ConstPtr& rgbImgMsg, 00179 const sensor_msgs::Image::ConstPtr& depthImgMsg, 00180 const sensor_msgs::Image::ConstPtr& thermalImgMsg, 00181 const sensor_msgs::CameraInfo::ConstPtr& rgbInfoMsg, 00182 const sensor_msgs::CameraInfo::ConstPtr& depthInfoMsg, 00183 const sensor_msgs::CameraInfo::ConstPtr& thermalInfoMsg 00184 ) 00185 { 00186 if(count == 0){ 00187 stringstream timePath; 00188 stringstream internaltimePath; 00189 timePath << resultsDir << "time.txt"; 00190 internaltimePath << resultsDir << "internaltime.txt"; 00191 00192 timeFile = fopen(timePath.str().c_str(), "w"); 00193 internaltimeFile = fopen(internaltimePath.str().c_str(), "w"); 00194 } 00195 00196 if(count%(maxFrames*skipFrames) == 0) { 00197 if(dataFile) 00198 fclose(dataFile); 00199 00200 stringstream dataPath; 00201 dataPath << resultsDir << "data/kinectdataD" << filenum << ".bin"; 00202 00203 filenum++; 00204 00205 dataFile = fopen(dataPath.str().c_str(), "w"); 00206 if (!dataFile){ 00207 printf("Unable to open file\n"); 00208 exit(0); 00209 } 00210 else { 00211 startTime = depthImgMsg->header.stamp.toSec(); 00212 if(!SAVEPLY) 00213 fwrite((const void*)&startTime,sizeof(double),1,dataFile); 00214 } 00215 } 00216 if( count%skipFrames == 0) { 00217 fprintf(timeFile,"%ld %ld %ld\n",rgbImgMsg->header.stamp.toNSec(), depthImgMsg->header.stamp.toNSec() , thermalImgMsg->header.stamp.toNSec() ); 00218 00219 if (rgbInfoMsg->binning_x!=0 && depthInfoMsg->binning_x!=0) 00220 { 00221 unsigned long internalimg_time = (((unsigned long) rgbInfoMsg->binning_y) << 32) | rgbInfoMsg->binning_x; 00222 unsigned long internaldepth_time = (((unsigned long) depthInfoMsg->binning_y) << 32) | depthInfoMsg->binning_x; 00223 fprintf(internaltimeFile,"%ld %ld\n",internalimg_time, internaldepth_time); 00224 } 00225 00226 stringstream imgPath; 00227 char *outputFilename; 00228 outputFilename = (char*) malloc(256); 00229 00230 //imgPath << resultsDir << "img/kinectImg%06d" << count << ".jpg"; 00231 sprintf(outputFilename, "%s/img/frame%06d.%s", resultsDir.c_str(), count, "jpg"); 00232 00233 // copy sensor msg Image to opencv 00234 cv_bridge::CvImagePtr cv_ptr; 00235 try { 00236 cv_ptr = cv_bridge::toCvCopy(rgbImgMsg, "bgr8"); 00237 } catch (cv_bridge::Exception& e) { 00238 ROS_ERROR("cv_bridge exception: %s", e.what()); 00239 return; 00240 } 00241 cv::imwrite(outputFilename,cv_ptr->image); 00242 00243 //stringstream thermalPath; 00244 //thermalPath << resultsDir << "thermal/thermalImg" << count << ".png"; 00245 char *outputFilenameThermal; 00246 outputFilenameThermal = (char*) malloc(256); 00247 sprintf(outputFilenameThermal, "%s/thermal/frame%06d.%s", resultsDir.c_str(), count, "png"); 00248 00249 // copy sensor msg Image to opencv 00250 cv_bridge::CvImagePtr cv_ptr_thermal; 00251 try { 00252 cv_ptr_thermal = cv_bridge::toCvCopy(thermalImgMsg, "bgr8"); 00253 } catch (cv_bridge::Exception& e) { 00254 ROS_ERROR("cv_bridge exception: %s", e.what()); 00255 return; 00256 } 00257 00258 cv::Mat rawThermal, grayThermal, dispThermal; 00259 00260 rawThermal = cv::Mat(cv_ptr_thermal->image); 00261 00262 if (rawThermal.type() == CV_16UC3) { 00263 cv::cvtColor(rawThermal, grayThermal, CV_RGB2GRAY); 00264 } else { 00265 grayThermal = cv::Mat(rawThermal); 00266 } 00267 00268 cv::imwrite(outputFilenameThermal,grayThermal); 00269 00270 int time = clock(); 00271 //CameraHandle(depthCamMsg); 00272 getPointCloud(depthImgMsg, rgbImgMsg); 00273 printf("Time depth: %f ms\n" ,((static_cast<float>(clock()-time)/CLOCKS_PER_SEC)*1000)); 00274 //if(SAVEPLY) 00275 // exit(0); 00276 } 00277 00278 ++count; 00279 } 00280 */