ros中调用opencv绘图测试

环境安装

代码

#include 
#include 
#include 
#include 
#include 
#include 
#include 
 
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;
}

你可能感兴趣的:(opencv,人工智能,计算机视觉)