49 #include <opencv2/highgui/highgui.hpp> 50 #include <opencv2/imgproc/imgproc.hpp> 52 #include <dynamic_reconfigure/server.h> 53 #include "opencv_apps/FindContoursConfig.h" 54 #include "opencv_apps/Contour.h" 55 #include "opencv_apps/ContourArray.h" 56 #include "opencv_apps/ContourArrayStamped.h" 69 typedef opencv_apps::FindContoursConfig
Config;
86 low_threshold_ = config_.canny_low_threshold;
89 const std::string&
frameWithDefault(
const std::string& frame,
const std::string& image_frame)
96 void imageCallbackWithInfo(
const sensor_msgs::ImageConstPtr& msg,
const sensor_msgs::CameraInfoConstPtr& cam_info)
98 doWork(msg, cam_info->header.frame_id);
103 doWork(msg, msg->header.frame_id);
108 need_config_update_ =
true;
111 void doWork(
const sensor_msgs::ImageConstPtr& msg,
const std::string& input_frame_from_msg)
120 opencv_apps::ContourArrayStamped contours_msg;
121 contours_msg.header = msg->header;
127 if (frame.channels() > 1)
129 cv::cvtColor(frame, src_gray, cv::COLOR_RGB2GRAY);
135 cv::GaussianBlur(src_gray, src_gray, cv::Size(3, 3), 0, 0, cv::BORDER_DEFAULT);
140 cv::namedWindow(window_name_, cv::WINDOW_AUTOSIZE);
143 cv::Mat canny_output;
144 int max_thresh = 255;
145 std::vector<std::vector<cv::Point> > contours;
146 std::vector<cv::Vec4i> hierarchy;
150 cv::blur(src_gray, canny_output, cv::Size(3, 3));
153 cv::Canny(canny_output, canny_output, low_threshold_, low_threshold_ * 2, 3);
156 cv::findContours(canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cv::Point(0, 0));
159 cv::Mat drawing = cv::Mat::zeros(canny_output.size(), CV_8UC3);
160 for (
size_t i = 0; i < contours.size(); i++)
162 cv::Scalar color = cv::Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
163 cv::drawContours(drawing, contours, (
int)i, color, 2, 8, hierarchy, 0, cv::Point());
165 opencv_apps::Contour contour_msg;
166 for (
const cv::Point& j : contours[i])
168 opencv_apps::Point2D point_msg;
171 contour_msg.points.push_back(point_msg);
173 contours_msg.contours.push_back(contour_msg);
179 if (need_config_update_)
182 reconfigure_server_->updateConfig(config_);
183 need_config_update_ =
false;
185 cv::createTrackbar(
"Canny thresh:", window_name_, &low_threshold_, max_thresh,
trackbarCallback);
187 cv::imshow(window_name_, drawing);
188 int c = cv::waitKey(1);
192 sensor_msgs::Image::Ptr out_img =
195 msg_pub_.
publish(contours_msg);
197 catch (cv::Exception& e)
199 NODELET_ERROR(
"Image processing error: %s %s %s %i", e.err.c_str(), e.func.c_str(), e.file.c_str(), e.line);
202 prev_stamp_ = msg->header.stamp;
208 if (config_.use_camera_info)
227 pnh_->param(
"queue_size", queue_size_, 3);
228 pnh_->param(
"debug_view", debug_view_,
false);
235 window_name_ =
"Demo code to find contours in an image";
236 low_threshold_ = 100;
238 reconfigure_server_ = boost::make_shared<dynamic_reconfigure::Server<Config> >(*pnh_);
239 dynamic_reconfigure::Server<Config>::CallbackType
f =
241 reconfigure_server_->setCallback(f);
244 msg_pub_ = advertise<opencv_apps::ContourArrayStamped>(*
pnh_,
"contours", 1);
259 ROS_WARN(
"DeprecationWarning: Nodelet find_contours/find_contours is deprecated, " 260 "and renamed to opencv_apps/find_contours.");
CvImageConstPtr toCvShare(const sensor_msgs::ImageConstPtr &source, const std::string &encoding=std::string())
#define NODELET_ERROR(...)
void publish(const boost::shared_ptr< M > &message) const
image_transport::Publisher img_pub_
Nodelet to automatically subscribe/unsubscribe topics according to subscription of advertised topics...
boost::shared_ptr< ReconfigureServer > reconfigure_server_
PLUGINLIB_EXPORT_CLASS(opencv_apps::FindContoursNodelet, nodelet::Nodelet)
Demo code to calculate moments.
void subscribe()
This method is called when publisher is subscribed by other nodes. Set up subscribers in this method...
boost::shared_ptr< ros::NodeHandle > pnh_
Shared pointer to private nodehandle.
void doWork(const sensor_msgs::ImageConstPtr &msg, const std::string &input_frame_from_msg)
bool always_subscribe_
A flag to disable watching mechanism and always subscribe input topics. It can be specified via ~alwa...
virtual void onInitPostProcess()
Post processing of initialization of nodelet. You need to call this method in order to use always_sub...
virtual void onInit()
Initialize nodehandles nh_ and pnh_. Subclass should call this method in its onInit method...
void publish(const sensor_msgs::Image &message) const
virtual void onInit()
Initialize nodehandles nh_ and pnh_. Subclass should call this method in its onInit method...
void imageCallback(const sensor_msgs::ImageConstPtr &msg)
boost::shared_ptr< image_transport::ImageTransport > it_
boost::shared_ptr< ros::NodeHandle > nh_
Shared pointer to nodehandle.
image_transport::CameraSubscriber cam_sub_
opencv_apps::FindContoursConfig Config
image_transport::Publisher advertiseImage(ros::NodeHandle &nh, const std::string &topic, int queue_size)
Advertise an image topic and watch the publisher. Publishers which are created by this method...
static bool need_config_update_
void unsubscribe()
This method is called when publisher is unsubscribed by other nodes. Shut down subscribers in this me...
image_transport::Subscriber img_sub_
const std::string & frameWithDefault(const std::string &frame, const std::string &image_frame)
void reconfigureCallback(Config &new_config, uint32_t level)
virtual void onInit()
Initialize nodehandles nh_ and pnh_. Subclass should call this method in its onInit method...
#define NODELET_DEBUG(...)
sensor_msgs::ImagePtr toImageMsg() const
void imageCallbackWithInfo(const sensor_msgs::ImageConstPtr &msg, const sensor_msgs::CameraInfoConstPtr &cam_info)
dynamic_reconfigure::Server< Config > ReconfigureServer
static void trackbarCallback(int, void *)