ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
I am most probably missing a point, either I am not using image_transport
properly or there is a bug in the image_transport
package.
The best alternative I have found now is the following one:
#include <ros/ros.h>
#include <opencv2/opencv.hpp>
#pragma GCC diagnostic ignored "-Wunused-parameter"
#include <cv_bridge/cv_bridge.h>
#pragma GCC diagnostic pop
int main(int argc,
char** argv)
{
ros::init(argc, argv, "image_publisher");
ros::NodeHandle nh;
std::string publish_topic("/webcam");
ros::Publisher pub = nh.advertise<sensor_msgs::Image>(publish_topic, 5);
ros::Publisher c_pub = nh.advertise<sensor_msgs::CompressedImage>(publish_topic + "/compressed", 5);
cv::VideoCapture cap;
if (!cap.open(0))
return 1;
ros::Rate loop_rate(25);
while (nh.ok())
{
cv::Mat frame;
cap >> frame;
if (frame.empty())
continue;
sensor_msgs::ImagePtr msg = cv_bridge::CvImage(std_msgs::Header(), "bgr8", frame).toImageMsg();
sensor_msgs::CompressedImagePtr c_msg =
cv_bridge::CvImage(std_msgs::Header(), "rgb8", frame).toCompressedImageMsg();
pub.publish(msg);
c_pub.publish(c_msg);
ros::spinOnce();
loop_rate.sleep();
}
return 0;
}
I set two publishers: one for the raw image (bgr8
encoding) and one for the compressed image (rgb8
encoding)
This way I get both the JPEG compressed images and the raw image with decent performance and the right colors.