split_ladybug_nodelet.cpp
Go to the documentation of this file.
00001 /*
00002  *  Copyright (C) 2012, Laboratorio de Robotica Movel - ICMC/USP
00003  *  Rafael Luiz Klaser <rlklaser@gmail.com>
00004  *  http://lrm.icmc.usp.br
00005  *
00006  *  Apoio FAPESP: 2012/04555-4
00007  *
00008  *  This program is free software: you can redistribute it and/or modify
00009  *  it under the terms of the GNU General Public License as published by
00010  *  the Free Software Foundation, either version 3 of the License, or
00011  *  (at your option) any later version.
00012  *
00013  *  This program is distributed in the hope that it will be useful,
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  *  GNU General Public License for more details.
00017  *
00018  *  You should have received a copy of the GNU General Public License
00019  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00020  */
00021 
00030 #include <string.h>
00031 #include <ros/ros.h>
00032 #include <nodelet/nodelet.h>
00033 #include <sensor_msgs/Image.h>
00034 #include <image_transport/image_transport.h>
00035 #include <cv_bridge/cv_bridge.h>
00036 
00037 #include <boost/make_shared.hpp>
00038 
00039 using namespace cv_bridge;
00040 using namespace cv;
00041 
00042 #define LB_WIDTH        512
00043 #define LB_HEIGHT       384
00044 #define NUM_IMGS    6
00045 
00046 #include <lrm_camera_drivers/split_node.h>
00047 
00048 namespace lrm_camera_drivers {
00049 
00050 class SplitLadybugNodelet: public nodelet::Nodelet {
00051 
00052         image_transport::Subscriber sub_;
00053         boost::shared_ptr<image_transport::ImageTransport> it_;
00054 
00055         image_transport::Publisher pub[NUM_IMGS];
00056         //int seq = 0;
00057 
00058         virtual void onInit();
00059         //void connectCb();
00060         void imageCb(const sensor_msgs::ImageConstPtr& raw_msg);
00061 };
00062 
00063 void SplitLadybugNodelet::onInit() {
00064 
00065         ros::NodeHandle &nh = getNodeHandle();
00066         ros::NodeHandle &nh_priv = getPrivateNodeHandle();
00067 
00068         it_.reset(new image_transport::ImageTransport(nh));
00069 
00070         image_transport::TransportHints hints("raw", ros::TransportHints(), nh);
00071         sub_ = it_->subscribe("camera/image_raw", 1, &SplitLadybugNodelet::imageCb, this, hints);
00072 
00073         pub[0] = it_->advertise("/camera/image_color/0", 1);
00074         pub[1] = it_->advertise("/camera/image_color/1", 1);
00075         pub[2] = it_->advertise("/camera/image_color/2", 1);
00076         pub[3] = it_->advertise("/camera/image_color/3", 1);
00077         pub[4] = it_->advertise("/camera/image_color/4", 1);
00078         pub[5] = it_->advertise("/camera/image_color/5", 1);
00079 
00080 }
00081 
00082 void SplitLadybugNodelet::imageCb(const sensor_msgs::ImageConstPtr& msg) {
00083         bool have_subsc = false;
00084         for (int i = 0; i < NUM_IMGS; i++) {
00085                 if (pub[i].getNumSubscribers() > 0) {
00086                         have_subsc = true;
00087                         break;
00088                 }
00089         }
00090         if (have_subsc) {
00091                 try {
00092                         cv_bridge::CvImageConstPtr cv_ptr;
00093                         cv_ptr = toCvShare(msg, "mono8");
00094                         int sz = 0;
00095 
00096                         sensor_msgs::Image omsg;
00097                         omsg.header.frame_id = "/ladybug";
00098                         omsg.header.seq = msg->header.seq;
00099                         omsg.header.stamp = msg->header.stamp;
00100 
00101                         for (int i = 0; i < NUM_IMGS; i++) {
00102 
00103                                 const cv::Mat_<uint8_t> img_0(LB_HEIGHT, LB_WIDTH, const_cast<uint8_t*>(&msg->data[sz]), msg->step);
00104                                 sz += (LB_WIDTH * LB_HEIGHT);
00105                                 const cv::Mat_<uint8_t> img_1(LB_HEIGHT, LB_WIDTH, const_cast<uint8_t*>(&msg->data[sz]), msg->step);
00106                                 sz += (LB_WIDTH * LB_HEIGHT);
00107                                 const cv::Mat_<uint8_t> img_2(LB_HEIGHT, LB_WIDTH, const_cast<uint8_t*>(&msg->data[sz]), msg->step);
00108                                 sz += (LB_WIDTH * LB_HEIGHT);
00109                                 const cv::Mat_<uint8_t> img_3(LB_HEIGHT, LB_WIDTH, const_cast<uint8_t*>(&msg->data[sz]), msg->step);
00110                                 sz += (LB_WIDTH * LB_HEIGHT);
00111 
00112                                 if (pub[i].getNumSubscribers() > 0) {
00113                                         omsg.width = LB_HEIGHT;
00114                                         omsg.height = LB_WIDTH;
00115                                         omsg.encoding = "rgb8";
00116                                         omsg.is_bigendian = false;
00117                                         omsg.step = omsg.width * 3;
00118                                         omsg.data.resize(LB_WIDTH * LB_HEIGHT * 3);
00119                                         CvImagePtr cp = toCvCopy(omsg, "rgb8");
00120                                         Mat src[] = { img_3, img_1, img_0 };
00121                                         cv::merge(src, 3, cp->image);
00122                                         pub[i].publish(cp->toImageMsg());
00123                                 }
00124                         }
00125                 } catch (cv_bridge::Exception& e) {
00126                         //ROS_ERROR("error:" << e);
00127                 }
00128         }
00129 }
00130 
00131 };
00132 
00133 #include <pluginlib/class_list_macros.h>
00134 PLUGINLIB_EXPORT_CLASS(lrm_camera_drivers::SplitLadybugNodelet, nodelet::Nodelet)


lrm_camera_drivers
Author(s): Rafael Luiz Klaser
autogenerated on Mon Oct 6 2014 01:52:55