Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include <string>
00031
00032 #include <opencv2/core/core.hpp>
00033 #include <opencv2/imgproc/imgproc.hpp>
00034
00035 #include <ros/ros.h>
00036 #include <nodelet/nodelet.h>
00037 #include <image_transport/image_transport.h>
00038 #include <sensor_msgs/image_encodings.h>
00039 #include <sensor_msgs/Image.h>
00040 #include <cv_bridge/cv_bridge.h>
00041 #include <swri_math_util/math_util.h>
00042
00043 namespace swri_image_util
00044 {
00045 class ScaleImageNodelet : public nodelet::Nodelet
00046 {
00047 public:
00048 ScaleImageNodelet() :
00049 scale_(1.0)
00050 {
00051 }
00052
00053 ~ScaleImageNodelet()
00054 {
00055 }
00056
00057 void onInit()
00058 {
00059 ros::NodeHandle &node = getNodeHandle();
00060 ros::NodeHandle &priv = getPrivateNodeHandle();
00061
00062 priv.param("scale", scale_, scale_);
00063
00064 image_transport::ImageTransport it(node);
00065 image_pub_ = it.advertise("scaled_image", 1);
00066 image_sub_ = it.subscribe("image", 1, &ScaleImageNodelet::ImageCallback, this);
00067 }
00068
00069 void ImageCallback(const sensor_msgs::ImageConstPtr& image)
00070 {
00071 if (scale_ == 1.0)
00072 {
00073 image_pub_.publish(image);
00074 return;
00075 }
00076
00077 cv_bridge::CvImageConstPtr cv_image = cv_bridge::toCvShare(image);
00078
00079 cv::Size size(
00080 swri_math_util::Round(image->width * scale_),
00081 swri_math_util::Round(image->height * scale_));
00082 cv::Mat scaled;
00083 cv::resize(cv_image->image, scaled, size);
00084
00085 cv_bridge::CvImagePtr cv_scaled = boost::make_shared<cv_bridge::CvImage>();
00086 cv_scaled->image = scaled;
00087 cv_scaled->encoding = cv_image->encoding;
00088 cv_scaled->header = cv_image->header;
00089
00090 image_pub_.publish(cv_scaled->toImageMsg());
00091 }
00092
00093 private:
00094 double scale_;
00095
00096 image_transport::Subscriber image_sub_;
00097 image_transport::Publisher image_pub_;
00098 };
00099 }
00100
00101
00102 #include <pluginlib/class_list_macros.h>
00103 PLUGINLIB_DECLARE_CLASS(
00104 swri_image_util,
00105 scale_image,
00106 swri_image_util::ScaleImageNodelet,
00107 nodelet::Nodelet)