utils.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. import numpy as np
  2. def read_calibration_file(calib_file_path):
  3. with open(calib_file_path, 'r') as f:
  4. lines = f.readlines()
  5. return lines
  6. def extract_intrinsic_matrix(calib_lines, camera_id='P0'):
  7. # Find the line corresponding to the camera_id
  8. for line in calib_lines:
  9. if line.startswith(camera_id):
  10. # Split the line and convert to float
  11. values = line.strip().split()[1:]
  12. values = [float(val) for val in values]
  13. # Reshape to 3x4 matrix (the last column is for extrinsics, we need the 3x3 part)
  14. P = np.array(values).reshape(3, 4)
  15. K = P[:3, :3]
  16. return K
  17. return None
  18. def main():
  19. calib_file_path = "../data/data_odometry_gray/dataset/sequences/00/calib.txt"
  20. calib_lines = read_calibration_file(calib_file_path)
  21. intrinsic_matrix = extract_intrinsic_matrix(calib_lines, camera_id='P0')
  22. if intrinsic_matrix is not None:
  23. print("Intrinsic Matrix (K):")
  24. print(intrinsic_matrix)
  25. else:
  26. print("Intrinsic matrix not found for the specified camera ID.")
  27. if __name__ == "__main__":
  28. main()