Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008 #include <ros/ros.h>
00009 #include <sensor_msgs/CameraInfo.h>
00010 #include <sensor_msgs/Image.h>
00011 #include <cv_bridge/cv_bridge.h>
00012 #include <sensor_msgs/image_encodings.h>
00013 #include <opencv2/imgproc/imgproc.hpp>
00014 #include <opencv2/highgui/highgui.hpp>
00015 #include <tf/transform_datatypes.h>
00016 #include <articulation_msgs/TrackMsg.h>
00017 #include <fstream>
00018
00019 using namespace std;
00020
00021 bool rgb=false;
00022 string fname;
00023 sensor_msgs::CameraInfo::ConstPtr cameraInfo_;
00024 cv_bridge::CvImagePtr cv_ptr;
00025 cv::Mat debugImage_;
00026
00027
00028 inline cv::Point3f to3D(cv::Point a) {
00029 cv::Point3f pt;
00030 float constant = 1.00 / cameraInfo_->K[0];
00031 pt.x = (a.x - (cv_ptr->image.cols >> 1)) * cv_ptr->image.at<float> (a)
00032 * constant;
00033 pt.y = (a.y - (cv_ptr->image.rows >> 1)) * cv_ptr->image.at<float> (a)
00034 * constant;
00035
00036 pt.z = cv_ptr->image.at<float> (a);
00037 return pt;
00038 }
00039
00040 inline cv::Point to2D(cv::Point3f pt) {
00041 cv::Point a;
00042 a.x = pt.x/ pt.z * cameraInfo_->K[0] + (cv_ptr->image.cols >> 1);
00043 a.y = pt.y/ pt.z * cameraInfo_->K[0] + (cv_ptr->image.rows >> 1);
00044 return a;
00045 }
00046
00047
00048 void infoCallback(const sensor_msgs::CameraInfo::ConstPtr& msg) {
00049 cameraInfo_ = msg;
00050 cout << "received info" << endl;
00051 }
00052
00053 void imageCallback(const sensor_msgs::Image::ConstPtr& msg) {
00054 cout << "received image" << endl;
00055 ros::Time begin = ros::Time::now();
00056 if (!cameraInfo_)
00057 return;
00058 try {
00059 cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::MONO16);
00060 } catch (cv_bridge::Exception& e) {
00061 ROS_ERROR("cv_bridge exception: %s", e.what());
00062 return;
00063 }
00064
00065
00066 cvtColor(cv_ptr->image * 0.2, debugImage_, CV_GRAY2BGR);
00067
00068 cv::imshow("depth", debugImage_);
00069
00070 fstream stream((fname + ".txt").c_str(),ios::out);
00071 stream <<"# u v x y z"<<endl;
00072 for(int y=0;y<cv_ptr->image.rows;y++) {
00073 for(int x=0;x<cv_ptr->image.cols;x++) {
00074 cv::Point3f p = to3D(cv::Point(x,y));
00075 if(isfinite(p.x))
00076 stream <<x<<"\t"<<y<<"\t"<<p.x<<"\t"<<p.y<<"\t"<<p.z<<endl;
00077 }
00078 }
00079 stream.close();
00080 cv::imwrite(fname + "-depth.png", debugImage_*256);
00081
00082 fstream stream2((fname + "-depth.txt").c_str(),ios::out);
00083 stream <<"# per pixel depth in meter, cols x rows ="<<cv_ptr->image.cols<<"x"<<cv_ptr->image.rows<<endl;
00084 for(int y=0;y<cv_ptr->image.rows;y++) {
00085 for(int x=0;x<cv_ptr->image.cols;x++) {
00086 cv::Point p = cv::Point(x,y);
00087 stream2 << cv_ptr->image.at<float> (p)<<" ";
00088 }
00089 stream2 << endl;
00090 }
00091 stream2.close();
00092
00093 if(rgb)
00094 exit(0);
00095
00096 }
00097
00098 void imageRGBCallback(const sensor_msgs::Image::ConstPtr& msg) {
00099 cout << "received RGB image" << endl;
00100 cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8);
00101 cv::cvtColor(cv_ptr->image,cv_ptr->image,CV_RGB2BGR);
00102 cv::imwrite(fname+"-color.png",cv_ptr->image);
00103
00104 fstream stream((fname + "-color.txt").c_str(),ios::out);
00105 stream <<"# pixel-wise (r g b), cols x rows ="<<cv_ptr->image.cols<<"x"<<cv_ptr->image.rows<<endl;
00106 for(int y=0;y<cv_ptr->image.rows;y++) {
00107 for(int x=0;x<cv_ptr->image.cols;x++) {
00108 cv::Point p = cv::Point(x,y);
00109 stream << (int)cv_ptr->image.at<cv::Vec3b>(p)[2] << " ";
00110 stream << (int)cv_ptr->image.at<cv::Vec3b>(p)[1] << " ";
00111 stream << (int)cv_ptr->image.at<cv::Vec3b>(p)[0] << " ";
00112 }
00113 stream << endl;
00114 }
00115 stream.close();
00116
00117 rgb=true;
00118 }
00119
00120 int main(int argc, char** argv) {
00121 ros::init(argc, argv, "filewriter");
00122
00123 if(argc != 2) {
00124 cout <<"wrong number of arguments. Usage: filewriter <prefix>"<<endl;
00125 exit(0);
00126 }
00127 fname = string("kinect-")+argv[1];
00128
00129 ros::NodeHandle n;
00130
00131 ros::Subscriber sub = n.subscribe("camera/depth/image", 1, imageCallback);
00132 ros::Subscriber sub3 = n.subscribe("camera/rgb/image_color", 1, imageRGBCallback);
00133 ros::Subscriber sub2 = n.subscribe("camera/depth/camera_info", 1,
00134 infoCallback);
00135
00136 cv::namedWindow("depth");
00137
00138 ros::spin();
00139 }