Script Callibrate
This commit is contained in:
119
scripts/callibriate.py
Normal file
119
scripts/callibriate.py
Normal file
@@ -0,0 +1,119 @@
|
||||
|
||||
import cv2
|
||||
import numpy as np
|
||||
import glob
|
||||
import os
|
||||
import sys
|
||||
import argparse
|
||||
|
||||
#
|
||||
# Create calibration .npz from checkerboard images
|
||||
# Usage:
|
||||
# python callibriate.py --camera cam0 --input-dir data/calibration/20260610_143022 [--output-dir ...]
|
||||
#
|
||||
|
||||
# ── CLI-Parameter ──────────────────────────────────────────────────────────────
|
||||
parser = argparse.ArgumentParser(description='Camera calibration from checkerboard images')
|
||||
parser.add_argument('--camera', required=True, help='Camera ID prefix, e.g. cam0')
|
||||
parser.add_argument('--input-dir', default='.', help='Directory containing calibration images')
|
||||
parser.add_argument('--output-dir', default=None, help='Directory to save .npz (default: same as --input-dir)')
|
||||
args = parser.parse_args()
|
||||
|
||||
camera = args.camera
|
||||
input_dir = os.path.abspath(args.input_dir)
|
||||
output_dir = os.path.abspath(args.output_dir) if args.output_dir else input_dir
|
||||
|
||||
def log(msg):
|
||||
print(msg, flush=True) # flush=True → Node.js sieht die Zeile sofort
|
||||
|
||||
# ── Parameters ────────────────────────────────────────────────────────────────
|
||||
CHECKERBOARD = (10, 7) # inner corners
|
||||
square_size = 25.0 / 1000.0 # 25 mm -> meters
|
||||
|
||||
# Prepare object points
|
||||
objp = np.zeros((CHECKERBOARD[0] * CHECKERBOARD[1], 3), np.float32)
|
||||
objp[:, :2] = np.mgrid[0:CHECKERBOARD[0], 0:CHECKERBOARD[1]].T.reshape(-1, 2)
|
||||
objp *= square_size
|
||||
|
||||
objpoints = [] # 3D points
|
||||
imgpoints = [] # 2D points
|
||||
|
||||
# ── Load images ───────────────────────────────────────────────────────────────
|
||||
pattern = os.path.join(input_dir, f"{camera}_*.jpg")
|
||||
images = sorted(glob.glob(pattern))
|
||||
log(f"Camera: {camera}")
|
||||
log(f"Input-Dir: {input_dir}")
|
||||
log(f"Output-Dir: {output_dir}")
|
||||
log(f"Pattern: {pattern}")
|
||||
log(f"Found images: {len(images)}")
|
||||
|
||||
img_size = None
|
||||
|
||||
for fname in images:
|
||||
img = cv2.imread(fname)
|
||||
|
||||
log(f"Processing {os.path.basename(fname)} ...")
|
||||
if img is None:
|
||||
log(f" Warning: could not read {fname}")
|
||||
continue
|
||||
|
||||
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
||||
|
||||
if img_size is None:
|
||||
img_size = gray.shape[::-1]
|
||||
|
||||
ret, corners = cv2.findChessboardCorners(
|
||||
gray,
|
||||
CHECKERBOARD,
|
||||
flags=cv2.CALIB_CB_ADAPTIVE_THRESH +
|
||||
cv2.CALIB_CB_NORMALIZE_IMAGE +
|
||||
cv2.CALIB_CB_FAST_CHECK
|
||||
)
|
||||
|
||||
if ret:
|
||||
corners2 = cv2.cornerSubPix(
|
||||
gray,
|
||||
corners,
|
||||
(11, 11),
|
||||
(-1, -1),
|
||||
(cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 1e-6)
|
||||
)
|
||||
objpoints.append(objp)
|
||||
imgpoints.append(corners2)
|
||||
log(f" ✅ Corners found")
|
||||
else:
|
||||
log(f" ❌ No corners found")
|
||||
|
||||
log(f"\nTotal valid images: {len(objpoints)} / {len(images)}")
|
||||
|
||||
# ── Sanity checks ─────────────────────────────────────────────────────────────
|
||||
if img_size is None:
|
||||
log("ERROR: No images were successfully loaded.")
|
||||
sys.exit(1)
|
||||
|
||||
if len(objpoints) == 0:
|
||||
log("ERROR: No chessboard corners detected in any image. Calibration failed.")
|
||||
sys.exit(1)
|
||||
|
||||
log("\n=== Sanity Checks Passed ===")
|
||||
|
||||
# ── Calibration ───────────────────────────────────────────────────────────────
|
||||
ret, K, D, rvecs, tvecs = cv2.calibrateCamera(
|
||||
objpoints,
|
||||
imgpoints,
|
||||
img_size,
|
||||
None,
|
||||
None
|
||||
)
|
||||
|
||||
log("\n=== Calibration Results ===")
|
||||
log(f"RMS reprojection error: {ret}")
|
||||
log(f"Camera matrix:\n{K}")
|
||||
log(f"Distortion coefficients:\n{D}")
|
||||
|
||||
# ── Save calibration ──────────────────────────────────────────────────────────
|
||||
os.makedirs(output_dir, exist_ok=True)
|
||||
output_path = os.path.join(output_dir, f"{camera}_calibration.npz")
|
||||
np.savez(output_path, camera_matrix=K, dist_coeffs=D)
|
||||
|
||||
log(f"\n✅ Calibration saved to {output_path}")
|
||||
95
scripts/checkerboard_11x8_25mm.pdf
Normal file
95
scripts/checkerboard_11x8_25mm.pdf
Normal file
@@ -0,0 +1,95 @@
|
||||
%PDF-1.4
|
||||
%âãÏÓ
|
||||
1 0 obj
|
||||
<< /Type /Catalog /Pages 2 0 R >>
|
||||
endobj
|
||||
2 0 obj
|
||||
<< /Type /Pages /Kids [3 0 R] /Count 1 >>
|
||||
endobj
|
||||
5 0 obj
|
||||
<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>
|
||||
endobj
|
||||
4 0 obj
|
||||
<< /Length 1465 >>
|
||||
stream
|
||||
q
|
||||
0 g
|
||||
31.18 14.18 70.87 70.87 re
|
||||
172.91 14.18 70.87 70.87 re
|
||||
314.65 14.18 70.87 70.87 re
|
||||
456.38 14.18 70.87 70.87 re
|
||||
598.11 14.18 70.87 70.87 re
|
||||
739.84 14.18 70.87 70.87 re
|
||||
102.05 85.04 70.87 70.87 re
|
||||
243.78 85.04 70.87 70.87 re
|
||||
385.51 85.04 70.87 70.87 re
|
||||
527.24 85.04 70.87 70.87 re
|
||||
668.98 85.04 70.87 70.87 re
|
||||
31.18 155.91 70.87 70.87 re
|
||||
172.91 155.91 70.87 70.87 re
|
||||
314.65 155.91 70.87 70.87 re
|
||||
456.38 155.91 70.87 70.87 re
|
||||
598.11 155.91 70.87 70.87 re
|
||||
739.84 155.91 70.87 70.87 re
|
||||
102.05 226.77 70.87 70.87 re
|
||||
243.78 226.77 70.87 70.87 re
|
||||
385.51 226.77 70.87 70.87 re
|
||||
527.24 226.77 70.87 70.87 re
|
||||
668.98 226.77 70.87 70.87 re
|
||||
31.18 297.64 70.87 70.87 re
|
||||
172.91 297.64 70.87 70.87 re
|
||||
314.65 297.64 70.87 70.87 re
|
||||
456.38 297.64 70.87 70.87 re
|
||||
598.11 297.64 70.87 70.87 re
|
||||
739.84 297.64 70.87 70.87 re
|
||||
102.05 368.51 70.87 70.87 re
|
||||
243.78 368.51 70.87 70.87 re
|
||||
385.51 368.51 70.87 70.87 re
|
||||
527.24 368.51 70.87 70.87 re
|
||||
668.98 368.51 70.87 70.87 re
|
||||
31.18 439.37 70.87 70.87 re
|
||||
172.91 439.37 70.87 70.87 re
|
||||
314.65 439.37 70.87 70.87 re
|
||||
456.38 439.37 70.87 70.87 re
|
||||
598.11 439.37 70.87 70.87 re
|
||||
739.84 439.37 70.87 70.87 re
|
||||
102.05 510.24 70.87 70.87 re
|
||||
243.78 510.24 70.87 70.87 re
|
||||
385.51 510.24 70.87 70.87 re
|
||||
527.24 510.24 70.87 70.87 re
|
||||
668.98 510.24 70.87 70.87 re
|
||||
f
|
||||
Q
|
||||
q
|
||||
0.5 w
|
||||
0 G
|
||||
31.18 14.18 779.53 566.93 re
|
||||
S
|
||||
Q
|
||||
BT
|
||||
/F1 7.5 Tf
|
||||
31.18 3.18 Td
|
||||
(Kalibrierungsmuster | 11x8 Felder | 10x7 innere Ecken | 25 mm/Feld | A4 Querformat, 100% drucken ohne Skalierung) Tj
|
||||
ET
|
||||
endstream
|
||||
endobj
|
||||
3 0 obj
|
||||
<< /Type /Page /Parent 2 0 R
|
||||
/MediaBox [0 0 841.89 595.28]
|
||||
/Contents 4 0 R
|
||||
/Resources << /ProcSet [/PDF /Text] /Font << /F1 5 0 R >> >>
|
||||
>>
|
||||
endobj
|
||||
xref
|
||||
0 6
|
||||
0000000000 65535 f
|
||||
0000000015 00000 n
|
||||
0000000064 00000 n
|
||||
0000001707 00000 n
|
||||
0000000191 00000 n
|
||||
0000000121 00000 n
|
||||
trailer
|
||||
<< /Size 6 /Root 1 0 R >>
|
||||
startxref
|
||||
1870
|
||||
%%EOF
|
||||
Reference in New Issue
Block a user