环境安装
代码
#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://www.toymoban.com/news/detail-824620.html
文章来源:https://www.toymoban.com/news/detail-824620.html
到了这里,关于ros中调用opencv绘图测试的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!