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
00018 using namespace std;
00019
00020
00021 sensor_msgs::CameraInfo::ConstPtr cameraInfo_;
00022 cv_bridge::CvImagePtr cv_ptr;
00023 cv::Mat debugImage_;
00024
00025
00026 inline cv::Point3f to3D(cv::Point a) {
00027 cv::Point3f pt;
00028 float constant = 1.00 / cameraInfo_->K[0];
00029 pt.x = (a.x - (cv_ptr->image.cols >> 1)) * cv_ptr->image.at<float> (a)
00030 * constant;
00031 pt.y = (a.y - (cv_ptr->image.rows >> 1)) * cv_ptr->image.at<float> (a)
00032 * constant;
00033
00034 pt.z = cv_ptr->image.at<float> (a);
00035 return pt;
00036 }
00037
00038 inline cv::Point to2D(cv::Point3f pt) {
00039 cv::Point a;
00040 a.x = pt.x/ pt.z * cameraInfo_->K[0] + (cv_ptr->image.cols >> 1);
00041 a.y = pt.y/ pt.z * cameraInfo_->K[0] + (cv_ptr->image.rows >> 1);
00042 return a;
00043 }
00044
00045
00046 void infoCallback(const sensor_msgs::CameraInfo::ConstPtr& msg) {
00047 cameraInfo_ = msg;
00048 cout << "received info" << endl;
00049 }
00050
00051 void imageCallback(const sensor_msgs::Image::ConstPtr& msg) {
00052 cout << "received image" << endl;
00053 ros::Time begin = ros::Time::now();
00054 if (!cameraInfo_)
00055 return;
00056 try {
00057 cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::MONO16);
00058 } catch (cv_bridge::Exception& e) {
00059 ROS_ERROR("cv_bridge exception: %s", e.what());
00060 return;
00061 }
00062
00063
00064 cvtColor(cv_ptr->image * 0.2, debugImage_, CV_GRAY2BGR);
00065
00066 cv::imshow("depth", debugImage_);
00067 cv::waitKey(3);
00068
00069 }
00070
00071
00072 int main(int argc, char** argv) {
00073 ros::init(argc, argv, "stub");
00074
00075 ros::NodeHandle n;
00076
00077 ros::Subscriber sub = n.subscribe("camera/depth/image", 1, imageCallback);
00078 ros::Subscriber sub2 = n.subscribe("camera/depth/camera_info", 1,
00079 infoCallback);
00080
00081 cv::namedWindow("depth");
00082
00083 ros::spin();
00084 }