lamp-triangulation / scripts /azimu-elevation.py
ngocleltt's picture
Upload 7 files
035b725 verified
Raw
History Blame Contribute Delete
2.21 kB
import csv
import math
from pathlib import Path
input_path = Path('triangulation_observations_with_centers.csv')
output_path = Path('triangulation_observations_with_angles.csv')
if not input_path.exists():
raise FileNotFoundError(f'Not find input: {input_path.resolve()}')
image_width = 1280.0
image_height = 720.0
cx = image_width / 2.0
cy = image_height / 2.0
horizontal_fov_deg = 90.0
vertical_fov_deg = 60.0
fx = (image_width / 2.0) / math.tan(math.radians(horizontal_fov_deg / 2.0))
fy = (image_height / 2.0) / math.tan(math.radians(vertical_fov_deg / 2.0))
with open(input_path, 'r', encoding='utf-8-sig') as f:
reader = csv.DictReader(f)
rows = list(reader)
fieldnames = reader.fieldnames[:] if reader.fieldnames else []
new_fields = ['camera_cx', 'camera_cy', 'fx_px', 'fy_px', 'azimuth_deg', 'elevation_deg']
for col in new_fields:
if col not in fieldnames:
fieldnames.append(col)
computed = 0
missing = 0
for row in rows:
row['camera_cx'] = cx
row['camera_cy'] = cy
row['fx_px'] = round(fx, 6)
row['fy_px'] = round(fy, 6)
row['azimuth_deg'] = ''
row['elevation_deg'] = ''
x_str = (row.get('bbox_center_x_px') or '').strip()
y_str = (row.get('bbox_center_y_px') or '').strip()
if not x_str or not y_str:
missing += 1
continue
x = float(x_str)
y = float(y_str)
dx = x - cx
dy = y - cy
azimuth_deg = math.degrees(math.atan(dx / fx))
elevation_deg = -math.degrees(math.atan(dy / fy))
row['azimuth_deg'] = round(azimuth_deg, 6)
row['elevation_deg'] = round(elevation_deg, 6)
computed += 1
with open(output_path, 'w', newline='', encoding='utf-8-sig') as f:
writer = csv.DictWriter(f, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(rows)
print(f'Đã tạo: {output_path}')
print(f'Tổng số dòng: {len(rows)}')
print(f'Số dòng tính được góc: {computed}')
print(f'Số dòng thiếu center pixel: {missing}')
print(f'Giả định camera: HFOV={horizontal_fov_deg}°, VFOV={vertical_fov_deg}°')
print(f'fx={fx:.3f}px, fy={fy:.3f}px, cx={cx:.1f}, cy={cy:.1f}')