Jim Ostrowski | ba2edd1 | 2022-12-03 15:44:37 -0800 | [diff] [blame] | 1 | #ifndef FRC971_VISION_VISUALIZE_ROBOT_H_ |
| 2 | #define FRC971_VISION_VISUALIZE_ROBOT_H_ |
| 3 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 4 | #include "Eigen/Dense" |
| 5 | #include "Eigen/Geometry" |
Jim Ostrowski | ba2edd1 | 2022-12-03 15:44:37 -0800 | [diff] [blame] | 6 | #include <opencv2/core.hpp> |
| 7 | #include <opencv2/highgui.hpp> |
| 8 | #include <opencv2/imgproc.hpp> |
| 9 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame^] | 10 | namespace frc971::vision { |
Jim Ostrowski | ba2edd1 | 2022-12-03 15:44:37 -0800 | [diff] [blame] | 11 | |
| 12 | // Helper class to visualize the coordinate frames associated with |
| 13 | // the robot Based on a virtual camera viewpoint, and camera model, |
| 14 | // this class can be used to draw 3D coordinate frames in a virtual |
| 15 | // camera view. |
| 16 | // |
| 17 | // Mostly useful just for doing all the projection calculations |
| 18 | // Leverages Eigen for transforms and opencv for drawing axes |
| 19 | |
| 20 | class VisualizeRobot { |
| 21 | public: |
milind-u | 2ab4db1 | 2023-03-25 21:59:23 -0700 | [diff] [blame] | 22 | VisualizeRobot(cv::Size default_size = cv::Size(1280, 720)) |
| 23 | : default_size_(default_size) {} |
| 24 | |
Jim Ostrowski | ba2edd1 | 2022-12-03 15:44:37 -0800 | [diff] [blame] | 25 | // Set image on which to draw |
| 26 | void SetImage(cv::Mat image) { image_ = image; } |
| 27 | |
milind-u | a30a4a1 | 2023-03-24 20:49:41 -0700 | [diff] [blame] | 28 | // Sets image to all black. |
milind-u | 2ab4db1 | 2023-03-25 21:59:23 -0700 | [diff] [blame] | 29 | // Uses default_size_ if no image has been set yet, else image_.size() |
| 30 | void ClearImage() { |
| 31 | auto image_size = (image_.data == nullptr ? default_size_ : image_.size()); |
milind-u | a30a4a1 | 2023-03-24 20:49:41 -0700 | [diff] [blame] | 32 | cv::Mat black_image_mat = cv::Mat::zeros(image_size, CV_8UC3); |
| 33 | SetImage(black_image_mat); |
| 34 | } |
| 35 | |
Jim Ostrowski | ba2edd1 | 2022-12-03 15:44:37 -0800 | [diff] [blame] | 36 | // Set the viewpoint of the camera relative to a global origin |
| 37 | void SetViewpoint(Eigen::Affine3d view_origin) { |
| 38 | H_world_viewpoint_ = view_origin; |
| 39 | } |
| 40 | |
| 41 | // Set camera parameters (for projection into a virtual view) |
| 42 | void SetCameraParameters(cv::Mat camera_intrinsics) { |
Jim Ostrowski | 49be823 | 2023-03-23 01:00:14 -0700 | [diff] [blame] | 43 | camera_mat_ = camera_intrinsics.clone(); |
Jim Ostrowski | ba2edd1 | 2022-12-03 15:44:37 -0800 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | // Set distortion coefficients (defaults to 0) |
| 47 | void SetDistortionCoefficients(cv::Mat dist_coeffs) { |
Jim Ostrowski | 49be823 | 2023-03-23 01:00:14 -0700 | [diff] [blame] | 48 | dist_coeffs_ = dist_coeffs.clone(); |
Jim Ostrowski | ba2edd1 | 2022-12-03 15:44:37 -0800 | [diff] [blame] | 49 | } |
| 50 | |
Jim Ostrowski | 49be823 | 2023-03-23 01:00:14 -0700 | [diff] [blame] | 51 | // Sets up a default camera view 10 m above the origin, pointing down |
| 52 | // Uses image_width and focal_length to define a default camera projection |
| 53 | // matrix |
| 54 | void SetDefaultViewpoint(int image_width = 1000, |
| 55 | double focal_length = 1000.0); |
| 56 | |
Jim Ostrowski | ba2edd1 | 2022-12-03 15:44:37 -0800 | [diff] [blame] | 57 | // Helper function to project a point in 3D to the virtual image coordinates |
| 58 | cv::Point ProjectPoint(Eigen::Vector3d point3d); |
| 59 | |
| 60 | // Draw a line connecting two 3D points |
milind-u | a30a4a1 | 2023-03-24 20:49:41 -0700 | [diff] [blame] | 61 | void DrawLine(Eigen::Vector3d start, Eigen::Vector3d end, |
| 62 | cv::Scalar color = cv::Scalar(0, 200, 0)); |
Jim Ostrowski | ba2edd1 | 2022-12-03 15:44:37 -0800 | [diff] [blame] | 63 | |
| 64 | // Draw coordinate frame for a target frame relative to the world frame |
| 65 | // Axes are drawn (x,y,z) -> (red, green, blue) |
| 66 | void DrawFrameAxes(Eigen::Affine3d H_world_target, std::string label = "", |
milind-u | a30a4a1 | 2023-03-24 20:49:41 -0700 | [diff] [blame] | 67 | cv::Scalar label_color = cv::Scalar(0, 0, 255), |
Jim Ostrowski | ba2edd1 | 2022-12-03 15:44:37 -0800 | [diff] [blame] | 68 | double axis_scale = 0.25); |
| 69 | |
Jim Ostrowski | 49be823 | 2023-03-23 01:00:14 -0700 | [diff] [blame] | 70 | // TODO<Jim>: Also implement DrawBoardOutline? Maybe one function w/ |
| 71 | // parameters? |
milind-u | a30a4a1 | 2023-03-24 20:49:41 -0700 | [diff] [blame] | 72 | void DrawRobotOutline(Eigen::Affine3d H_world_robot, std::string label = "", |
| 73 | cv::Scalar color = cv::Scalar(0, 200, 0)); |
Jim Ostrowski | ba2edd1 | 2022-12-03 15:44:37 -0800 | [diff] [blame] | 74 | |
| 75 | Eigen::Affine3d H_world_viewpoint_; // Where to view the world from |
| 76 | cv::Mat image_; // Image to draw on |
milind-u | 2ab4db1 | 2023-03-25 21:59:23 -0700 | [diff] [blame] | 77 | cv::Mat camera_mat_; // Virtual camera intrinsics (defines fov, center) |
| 78 | cv::Mat dist_coeffs_; // Distortion coefficients, if desired (only used in |
| 79 | // DrawFrameAxes |
| 80 | cv::Size default_size_; // Default image size |
Jim Ostrowski | ba2edd1 | 2022-12-03 15:44:37 -0800 | [diff] [blame] | 81 | }; |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame^] | 82 | } // namespace frc971::vision |
Jim Ostrowski | ba2edd1 | 2022-12-03 15:44:37 -0800 | [diff] [blame] | 83 | |
| 84 | #endif // FRC971_VISION_VISUALIZE_ROBOT_H_ |