ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
1

edge detection

asked 2012-02-03 17:06:14 -0600

nandhini gravatar image

how to write a simple Canny edge detection program in ROS? I have done this in VC++. But i'm not getting the program flow in ROS...

edit retag flag offensive close merge delete

5 Answers

Sort by ยป oldest newest most voted
5

answered 2012-02-03 17:28:15 -0600

ahendrix gravatar image

You'll want to use the image_transport library to subscribe to image messages from a camera, and cv_bridge to convert from ROS image messages into an openCV image type.

Take a look at the cv_bridge C++ tutorial. The Diamondback tutorial should apply to Electric as well.

You may also want to take a look at the generic publisher and subscriber tutorials to get a feel for ROS in general.

edit flag offensive delete link more
4

answered 2012-02-03 22:34:09 -0600

karthik gravatar image

Hi, Program flow in ROS is that, it understands the images as a streams of frames (messages) from the camera. You have to process these frames according to the algorithm that you wanted. So these frames coming into the ROS will not be in Mat format for you to use the canny function of OpenCV directly. Hence as mentioned by Ahendrix you have to convert them to the Mat format using cv_bridge and process the frame. Once you have processed it you may want to convert it to ros msg again and publish it to the other nodes.

Hope it helps!! Karthik

edit flag offensive delete link more
2

answered 2012-02-03 22:50:24 -0600

noonv gravatar image
edit flag offensive delete link more
1

answered 2012-02-05 08:44:29 -0600

bkx gravatar image

First I'd recommend running this OpenCV tutorial outside of ROS.

Once you get that working, I'd try integrating it with this ROS tutorial also mentioned above.

edit flag offensive delete link more
0

answered 2015-03-18 00:56:01 -0600

suhr gravatar image

^I integrated http://wiki.ros.org/cv_bridge/Tutoria... with https://github.com/epsilonorion/ros_t...

Here's the code: include ros/ros.h include image_transport/image_transport.h include cv_bridge/cv_bridge.h include sensor_msgs/image_encodings.h include opencv2/imgproc/imgproc.hpp include opencv2/highgui/highgui.hpp include stdlib.h

class ImageConverter { ros::NodeHandle nh_; image_transport::ImageTransport it_; image_transport::Subscriber image_sub_; image_transport::Publisher image_pub_;

public: ImageConverter() : it_(nh_) { // Subscribe to input video feed and publish output video feed image_sub_ = it_.subscribe("/image_raw", 1, &ImageConverter::imageCb, this); image_pub_ = it_.advertise("/canny_edge/output_video", 1); cv::namedWindow(OPENCV_WINDOW); } ~ImageConverter() { cv::destroyWindow(OPENCV_WINDOW); }

 void imageCb(const sensor_msgs::ImageConstPtr& msg)
 {
   cv_bridge::CvImagePtr cv_ptr;
   try
   {
     cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::RGB8);
   }
   catch (cv_bridge::Exception& e)
   {
     ROS_ERROR("cv_bridge exception: %s", e.what());
    return;
   }

Mat out1; Mat gray_out; Mat canny_out; Mat gray_out1; Mat img1; cv::cvtColor(cv_ptr->image, gray_out, CV_RGB2GRAY); cv::GaussianBlur(gray_out, gray_out, Size(3, 3), 0, 0); cv::Canny(gray_out, canny_out, 50, 125, 3); cv::cvtColor(canny_out, gray_out1, CV_GRAY2RGB); cv::imshow( "CAMERA FEED", cv_ptr->image); cv::imshow( "GRAY CAMERA", gray_out); cv::imshow( "CANNY CAMERA", canny_out); cv::imshow( "CANNY EDGE DETECTION",gray_out1); cvWaitKey(3); image_pub_.publish(cv_ptr->toImageMsg()); } }; int main(int argc, char** argv) { ros::init(argc, argv, "image_converter"); ImageConverter ic; ros::spin(); return 0; }

edit flag offensive delete link more

Question Tools

Stats

Asked: 2012-02-03 17:06:14 -0600

Seen: 3,294 times

Last updated: Feb 05 '12