fake_stereo_cam.cpp
Go to the documentation of this file.
00001 #include <ros/ros.h>
00002 #include <image_transport/image_transport.h>
00003 #include <cv_bridge/CvBridge.h>
00004 #include <opencv/highgui.h>
00005 #include <fstream>
00006 #include <dirent.h>
00007 #include <fnmatch.h>
00008 #include <cstdio>
00009 
00010 // Names of left and right files in directory (with wildcards)
00011 char *lreg, *rreg, *dreg;
00012 
00013 // Filters for scandir
00014 int getleft(struct dirent const *entry)
00015 {
00016   if (!fnmatch(lreg,entry->d_name,0))
00017     return 1;
00018   return 0;
00019 }
00020 
00021 int getright(struct dirent const *entry)
00022 {
00023   if (!fnmatch(rreg,entry->d_name,0))
00024     return 1;
00025   return 0;
00026 }
00027 
00028 
00029 int getidir(struct dirent const *entry)
00030 {
00031   if (!fnmatch(dreg,entry->d_name,0))
00032     return 1;
00033   return 0;
00034 }
00035 
00036 int main(int argc, char** argv)
00037 {
00038   ros::init(argc, argv, "fake_stereo_cam");
00039 
00040   // Set up publishers
00041   ros::NodeHandle nh;
00042   image_transport::ImageTransport it(nh);
00043   image_transport::CameraPublisher l_pub = it.advertiseCamera("left/image_rect", 1);
00044   image_transport::CameraPublisher r_pub = it.advertiseCamera("right/image_rect", 1);
00045 
00046   // Read in camera info
00047   std::fstream cam_params;
00048   cam_params.open(argv[1], std::fstream::in);
00049   if (!cam_params.is_open()) {
00050     ROS_FATAL("Can't open camera file %s\n", argv[1]);
00051     return -1;
00052   }
00053   double fx, fy, cx, cy, Tx;
00054   cam_params >> fx >> fy >> cx >> cy >> Tx;
00055   sensor_msgs::CameraInfo l_info, r_info;
00056   l_info.header.frame_id = "stereo_optical_frame";
00057   l_info.K[0] = l_info.P[0] = fx;
00058   l_info.K[4] = l_info.P[5] = fy;
00059   l_info.K[2] = l_info.P[2] = cx;
00060   l_info.K[5] = l_info.P[6] = cy;
00061   l_info.R[0] = l_info.R[4] = l_info.R[8] = 1.0;
00062   r_info = l_info;
00063   r_info.P[3] = -Tx * fx;
00064 
00065   // set up directories
00066   struct dirent **lims, **rims, **dirs;
00067   int nlim, nrim, ndirs;
00068   std::string dname = argv[2];
00069   if (!dname.compare(dname.size()-1,1,"/")) // see if slash at end
00070     dname.erase(dname.size()-1);
00071 
00072   std::string dirfn = dname.substr(dname.rfind("/")+1);
00073   std::string tdir = dname.substr(0,dname.rfind("/")+1);
00074   std::cout << "Top directory is " << tdir << std::endl;
00075   std::cout << "Search directory name is " << dirfn << std::endl;
00076   dreg = (char *)dirfn.c_str();
00077 
00078   ndirs = scandir(tdir.c_str(), &dirs, getidir, alphasort);
00079   printf("Found %d directories\n", ndirs);
00080   printf("%s\n",dirs[0]->d_name);
00081 
00082   lreg = argv[3];
00083   rreg = argv[4];
00084 
00085   // loop over directories
00086   ros::Rate rate(2);
00087   for (int dd=0; dd<ndirs && ros::ok(); dd++) {
00088     char dir[2048];
00089     sprintf(dir,"%s%s",tdir.c_str(),dirs[dd]->d_name);
00090     printf("Current directory: %s\n", dir);
00091 
00092     // get left/right image file names, sorted
00093     nlim = scandir(dir,&lims,getleft,alphasort);
00094     printf("Found %d left images\n", nlim);
00095     printf("%s\n",lims[0]->d_name);
00096 
00097     nrim = scandir(dir,&rims,getright,alphasort);
00098     printf("Found %d right images\n", nrim);
00099     printf("%s\n",rims[0]->d_name);
00100 
00101     if (nlim != nrim) {
00102       ROS_FATAL("Number of left/right images does not match: %d vs. %d\n", nlim, nrim);
00103       return -1;
00104     }
00105 
00106     // loop over each stereo pair and publish them
00107     for (int ii=0; ii<nlim && ros::ok(); ii++) {
00108       // Load images
00109       char fn[2048];
00110       sprintf(fn,"%s/%s",dir,lims[ii]->d_name);
00111       cv::Mat left = cv::imread(fn,0);
00112       sprintf(fn,"%s/%s",dir,rims[ii]->d_name);
00113       cv::Mat right = cv::imread(fn,0);
00114 
00115       IplImage left_ipl = left, right_ipl = right;
00116       sensor_msgs::ImagePtr left_msg  = sensor_msgs::CvBridge::cvToImgMsg(&left_ipl);
00117       sensor_msgs::ImagePtr right_msg = sensor_msgs::CvBridge::cvToImgMsg(&right_ipl);
00118 
00119       ros::Time stamp = ros::Time::now();
00120       l_pub.publish(*left_msg, l_info, stamp);
00121       r_pub.publish(*right_msg, r_info, stamp);
00122 
00123       ros::spinOnce();
00124       rate.sleep();
00125     }
00126   }
00127 }


vslam_system
Author(s): Kurt Konolige, Patrick Mihelich, Helen Oleynikova
autogenerated on Thu Jan 2 2014 12:12:31