-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainReconstruction.py
89 lines (76 loc) · 2.83 KB
/
mainReconstruction.py
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
import csv
import pickle
from Reconstruction import DLTrecon
import markerdet
# List of video paths
video_paths = ["D:\MaCodes\DLT\moy.MOV"]
minArea = 20
marker_threshold = 100 # Adjust this threshold based on marker visibility
# Load the calibration matrices (Ls) from the pickle file
with open("calibration_params.pkl", "rb") as f:
Ls = pickle.load(f)
nd = 0
if len(Ls[0]) == 9:
nd = 2
elif len(Ls[0]) == 12:
nd = 3
if nd != 2 and nd != 3:
raise ValueError(
"Invalid number of dimensions (nd) in the calibration matrices.")
uv = markerdet.returnUV(video_paths, marker_threshold, minArea)
# Check if the number of cameras (views) used for calibration matches the number of detected marker sets
nc = len(uv[0])
if nc != len(Ls):
raise ValueError("Invalid number of cameras (views).")
# Perform 2D or 3D point reconstruction based on the number of dimensions (nd)
if nd == 2:
# Perform 2D point reconstruction for each frame
XY = []
for frame in uv:
frame_XY = []
for i in range(len(frame[0])):
frame_XY.append(
DLTrecon(nd, nc, Ls, [view[i] for view in frame])[::-1])
XY.append(frame_XY)
# Save reconstructed points to CSV file
csv_file = "XY.csv"
with open(csv_file, mode='w', newline='') as file:
writer = csv.writer(file)
header = ["Frame Number"]
for i in range(len(XY[0])):
header.append(f"Marker {i + 1} (X, Y)")
writer.writerow(header)
for frame_idx, frame_XY in enumerate(XY, start=1):
row = [frame_idx]
for point in frame_XY:
formatted_point = f"({point[0]:.2f}, {point[1]:.2f})"
row.append(formatted_point)
writer.writerow(row)
print("Reconstructed points saved to XY.csv")
elif nd == 3:
# Perform 3D point reconstruction for each frame
XYZ = []
for frame in uv:
frame_XYZ = []
for i in range(len(frame[0])):
frame_XYZ.append(
DLTrecon(nd, nc, Ls, [view[i] for view in frame])[::-1])
XYZ.append(frame_XYZ)
# Save reconstructed points to CSV file
csv_file = "XYZ.csv"
with open(csv_file, mode='w', newline='') as file:
writer = csv.writer(file)
header = ["Frame Number"]
for i in range(len(XYZ[0])):
header.append(f"Marker {i + 1} (X, Y, Z)")
writer.writerow(header)
for frame_idx, frame_XYZ in enumerate(XYZ, start=1):
row = [frame_idx]
for point in frame_XYZ:
formatted_point = f"({point[0]:.2f}, {point[1]:.2f}, {point[2]:.2f})"
row.append(formatted_point)
writer.writerow(row)
print("Reconstructed points saved to XYZ.csv")
else:
raise ValueError(
"Invalid number of dimensions (nd). Only 2D and 3D reconstructions are supported.")