Untitled

mail@pastecode.io avatar
unknown
plain_text
6 days ago
2.1 kB
1
Indexable
Never
import numpy as np

def monocular_depth_to_metric(depth_map, fx, fy, baseline, image_width, image_height):
    """
    Convert a monocular depth map to metric depth, considering separate focal lengths for x and y.
    
    Args:
    depth_map (numpy.ndarray): Depth map from a monocular depth estimation model
    fx (float): Focal length in x direction (in pixels)
    fy (float): Focal length in y direction (in pixels)
    baseline (float): An arbitrary baseline for scale (in meters)
    image_width (int): Width of the image in pixels
    image_height (int): Height of the image in pixels
    
    Returns:
    numpy.ndarray: Estimated metric depth map
    """
    # Normalize depth map to 0-1 range if not already
    depth_norm = (depth_map - np.min(depth_map)) / (np.max(depth_map) - np.min(depth_map))
    
    # Convert normalized depth to a pseudo-disparity
    pseudo_disparity = 1 / (depth_norm + 1e-6)  # Adding epsilon to avoid division by zero
    
    # Create coordinate grids
    y, x = np.mgrid[0:image_height, 0:image_width]
    
    # Calculate pixel-wise scaling factors
    scale_x = x / fx
    scale_y = y / fy
    
    # Use the average of x and y scales
    scale = (scale_x + scale_y) / 2
    
    # Estimate pixel disparity
    pixel_disparity = pseudo_disparity * scale
    
    # Convert to metric depth using the average focal length
    avg_focal_length = (fx + fy) / 2
    metric_depth = (avg_focal_length * baseline) / pixel_disparity
    
    return metric_depth

# Example usage
depth_map = np.random.rand(480, 640)  # Example depth map from a monocular model
fx = 500  # Example focal length in x direction (pixels)
fy = 505  # Example focal length in y direction (pixels)
baseline = 0.1  # Arbitrary baseline for scale (in meters)
image_width = 640
image_height = 480

metric_depth = monocular_depth_to_metric(depth_map, fx, fy, baseline, image_width, image_height)

print("Shape of metric depth map:", metric_depth.shape)
print("Min depth:", np.min(metric_depth), "meters")
print("Max depth:", np.max(metric_depth), "meters")
Leave a Comment