-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalibrado_automatico.py
53 lines (41 loc) · 1.8 KB
/
calibrado_automatico.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
'''
Basado en: http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_calib3d/py_calibration/py_calibration.html
'''
import numpy as np
import cv2
import glob
# Definir el número de filas y columnas del tablero de ajedrez.
filas = 7
columnas = 6
# Establecer el criterio de terminación para el algoritmo de subpíxeles de esquina.
criterios = (cv2.TERM_CRITERIA_MAX_ITER + cv2.TERM_CRITERIA_EPS, 30, 0.001)
# Preparar los puntos del objeto.
puntosObjeto = np.zeros((filas * columnas, 3), np.float32)
puntosObjeto[:, :2] = np.mgrid[0:filas, 0:columnas].T.reshape(-1, 2)
# Crear los arreglos para almacenar los puntos del objeto y los puntos de la imagen
arregloPuntosObjeto = []
arregloPuntosImagen = []
# Recorrer los puntos de la imagen.
for ruta in glob.glob('data/calibracion/left[0-1][0-9].jpg'):
# Cargar la imagen y convertirla a escala de grises.
img = cv2.imread(ruta)
gris = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Find the chess board corners
ret, esquinas = cv2.findChessboardCorners(gris, (filas, columnas), None)
# Make sure the chess board pattern was found in the image
if ret:
# Refine the corner position
esquinas = cv2.cornerSubPix(gris, esquinas, (11, 11), (-1, -1), criterios)
# Add the object points and the image points to the arrays
arregloPuntosObjeto.append(puntosObjeto)
arregloPuntosImagen.append(esquinas)
# Draw the corners on the image
cv2.drawChessboardCorners(img, (filas, columnas), esquinas, ret)
# Display the image
cv2.imshow('chess board', img)
cv2.waitKey(500)
# Calibrar la cámara y guardar los resultados.
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(arregloPuntosObjeto, arregloPuntosImagen, gris.shape[::-1], None, None)
print(mtx)
cv2.waitKey(0)
cv2.destroyAllWindows()