crosshairs_nodelet.cpp
Go to the documentation of this file.
00001 // *****************************************************************************
00002 //
00003 // Copyright (c) 2017, Southwest Research Institute® (SwRI®)
00004 // All rights reserved.
00005 //
00006 // Redistribution and use in source and binary forms, with or without
00007 // modification, are permitted provided that the following conditions are met:
00008 //     * Redistributions of source code must retain the above copyright
00009 //       notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above copyright
00011 //       notice, this list of conditions and the following disclaimer in the
00012 //       documentation and/or other materials provided with the distribution.
00013 //     * Neither the name of Southwest Research Institute® (SwRI®) nor the
00014 //       names of its contributors may be used to endorse or promote products
00015 //       derived from this software without specific prior written permission.
00016 //
00017 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020 // ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
00021 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00022 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00023 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00024 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00026 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027 //
00028 // *****************************************************************************
00029 
00030 #include <string>
00031 
00032 #include <opencv2/core/core.hpp>
00033 #include <opencv2/highgui/highgui.hpp>
00034 #include <opencv2/imgproc/imgproc.hpp>
00035 
00036 #include <ros/ros.h>
00037 #include <nodelet/nodelet.h>
00038 #include <image_transport/image_transport.h>
00039 #include <sensor_msgs/image_encodings.h>
00040 #include <sensor_msgs/Image.h>
00041 #include <cv_bridge/cv_bridge.h>
00042 
00043 namespace swri_image_util
00044 {
00045   class CrosshairsNodelet : public nodelet::Nodelet
00046   {
00047   public:
00048     CrosshairsNodelet()
00049     {
00050     }
00051 
00052     ~CrosshairsNodelet()
00053     {
00054     }
00055 
00056     void onInit()
00057     {
00058       ros::NodeHandle &node = getNodeHandle();
00059       ros::NodeHandle &priv = getPrivateNodeHandle();
00060 
00061       image_transport::ImageTransport it(node);
00062       image_pub_ = it.advertise("crosshairs_image", 1);
00063       image_sub_ = it.subscribe("image", 1, &CrosshairsNodelet::ImageCallback, this);
00064     }
00065 
00066     void ImageCallback(const sensor_msgs::ImageConstPtr& image)
00067     {
00068       cv_bridge::CvImagePtr cv_image = cv_bridge::toCvCopy(image);
00069       // Get image dimensions
00070       const int h = cv_image->image.rows;
00071       const int w = cv_image->image.cols;
00072       // Define line properties
00073       const cv::Scalar black(0, 0, 0);
00074       const int thickness = 3;
00075       const int line_type = 8;  // 8-connected line
00076       // Draw vertical line
00077       cv::line(cv_image->image, cv::Point(0, w/2), cv::Point(h-1, w/2), black, thickness, line_type);
00078       // Draw horizontal line
00079       cv::line(cv_image->image, cv::Point(h/2, 0), cv::Point(h/2, w-1), black, thickness, line_type);
00080       // Publish image
00081       image_pub_.publish(cv_image->toImageMsg());
00082     }
00083 
00084   private:
00085     image_transport::Subscriber image_sub_;
00086     image_transport::Publisher image_pub_;
00087   };
00088 }
00089 
00090 // Register nodelet plugin
00091 #include <swri_nodelet/class_list_macros.h>
00092 SWRI_NODELET_EXPORT_CLASS(swri_image_util, CrosshairsNodelet)


swri_image_util
Author(s): Kris Kozak
autogenerated on Thu Jun 6 2019 20:34:52