-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera_utils.py
More file actions
108 lines (92 loc) · 3.92 KB
/
camera_utils.py
File metadata and controls
108 lines (92 loc) · 3.92 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
import cv2
import numpy as np
# This is a demonstration file for camera calibration
# In a real application, you would capture multiple images from each camera
# using a calibration pattern (like a chessboard) and calibrate each camera individually
def calibrate_cameras():
"""
Demonstrates the calibration process for multiple cameras.
In a real application:
1. You would capture many images of a calibration pattern from each camera
2. Find pattern points (e.g., chessboard corners) in these images
3. Use cv2.calibrateCamera to get camera intrinsics
4. Use stereo calibration to get extrinsics relating cameras to each other
For this demo, we'll use pre-defined values
"""
# Simulated camera matrices (intrinsics)
# In a real application, you would get these from cv2.calibrateCamera
camera_matrices = [
np.array([[800, 0, 480], [0, 800, 270], [0, 0, 1]], dtype=np.float32), # Camera 1
np.array([[800, 0, 480], [0, 800, 270], [0, 0, 1]], dtype=np.float32), # Camera 2
np.array([[800, 0, 480], [0, 800, 270], [0, 0, 1]], dtype=np.float32), # Camera 3
np.array([[800, 0, 480], [0, 800, 270], [0, 0, 1]], dtype=np.float32), # Camera 4
]
# Simulated distortion coefficients
# In a real application, you would get these from cv2.calibrateCamera
distortion_coeffs = [
np.zeros(5, dtype=np.float32), # Camera 1
np.zeros(5, dtype=np.float32), # Camera 2
np.zeros(5, dtype=np.float32), # Camera 3
np.zeros(5, dtype=np.float32), # Camera 4
]
# Simulated rotation and translation vectors (extrinsics)
# These define how cameras are positioned relative to each other
# In a real application, you would get these from stereo calibration
rvecs = [
np.zeros(3, dtype=np.float32), # Camera 1 (reference)
np.array([0, 0.2, 0], dtype=np.float32), # Camera 2
np.array([0, 0.4, 0], dtype=np.float32), # Camera 3
np.array([0, 0.6, 0], dtype=np.float32), # Camera 4
]
tvecs = [
np.zeros(3, dtype=np.float32), # Camera 1 (reference)
np.array([200, 0, 0], dtype=np.float32), # Camera 2
np.array([0, 200, 0], dtype=np.float32), # Camera 3
np.array([200, 200, 0], dtype=np.float32), # Camera 4
]
# Create homography matrices that will map each camera to reference plane
# In a real application, you would compute these based on feature matching or known points
homographies = [
np.eye(3, dtype=np.float32), # Camera 1 (reference)
np.array([ # Camera 2
[1, 0.05, -40],
[0, 1, 20],
[0, 0, 1]
], dtype=np.float32),
np.array([ # Camera 3
[1, 0, 40],
[0.05, 1, -20],
[0, 0, 1]
], dtype=np.float32),
np.array([ # Camera 4
[1, 0.05, 0],
[0.05, 1, 0],
[0, 0, 1]
], dtype=np.float32),
]
return {
'camera_matrices': camera_matrices,
'distortion_coeffs': distortion_coeffs,
'rvecs': rvecs,
'tvecs': tvecs,
'homographies': homographies
}
def create_weight_maps(img_shape, border_size=100):
"""
Create weight maps for feather blending
"""
h, w = img_shape[:2]
# Create a base map
y, x = np.mgrid[0:h, 0:w].astype(np.float32)
# Distance from left, right, top, bottom
left = x.copy()
right = w - x
top = y.copy()
bottom = h - y
# Taking minimum distance to any edge
weights = np.minimum(np.minimum(left, right), np.minimum(top, bottom))
# Normalize to [0, 1] and clip values beyond border_size
weights = np.minimum(weights, border_size) / border_size
return weights
if __name__ == "__main__":
print("Run main.py for camera blending demo")