在ROS中使用opencv-打开双目摄像头

需要一个/left/image和/right/image的节点,我这里使用uvc_camera这个节点,下载看前文摄像头标定总结,运行指令:

$ rosrun uvc_camera uvc_stereo_node 

具体的程序代码为:

#include 
#include 
#include 
#include 
#include 
#include 

static const std::string OPENCV_WINDOW1 = "Image window1";
static const std::string OPENCV_WINDOW2 = "Image window2";

class ImageConverter
{
  ros::NodeHandle nh_;
  image_transport::ImageTransport it_; 
  image_transport::Subscriber image_sub_1;
  image_transport::Subscriber image_sub_2; 
  image_transport::Publisher image_pub_1;
  image_transport::Publisher image_pub_2;  

public:
  ImageConverter()
    : it_(nh_)
  {
    // Subscrive to input video feed and publish output video feed
    image_sub_1 = it_.subscribe("/left/image_raw", 1, 
      &ImageConverter::imageCb1, this);
    image_sub_2 = it_.subscribe("/right/image_raw", 1, 
      &ImageConverter::imageCb2, this);
    image_pub_1 = it_.advertise("/image_converter/output_video1", 1);
    image_pub_2 = it_.advertise("/image_converter/output_video2", 1);

    cv::namedWindow(OPENCV_WINDOW1);
    cv::namedWindow(OPENCV_WINDOW2);
  }

  ~ImageConverter()
  {
    cv::destroyWindow(OPENCV_WINDOW1);
    cv::destroyWindow(OPENCV_WINDOW2);
  }

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

    // Draw an example circle on the video stream
    if (cv_ptr->image.rows > 60 && cv_ptr->image.cols > 60)
      cv::circle(cv_ptr->image, cv::Point(50, 50), 10, CV_RGB(255,0,0));

    // Update GUI Window
    cv::imshow(OPENCV_WINDOW1, cv_ptr->image);
    cv::waitKey(3);   

    // Output modified video stream
    image_pub_1.publish(cv_ptr->toImageMsg());
  }

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

    // Draw an example circle on the video stream
    if (cv_ptr->image.rows > 60 && cv_ptr->image.cols > 60)
      cv::circle(cv_ptr->image, cv::Point(50, 50), 10, CV_RGB(255,0,0));

    // Update GUI Window
    cv::imshow(OPENCV_WINDOW2, cv_ptr->image);
    cv::waitKey(3);   

    // Output modified video stream
    image_pub_2.publish(cv_ptr->toImageMsg());
  }
};

int main(int argc, char** argv)
{
  ros::init(argc, argv, "image_converter");
  ImageConverter ic;
  ros::spin();

  return 0;

}

在ROS中使用opencv-打开双目摄像头_第1张图片

你可能感兴趣的:(opencv,ROS)