ROS发布压缩图像的代码
时间: 2023-05-17 12:04:35 浏览: 132
以下是一个发布压缩图像的ROS节点的示例代码:
```
#include <ros/ros.h>
#include <sensor_msgs/Image.h>
#include <sensor_msgs/CompressedImage.h>
#include <image_transport/image_transport.h>
#include <cv_bridge/cv_bridge.h>
#include <opencv2/opencv.hpp>
int main(int argc, char** argv)
{
ros::init(argc, argv, "image_publisher");
ros::NodeHandle nh;
image_transport::ImageTransport it(nh);
image_transport::Publisher pub = it.advertise("/compressed_image", 1);
cv::VideoCapture cap(0);
if(!cap.isOpened())
{
ROS_ERROR("Failed to open camera.");
return -1;
}
ros::Rate loop_rate(30);
while(ros::ok())
{
cv::Mat frame;
cap >> frame;
if(frame.empty())
{
ROS_ERROR("Failed to capture frame.");
break;
}
sensor_msgs::CompressedImage msg;
msg.header.stamp = ros::Time::now();
msg.format = "jpeg";
cv::imencode(".jpg", frame, msg.data);
pub.publish(msg);
ros::spinOnce();
loop_rate.sleep();
}
return 0;
}
```
这个节点使用OpenCV从摄像头捕获图像,并将压缩后的图像发布到ROS话题“/compressed_image”上。
阅读全文