File size: 2,986 Bytes
3903652 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | #!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Multi-Camera View Concatenation Utility
Simple utility for concatenating three camera views:
- Head camera: Keep original size
- Left/Right wrist cameras: Resize to half and stack vertically
"""
import cv2
import numpy as np
from typing import Optional, Tuple
def resize_and_concatenate_frames(
self,
head_img: np.ndarray,
left_img: np.ndarray,
right_img: np.ndarray
) -> Optional[np.ndarray]:
"""
Concatenate three camera views in T-shape layout:
- Top: Head camera (keep original size, e.g., 480x640)
- Bottom left: Left wrist camera (resize to half, e.g., 240x320)
- Bottom right: Right wrist camera (resize to half, e.g., 240x320)
Final output: 720x640 (height x width)
Args:
head_img: Head camera image (keep original size)
left_img: Left wrist camera image (resize to half size)
right_img: Right wrist camera image (resize to half size)
Returns:
Concatenated image with T-shape layout
"""
try:
# Get original dimensions
orig_h, orig_w = head_img.shape[:2]
# Resize wrist cameras to half size
half_h, half_w = orig_h // 2, orig_w // 2
left_resized = cv2.resize(left_img, (half_w, half_h))
right_resized = cv2.resize(right_img, (half_w, half_h))
# Concatenate left and right wrist cameras horizontally for bottom row
bottom_row = np.hstack([left_resized, right_resized])
# Create final T-shape layout:
# Top row: head camera (orig_h x orig_w)
# Bottom row: combined wrist cameras (half_h x orig_w)
combined = np.vstack([head_img, bottom_row])
return combined
except Exception as e:
return None
def get_concatenated_dimensions(original_shape: Tuple[int, int]) -> Tuple[int, int]:
"""
Calculate output dimensions for concatenated frame.
Args:
original_shape: (height, width) of original images
Returns:
(height, width) of concatenated result
"""
h, w = original_shape
# Final: (3w/2) × h
return h, int(w * 1.5)
# Example usage
if __name__ == "__main__":
# Create dummy test images
h, w = 240, 320
head_img = np.random.randint(0, 255, (h, w, 3), dtype=np.uint8)
left_img = np.random.randint(0, 255, (h, w, 3), dtype=np.uint8)
right_img = np.random.randint(0, 255, (h, w, 3), dtype=np.uint8)
# Test concatenation
result = resize_and_concatenate_frames(head_img, left_img, right_img)
if result is not None:
print(f"Original shape: {head_img.shape}")
print(f"Concatenated shape: {result.shape}")
print(f"Expected shape: {get_concatenated_dimensions((h, w))}")
# Save test result (optional)
# cv2.imwrite("test_concatenated.jpg", result)
else:
print("Concatenation failed")
|