ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
Here is a snippet of code I have used to do this and the full code is here. It is a package that also uses opencv to grab images and publish them. Some of the variables are:
image_transport::ImageTransport transport;
image_transport::CameraPublisher image_pub;
camera_info_manager::CameraInfoManager info_mgr;om camera
And the code:
// if subscribers, pub images
if(image_pub.getNumSubscribers() > 0) {
ros::Time time = ros::Time::now();
// convert OpenCV image to ROS message
cv_bridge::CvImage cvi;
cvi.header.stamp = time;
cvi.header.frame_id = "image";
cvi.encoding = "bgr8";
cvi.image = cv_image;
// get camera parameters
sensor_msgs::CameraInfo info = info_mgr.getCameraInfo();
info.header.stamp = cvi.header.stamp;
info.header.frame_id = cvi.header.frame_id; // whatever it is called
info.width = cv_image.cols;
info.height = cv_image.rows;
sensor_msgs::Image im;
cvi.toImageMsg(im);
image_pub.publish(im,info);
}
2 | fix error |
Here is a snippet of code I have used to do this and the full code is here. It is a package that also uses opencv to grab images and publish them. Some of the variables are:
image_transport::ImageTransport transport;
image_transport::CameraPublisher image_pub;
camera_info_manager::CameraInfoManager info_mgr;om camera
info_mgr;
And the code:
// if subscribers, pub images
if(image_pub.getNumSubscribers() > 0) {
ros::Time time = ros::Time::now();
// convert OpenCV image to ROS message
cv_bridge::CvImage cvi;
cvi.header.stamp = time;
cvi.header.frame_id = "image";
cvi.encoding = "bgr8";
cvi.image = cv_image;
// get camera parameters
sensor_msgs::CameraInfo info = info_mgr.getCameraInfo();
info.header.stamp = cvi.header.stamp;
info.header.frame_id = cvi.header.frame_id; // whatever it is called
info.width = cv_image.cols;
info.height = cv_image.rows;
sensor_msgs::Image im;
cvi.toImageMsg(im);
image_pub.publish(im,info);
}