From 2e24d3c112db840a452d626721c98f30b1bbbe64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=2E=20E=2E=20Dom=C3=ADnguez-Vidal?= Date: Tue, 24 Feb 2026 12:29:15 +0100 Subject: [PATCH] Added param 'keep_image_size' to resize depth image and keep original RGB image size --- config/params.yaml | 1 + depth_anything_3_ros2/depth_anything_3_node.py | 16 +++++++++++++++- launch/depth_anything_3.launch.py | 6 ++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/config/params.yaml b/config/params.yaml index aac2b8b..c733bb1 100644 --- a/config/params.yaml +++ b/config/params.yaml @@ -11,6 +11,7 @@ depth_anything_3: inference_height: 518 # Height for inference inference_width: 518 # Width for inference input_encoding: "bgr8" # Expected input encoding (bgr8/rgb8) + keep_image_size: false # Resize output to match input resolution # Output configuration normalize_depth: true # Normalize depth to [0, 1] range diff --git a/depth_anything_3_ros2/depth_anything_3_node.py b/depth_anything_3_ros2/depth_anything_3_node.py index d186755..bd0d1f7 100644 --- a/depth_anything_3_ros2/depth_anything_3_node.py +++ b/depth_anything_3_ros2/depth_anything_3_node.py @@ -15,9 +15,10 @@ from sensor_msgs.msg import Image, CameraInfo from std_msgs.msg import Header from cv_bridge import CvBridge, CvBridgeError +import cv2 from .da3_inference import DA3InferenceWrapper, SharedMemoryInference, SharedMemoryInferenceFast -from .utils import normalize_depth, colorize_depth, PerformanceMetrics +from .utils import normalize_depth, colorize_depth, PerformanceMetrics, resize_image class DepthAnything3Node(Node): @@ -137,6 +138,7 @@ def _declare_parameters(self) -> None: self.declare_parameter("inference_height", 518) self.declare_parameter("inference_width", 518) self.declare_parameter("input_encoding", "bgr8") + self.declare_parameter("keep_image_size", False) # Output configuration self.declare_parameter("normalize_depth", True) @@ -166,6 +168,7 @@ def _load_parameters(self) -> None: self.inference_height = self.get_parameter("inference_height").value self.inference_width = self.get_parameter("inference_width").value self.input_encoding = self.get_parameter("input_encoding").value + self.keep_image_size = self.get_parameter("keep_image_size").value # Output configuration self.normalize_depth_output = self.get_parameter("normalize_depth").value @@ -233,6 +236,17 @@ def image_callback(self, msg: Image) -> None: # Extract depth map depth_map = result["depth"] + # Resize if requested + if self.keep_image_size: + original_size = (cv_image.shape[0], cv_image.shape[1]) + depth_map = resize_image( + depth_map, target_size=original_size, keep_aspect_ratio=False, interpolation=cv2.INTER_LINEAR + ) + if "confidence" in result: + result["confidence"] = resize_image( + result["confidence"], target_size=original_size, keep_aspect_ratio=False, interpolation=cv2.INTER_NEAREST + ) + # Normalize if requested if self.normalize_depth_output: depth_map = normalize_depth(depth_map) diff --git a/launch/depth_anything_3.launch.py b/launch/depth_anything_3.launch.py index d22b3dd..507636f 100644 --- a/launch/depth_anything_3.launch.py +++ b/launch/depth_anything_3.launch.py @@ -68,6 +68,11 @@ def generate_launch_description(): default_value='bgr8', description='Expected input image encoding (bgr8 or rgb8)' ), + DeclareLaunchArgument( + 'keep_image_size', + default_value='false', + description='Resize output depth map to match input image resolution' + ), # Output configuration DeclareLaunchArgument( @@ -136,6 +141,7 @@ def generate_launch_description(): 'inference_height': LaunchConfiguration('inference_height'), 'inference_width': LaunchConfiguration('inference_width'), 'input_encoding': LaunchConfiguration('input_encoding'), + 'keep_image_size': LaunchConfiguration('keep_image_size'), 'normalize_depth': LaunchConfiguration('normalize_depth'), 'publish_colored': LaunchConfiguration('publish_colored'), 'publish_confidence': LaunchConfiguration('publish_confidence'),