ros中调用opencv绘图测试

发布时间:2024年01月24日

环境安装

代码

#include <ros/ros.h>
#include <stdio.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>
 
static const char WINDOW[] = "Image window";
static void help()
{
    printf("\nThis program demonstrates converting OpenCV Image to ROS Image messages  \n"
        );
 
}
 
int main(int argc, char** argv)
{
  help();
  ros::init(argc, argv, "image_converter");
 
  // //Reading an image from the file
  // cv::Mat cv_image = cv::imread("/home/hjb/test_ros/src/opencv_test/pictures/1.png");
  // if(cv_image.empty() )
  //   {
  //       ROS_ERROR("Read the picture failed!");
  //       return -1;
  //   }
  cv::Mat cv_image(600, 800, CV_8UC3, cv::Scalar(255, 255, 255)); // 创建一个300x300像素的画布
 
  //Convert OpenCV image to ROS message
  ros::NodeHandle node;
  image_transport::ImageTransport transport(node);
  image_transport::Publisher image_pub; 
  image_pub=transport.advertise("OutImage", 1);
  ros::Time time=ros::Time::now(); 
 
  cv_bridge::CvImage cvi;
  cvi.header.stamp = time;
  cvi.header.frame_id = "image";
  cvi.encoding = "bgr8";
  cvi.image = cv_image;
 
  sensor_msgs::Image im;
  cvi.toImageMsg(im);
  image_pub.publish(im);
  ROS_INFO("Converted Successfully!");
 
  //Show the image
  cv::namedWindow(WINDOW);
  cv::circle(cv_image, cv::Point(0, 0), 100, cv::Scalar(0, 0, 255), -1);
  cv::imshow(WINDOW,cv_image);
  cv::waitKey(0);

 
  ros::spin();
  return 0;
}

文章来源:https://blog.csdn.net/LW_12345/article/details/135825540
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。