Ask Your Question
0

problem facing in integration of ROS and OPENCV

asked 2013-05-26 21:49:31 -0500

jasmine gravatar image

updated 2014-01-15 08:24:18 -0500

ahendrix gravatar image

hello! i have picked code from cvbridge tutorial of ROS, followed all the steps then build package. node has been created but when i m trying to run that node after running image stream according to tutorial, nothing appear on screen. here is 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>

namespace enc = sensor_msgs::image_encodings;

static const char WINDOW[] = "Image window";


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

   public:
     ImageConverter()
       : it_(nh_)
     {
       image_pub_ = it_.advertise("out" , 1);
       image_sub_ = it_.subscribe("/topic_imgtransport", 1, &ImageConverter::imageCb, this);

      cv::namedWindow(WINDOW);
     }

     ~ImageConverter()
     {
       cv::destroyWindow(WINDOW);
     }

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


       cv::circle(cv_ptr->image, cv::Point(50, 50), 10, CV_RGB(255,0,0));
       cv::imshow(WINDOW, cv_ptr->image);
       cv::waitKey(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 retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2014-01-15 06:41:15 -0500

Wolf gravatar image

In some cases you need to add cv::startWindowThread() after cv:namedWindow(). In particular this is the case, if you do not call cv::waitKey() without arguments, i. e. not blocking until key pressed, after cv::imshow().

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2013-05-26 21:49:31 -0500

Seen: 1,440 times

Last updated: Jan 15 '14