ros中调用opencv绘图测试

环境安装

代码

cpp 复制代码
#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;
}
相关推荐
Henry-SAP6 分钟前
AI突破重塑未来科技格局
人工智能·云原生·sap·erp
空堂与归1 小时前
图像识别总是准确率低?用卷积神经网络CNN实现高效分类
人工智能·深度学习·分类·cnn
老猿AI洞察2 小时前
阿里云启用巴西数据中心,AI服务出海落子南半球
人工智能·阿里云·云计算
大金SEO2 小时前
GEO启动的正确顺序:先做信源清理,再谈内容扩张
大数据·人工智能
Bruce_Liuxiaowei2 小时前
从基础理论开始学习人工智能(三):盲目搜索——从状态空间图到生成-测试范式
人工智能·学习
qyz_hr2 小时前
拥抱AI,红海云筑牢企业组织与人力数据底层能力
人工智能
Capricorn19883 小时前
个人知识库接入大模型频现“幻觉引用”?排查 RAG 溯源失效问题,解析知芽构建可信第三大脑的底层架构
大数据·论文阅读·人工智能·笔记·架构·论文笔记
LaughingZhu3 小时前
Product Hunt 每日热榜 | 2026-08-27
人工智能·深度学习·神经网络·搜索引擎·百度
时代分流3 小时前
生成式搜索时代的 GEO 技术体系与落地实践:解析矩擎 GEO 5.0 全栈架构
人工智能