image_annotator.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2008, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the Willow Garage nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
36 
37 #include <ros/ros.h>
38 #include <boost/thread/mutex.hpp>
39 
40 #include <calibration_msgs/CalibrationPattern.h>
41 #include <sensor_msgs/Image.h>
42 #include <cv_bridge/cv_bridge.h>
43 #include <opencv2/imgproc/imgproc.hpp>
46 
47 namespace image_cb_detector
48 {
49 
50 class ImageAnnotator
51 {
52 public:
54 
55  void processPair(const sensor_msgs::ImageConstPtr& image, const calibration_msgs::CalibrationPatternConstPtr& features);
56 private:
59 
60  // Params
61  int marker_size_;
62  double scaling_;
63 };
64 
65 }
66 
67 using namespace image_cb_detector;
68 
70 {
71  image_pub_ = n_.advertise<sensor_msgs::Image>("annotated", 1);
72 
73  ros::NodeHandle local_ns_("~");
74 
75  local_ns_.param("marker_size", marker_size_, 1);
76  local_ns_.param("scaling", scaling_, 1.0);
77  ROS_INFO("[marker_size]: %i", marker_size_);
78  ROS_INFO("[scaling]: %.3f", scaling_);
79 }
80 
81 void ImageAnnotator::processPair(const sensor_msgs::ImageConstPtr& image, const calibration_msgs::CalibrationPatternConstPtr& features)
82 {
83  try {
84  cv_bridge::CvImageConstPtr cv_image = cv_bridge::toCvShare(image, "rgb8");
85 
86  // ***** Resize the image based on scaling parameters in config *****
87  const int scaled_width = (int) (.5 + cv_image->image.cols * scaling_);
88  const int scaled_height = (int) (.5 + cv_image->image.rows * scaling_);
89  cv::Mat cv_image_scaled;
90  cv::resize(cv_image->image, cv_image_scaled,
91  cv::Size(scaled_width, scaled_height), 0, 0, cv::INTER_LINEAR);
92 
93 
94  if (features->success)
95  {
96  cv::Point2i pt0(features->image_points[0].x*scaling_,
97  features->image_points[0].y*scaling_);
98  cv::circle(cv_image_scaled, pt0, marker_size_*2, cv::Scalar(0,0,255), 1) ;
99  for (unsigned int i=0; i<features->image_points.size(); i++)
100  {
101  cv::Point2i pt(features->image_points[i].x*scaling_,
102  features->image_points[i].y*scaling_);
103  cv::circle(cv_image_scaled, pt, marker_size_, cv::Scalar(0,255,0), 1) ;
104  }
105  }
106  else
107  {
108  for (unsigned int i=0; i<features->image_points.size(); i++)
109  {
110  cv::Point2i pt(features->image_points[i].x*scaling_,
111  features->image_points[i].y*scaling_);
112  cv::circle(cv_image_scaled, pt, marker_size_, cv::Scalar(255,0,0), 1) ;
113  }
114  }
115 
116  // Send the annotated image over ROS
117  sensor_msgs::Image result_image = *(cv_bridge::CvImage(cv_image->header, cv_image->encoding, cv_image_scaled).toImageMsg());
118  image_pub_.publish(result_image);
119  } catch(cv_bridge::Exception & e) {
120  ROS_ERROR("cv_bridge exception: %s", e.what());
121  }
122 }
123 
124 int main(int argc, char** argv)
125 {
126  ros::init(argc, argv, "image_annotator");
127 
128  ros::NodeHandle nh;
129 
130  ImageAnnotator annotator;
131 
132  message_filters::Subscriber<sensor_msgs::Image> image_sub(nh, "image", 1);
134  message_filters::TimeSynchronizer<sensor_msgs::Image,
135  calibration_msgs::CalibrationPattern> sync(image_sub, features_sub, 5);
136 
137  sync.registerCallback(boost::bind(&ImageAnnotator::processPair, &annotator, _1, _2));
138 
139  ros::spin();
140  return 0;
141 }
image_cb_detector::ImageAnnotator::image_pub_
ros::Publisher image_pub_
Definition: image_annotator.cpp:121
ros::Publisher
boost::shared_ptr
cv_bridge::CvImage::toImageMsg
sensor_msgs::ImagePtr toImageMsg() const
ros::init
ROSCPP_DECL void init(const M_string &remappings, const std::string &name, uint32_t options=0)
ros.h
image_cb_detector::ImageAnnotator::scaling_
double scaling_
Definition: image_annotator.cpp:126
image_cb_detector::ImageAnnotator::processPair
void processPair(const sensor_msgs::ImageConstPtr &image, const calibration_msgs::CalibrationPatternConstPtr &features)
Definition: image_annotator.cpp:81
time_synchronizer.h
cv_bridge::Exception
ros::Publisher::publish
void publish(const boost::shared_ptr< M > &message) const
ros::NodeHandle::advertise
Publisher advertise(AdvertiseOptions &ops)
message_filters::Subscriber< sensor_msgs::Image >
image_cb_detector::ImageAnnotator::marker_size_
int marker_size_
Definition: image_annotator.cpp:125
image_cb_detector::ImageAnnotator::n_
ros::NodeHandle n_
Definition: image_annotator.cpp:122
subscriber.h
image_cb_detector::ImageAnnotator::ImageAnnotator
ImageAnnotator()
Definition: image_annotator.cpp:69
cv_bridge.h
ROS_ERROR
#define ROS_ERROR(...)
main
int main(int argc, char **argv)
Definition: image_annotator.cpp:124
cv_bridge::CvImage
image_cb_detector::ImageAnnotator
Definition: image_annotator.cpp:82
ros::spin
ROSCPP_DECL void spin()
message_filters::TimeSynchronizer
cv_bridge::toCvShare
CvImageConstPtr toCvShare(const sensor_msgs::Image &source, const boost::shared_ptr< void const > &tracked_object, const std::string &encoding=std::string())
ROS_INFO
#define ROS_INFO(...)
image_cb_detector
Definition: image_cb_detector.h:45
ros::NodeHandle


image_cb_detector
Author(s): Vijay Pradeep, Eitan Marder-Eppstein
autogenerated on Tue Mar 1 2022 23:58:48