visualize_head.cpp
Go to the documentation of this file.
00001 #include <hrl_phri_2011/ellipsoid_space.h>
00002 #include <ros/ros.h>
00003 //#include <hrl_phri_2011/pc_utils.h>
00004 #include <cv_bridge/cv_bridge.h>
00005 #include <opencv/cv.h>
00006 #include <opencv2/imgproc/imgproc.hpp>
00007 #include <opencv2/highgui/highgui.hpp>
00008 #include <image_transport/image_transport.h>
00009 #include <sensor_msgs/image_encodings.h>
00010 #include <sensor_msgs/PointCloud2.h>
00011 #include <pcl_ros/point_cloud.h>
00012 #include <pcl/point_cloud.h>
00013 #include <pcl/point_types.h>
00014 #include <rosbag/bag.h>
00015 #include <rosbag/view.h>
00016 #include <rosbag/message_instance.h>
00017 #include <Eigen/Eigen>
00018 
00019 typedef pcl::PointXYZRGB PRGB;
00020 typedef pcl::PointCloud<PRGB> PCRGB;
00021 
00022 using namespace std;
00023 
00024 using namespace Eigen;
00025 namespace enc = sensor_msgs::image_encodings;
00026 
00027 void pubLoop(PCRGB &pc, const std::string& topic) {
00028     ros::NodeHandle nh;
00029     ros::Publisher pub_pc = nh.advertise<sensor_msgs::PointCloud2>(topic, 1);
00030     ros::Rate r(1);
00031     sensor_msgs::PointCloud2 pc_msg;
00032     pcl::toROSMsg(pc, pc_msg);
00033     while(ros::ok()) {
00034         pc_msg.header.stamp = ros::Time::now();
00035         pub_pc.publish(pc_msg);
00036         r.sleep();
00037     }
00038 }
00039 
00040 void transformPC(const PCRGB &in_pc, PCRGB &out_pc, 
00041                  const Eigen::Affine3d& transform) {
00042     MatrixXd pt_mat = MatrixXd::Constant(4, in_pc.points.size(), 1.0);
00043     uint32_t i = 0;
00044     BOOST_FOREACH(PRGB const pt, in_pc.points) {
00045         pt_mat(0, i) = pt.x; pt_mat(1, i) = pt.y; pt_mat(2, i) = pt.z; 
00046         i++;
00047     }
00048     MatrixXd trans_pt_mat = transform.matrix() * pt_mat;
00049     for(i=0;i<in_pc.points.size();i++) {
00050         PRGB pt;
00051         pt.x = trans_pt_mat(0, i); pt.y = trans_pt_mat(1, i); pt.z = trans_pt_mat(2, i); 
00052         pt.rgb = in_pc.points[i].rgb;
00053         out_pc.points.push_back(pt);
00054     }
00055 }
00056 
00057 
00058 void imgPubLoop(cv::Mat& img_mat) {
00059     ros::NodeHandle nh;
00060     image_transport::ImageTransport img_trans(nh);
00061     image_transport::Publisher img_pub = img_trans.advertise("/camera/img_pub", 1);
00062     cv_bridge::CvImage cvb_img;
00063     cvb_img.image = img_mat;
00064     cvb_img.header.frame_id = "/base_link";
00065     cvb_img.encoding = enc::RGB8;
00066     ros::Rate r(1);
00067     while(ros::ok()) {
00068         cvb_img.header.stamp = ros::Time::now();
00069         img_pub.publish(cvb_img.toImageMsg());
00070         ros::spinOnce();
00071         r.sleep();
00072     }
00073 }
00074 
00075 void visualize(PCRGB::Ptr& pc_head) {
00076     Ellipsoid e(0.05, 0.061);
00077     double lat, lon, height, mapx, mapy;
00078     int i=0;
00079     cv::Mat img(1000, 1000, CV_8UC3);//, cv::Vec3b(0xff, 0xff, 0xff));
00080     double minmapx = 1000, maxmapx = -1000, minmapy = 1000, maxmapy = -1000;
00081     double size = 200, offx = 0, offy = 0;
00082     BOOST_FOREACH(PRGB const pt, pc_head->points) {
00083         e.cartToEllipsoidal(pt.x, pt.y, pt.z, lat, lon, height);
00084         e.mollweideProjection(lat, lon, mapx, mapy);
00085         minmapx = min(lon, minmapx);
00086         maxmapx = max(lon, maxmapx);
00087         minmapy = min(lat, minmapy);
00088         maxmapy = max(lat, maxmapy);
00089         uint8_t r = ((uint8_t*) &pt.rgb)[2];
00090         uint8_t g = ((uint8_t*) &pt.rgb)[1];
00091         uint8_t b = ((uint8_t*) &pt.rgb)[0];
00092         int pixx = mapx*3400 + 500;
00093         int pixy = mapy*3400 + 500;
00094         //int pixx = mapx*4000 + 1000;
00095         //int pixy = mapy*3000 + 700;
00096         if(i++ % 100 == 0) {
00097             //printf("(%f, %f, %f) ", mapx, mapy, height);
00098             //printf("(%f, %f, %f) ", lat, lon, height);
00099             printf("[pt (%f, %f, %f) ;", pt.x, pt.y, pt.z);
00100             printf("ell (%f, %f, %f) ;", lat, lon, height);
00101             printf("pix (%d, %d)] ", pixx, pixy);
00102         }
00103         if(pixx < 0 || pixx >= 1000 || pixy < 0 || pixy >= 1000)
00104             continue;
00105         img.at<cv::Vec3b>(pixx, pixy)[0] = b;
00106         img.at<cv::Vec3b>(pixx, pixy)[1] = g;
00107         img.at<cv::Vec3b>(pixx, pixy)[2] = r;
00108     }
00109     printf("\nMins, max %f %f %f %f\n", minmapx, minmapy, maxmapx, maxmapy);
00110     IplImage img_ipl = img;
00111     cvSaveImage("test.png", &img_ipl);
00112     imgPubLoop(img);
00113 }
00114 
00115 int main(int argc, char **argv)
00116 {
00117     ros::init(argc, argv, "pub_head");
00118 
00119     // Load bag
00120     rosbag::Bag bag;
00121     bag.open(std::string(argv[1]), rosbag::bagmode::Read);
00122     rosbag::View view(bag, rosbag::TopicQuery("/stitched_head"));
00123 
00124     PCRGB::Ptr pc_head(new PCRGB());
00125     BOOST_FOREACH(rosbag::MessageInstance const m, view) {
00126         sensor_msgs::PointCloud2::Ptr pc2 = m.instantiate<sensor_msgs::PointCloud2>();
00127         pcl::fromROSMsg(*pc2, *pc_head);
00128         break;
00129     }
00130     PCRGB::Ptr pc_head_tf(new PCRGB());
00131     Eigen::Affine3d aff_tf(Eigen::Quaternion<double>(0.00163   , 0.25178997, 0.18013405,  0.95086849));
00132     aff_tf = (Translation3d(0.03, 0.0, 0.03)) * aff_tf;
00133     aff_tf = Quaterniond(0, 0, -1.,  0) * aff_tf;
00134     printf("\n\n %f %f %f %f",aff_tf(0,0), aff_tf(0,1), aff_tf(0,2),  aff_tf(0,3));
00135     transformPC(*pc_head, *pc_head_tf, aff_tf);
00136     pc_head_tf->header.frame_id = "/base_link";
00137     pubLoop(*pc_head_tf, "/tf_head");
00138     visualize(pc_head_tf);
00139     return 0;
00140 }


hrl_phri_2011
Author(s): Kelsey Hawkins
autogenerated on Wed Nov 27 2013 12:22:40