A common task in robotics-related Computer Vision projects is projecting a 3D point into the 2D image plane of a camera, or vice-versa. In ROS this can be achieved very easily with the help of classes tf.TransformListener and image_geometry.PinholeCameraModel.
Given an arbitrary 3D point in world coordinates, the first step is to convert its coordinates from the global reference frame to a camera-centric frame. Given a tuple
p_3d containing global coordinates of a point, and assuming there is a tf broadcaster publishing transforms between the
/world (global) and
/base_link (camera-centric) frames, the code below transforms
p_3d to camera-centric coordinates:
1 2 3 4 5 6 7 8 9 10 11
The camera-centric point
p_camera can then be projected into the image as in the code below:
1 2 3 4 5 6 7 8 9 10
camera_info argument is an instance of the sensor_msgs.msg.CameraInfo message class, it can be instantiated locally or received through a topic.
Notice that in real-world applications, rather than locally instantiating
image_geometry.PinholeCameraModel objects as in the code above, they'd more likely be instantiated once and kept as attributes of a node object.