I'm using a simulation in Gazebo using ROS2, the drone is from the University of Shanghai and you can find it here Github link. The problem I'm facing is that I can't find out why the projection of the 3D world objects in the 2D coordinates of the camera used on the drone gives me normalized values which are way bigger than (-1,1). My goal is to find if the objects are in the camera fov or not by drawing a dot in the center of them.
Below is the function callback to draw on the processed objects:
` def process_and_draw_objects(self, image): # Print the intrinsic and extrinsic parameters print(f"Intrinsic Matrix (K):\n{self.K}") print(f"Extrinsic Parameters:\nRotation (R):\n{self.R} \nTranslation (t):\n{self.t}")
for object_name, data in self.objects_data.items():
pose_3d_world = np.array(data["pose"])
# Print original 3D position of the object
print(f"Object {object_name} 3D World Pose:\n{pose_3d_world}")
# Transform to camera frame
pose_cam = np.dot(self.R, pose_3d_world.reshape((3, 1))) + self.t
# Print the pose in camera frame
print(f"Pose in camera frame:\n{pose_cam}")
# Project onto the image plane
point_2d_homogeneous = np.dot(self.K, pose_cam)
if point_2d_homogeneous[2] != 0:
point_2d = point_2d_homogeneous[:2] / point_2d_homogeneous[2]
print(f"Projected 2D point (normalized):\n{point_2d}")
# Check image bounds
x_img, y_img = point_2d.flatten()
if 0 <= x_img < self.image_width and 0 <= y_img < self.image_height:
projected_point = (int(x_img), int(y_img))
# If visible, draw on image
cv2.circle(image, projected_point, 5, (0, 255, 0), -1)
print(f"Object {object_name} is visible at {projected_point}")
else:
print(f"Projected 2D point out of image bounds: {point_2d.flatten()}")
print(f"Object {object_name} is not visible.")
else:
print(f"Point behind camera: z={point_2d_homogeneous[2]}")
print(f"Object {object_name} is not visible.")
return image`
I'm subscribed to the necessary ROS2 nodes in order to receive the data needed in order to calculate the real-time simulation camera, and what it sees. Here is my full script (Github gist).