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;
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);
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);
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));
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);
188 int c = cv::waitKey(1);
192 sensor_msgs::Image::Ptr out_img =
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);
235 window_name_ =
"Demo code to find contours in an image";
239 dynamic_reconfigure::Server<Config>::CallbackType
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.");
266 #ifdef USE_PLUGINLIB_CLASS_LIST_MACROS_H