Last active
June 7, 2024 08:41
-
-
Save guilyx/433e3bbf322255733475118b424e679b to your computer and use it in GitHub Desktop.
Python script to calculate the horizontal and vertical Field of View (FOV) from a ROS2 CameraInfo message.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import math | |
| from sensor_msgs.msg import CameraInfo | |
| def calculate_fov(camera_info: CameraInfo): | |
| """ | |
| Calculate the horizontal and vertical Field of View (FOV) from a CameraInfo message. | |
| Parameters: | |
| camera_info (CameraInfo): ROS2 CameraInfo message containing intrinsic camera parameters. | |
| Returns: | |
| tuple: Horizontal FOV (in degrees), Vertical FOV (in degrees) | |
| """ | |
| fx = camera_info.K[0] # Focal length in pixels along x-axis | |
| fy = camera_info.K[4] # Focal length in pixels along y-axis | |
| width = camera_info.width | |
| height = camera_info.height | |
| # Calculate horizontal and vertical FOV | |
| fov_x = 2 * math.atan(width / (2 * fx)) | |
| fov_y = 2 * math.atan(height / (2 * fy)) | |
| # Convert radians to degrees | |
| fov_x = math.degrees(fov_x) | |
| fov_y = math.degrees(fov_y) | |
| return fov_x, fov_y | |
| # Example usage with a CameraInfo message | |
| if __name__ == "__main__": | |
| # Create a sample CameraInfo message with example intrinsic parameters | |
| camera_info = CameraInfo() | |
| camera_info.K = [500, 0, 320, 0, 500, 240, 0, 0, 1] # Example intrinsic parameters | |
| camera_info.width = 640 | |
| camera_info.height = 480 | |
| fov_x, fov_y = calculate_fov(camera_info) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment