Hi @tanisha.jain
I made for you an example of ROS 2 node where you can publish an image starting from a file.
import rclpy
from rclpy.node import Node
from std_msgs.msg import String
from cv_bridge import CvBridge
from sensor_msgs.msg import Image, CameraInfo
import cv2
import numpy as np
class MinimalPublisher(Node):
def __init__(self):
super().__init__('minimal_publisher')
self.publisher_ = self.create_publisher(Image, '/image', 10)
self.publisher_info_ = self.create_publisher(CameraInfo, '/camera_info', 10)
timer_period = 0.5 # seconds
self.timer = self.create_timer(timer_period, self.timer_callback)
self.i = 0
self.im_list = []
self.cv_image = cv2.imread('test.jpg') # an RGB image
self.bridge = CvBridge()
self._camera_info = CameraInfo()
self._camera_info.header.frame_id = 'optical_frame'
self._camera_info.height = 720
self._camera_info.width = 1280
self._camera_info.distortion_model = 'plumb_bob'
self._camera_info.d = [-0.010, 0.010, 0.00, 0.00, 0.00]
self._camera_info.k = [484,0.0,630,0.0,485,373,0.0,0.0,1.0]
self._camera_info.r = [0.99,-0.00,-0.01,0.00,0.99,0.00,0.01,-0.00,0.99]
self._camera_info.p = [498,0.0,629,0.0,0.0,498,377,0.0,0.0,0.0,1.0,0.0]
self._camera_info.binning_x = 0
self._camera_info.binning_y = 0
self._camera_info.roi.do_rectify = False
def timer_callback(self):
image = self.bridge.cv2_to_imgmsg(np.array(self.cv_image), "bgr8")
self.publisher_.publish(image)
self.publisher_info_.publish(self._camera_info)
self.get_logger().info('Publishing an image')
def main(args=None):
rclpy.init(args=args)
minimal_publisher = MinimalPublisher()
rclpy.spin(minimal_publisher)
minimal_publisher.destroy_node()
rclpy.shutdown()
if __name__ == '__main__':
main()
The variables are dummies:
- self._camera_info.d
- camera_info.k
- camera_info.r
- camera_info.p
Please set an image on the same folder where you start this ROS 2 node with the name: test.jpg