104 lines
2.7 KiB
Python
104 lines
2.7 KiB
Python
import numpy as np
|
|
import cv2 as cv
|
|
import glob
|
|
|
|
PAT_NX = 7
|
|
PAT_NY = 6
|
|
|
|
def draw_gizmo(img, corners, imgpts):
|
|
corner = tuple(corners[0].ravel().astype("int32"))
|
|
imgpts = imgpts.astype("int32")
|
|
img = cv.line(img, corner, tuple(imgpts[0].ravel()), (255,0,0), 5)
|
|
img = cv.line(img, corner, tuple(imgpts[1].ravel()), (0,255,0), 5)
|
|
img = cv.line(img, corner, tuple(imgpts[2].ravel()), (0,0,255), 5)
|
|
return img
|
|
|
|
|
|
def draw_cube(img, corners, imgpts):
|
|
imgpts = np.int32(imgpts).reshape(-1, 2)
|
|
|
|
# draw ground floor in green
|
|
img = cv.drawContours(img, [imgpts[:4]], -1, (0, 255, 0), -3)
|
|
|
|
# draw pillars in blue color
|
|
for i, j in zip(range(4), range(4, 8)):
|
|
img = cv.line(img, tuple(imgpts[i]), tuple(imgpts[j]), (255), 3)
|
|
|
|
# draw top layer in red color
|
|
img = cv.drawContours(img, [imgpts[4:]], -1, (0, 0, 255), 3)
|
|
|
|
return img
|
|
|
|
def init_data():
|
|
criteria = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 30, 0.001)
|
|
objp = np.zeros((PAT_NX * PAT_NY, 3), np.float32)
|
|
objp[:, :2] = np.mgrid[0:PAT_NX, 0:PAT_NY].T.reshape(-1, 2)
|
|
|
|
axis = np.float32([[3, 0, 0], [0, 3, 0], [0, 0, -3]]).reshape(-1, 3)
|
|
|
|
return criteria, objp, axis
|
|
|
|
def main():
|
|
process_video()
|
|
|
|
def process_video():
|
|
with np.load('results/cam.npz') as X:
|
|
mtx, dist, _, _ = [X[i] for i in ('mtx', 'dist', 'rvecs', 'tvecs')]
|
|
|
|
# Init
|
|
criteria, objp, axis = init_data()
|
|
|
|
# Open camera
|
|
video = cv.VideoCapture(0)
|
|
if not video.isOpened():
|
|
print("Cannot open camera")
|
|
exit()
|
|
|
|
while True:
|
|
# Read a new frame
|
|
ok, img = video.read()
|
|
if not ok:
|
|
return
|
|
process(img, mtx, dist, criteria, objp, axis)
|
|
k = cv.waitKey(1) & 0xFF
|
|
if k == ord('q'):
|
|
break
|
|
|
|
def process_still():
|
|
with np.load('results/cam.npz') as X:
|
|
mtx, dist, _, _ = [X[i] for i in ('mtx', 'dist', 'rvecs', 'tvecs')]
|
|
|
|
# Init
|
|
criteria, objp, axis = init_data()
|
|
|
|
for fname in glob.glob('check*.jpg'):
|
|
img = cv.imread(fname)
|
|
process(img, mtx, dist, criteria, objp, axis)
|
|
k = cv.waitKey(1) & 0xFF
|
|
if k == ord('q'):
|
|
break
|
|
|
|
|
|
def process(img, mtx, dist, criteria, objp, axis): # Load previously saved data
|
|
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
|
|
ret, corners = cv.findChessboardCorners(gray, (PAT_NX, PAT_NY), corners=None, flags=cv.CALIB_CB_ADAPTIVE_THRESH+cv.CALIB_CB_NORMALIZE_IMAGE+cv.CALIB_CB_FAST_CHECK)
|
|
cv.drawChessboardCorners(img, corners=corners, patternSize=(PAT_NX, PAT_NY), patternWasFound=ret)
|
|
|
|
if ret:
|
|
corners2 = cv.cornerSubPix(gray, corners, (11, 11), (-1, -1), criteria)
|
|
|
|
# Find the rotation and translation vectors.
|
|
ret, rvecs, tvecs = cv.solvePnP(objp, corners2, mtx, dist)
|
|
|
|
# project 3D points to image plane
|
|
imgpts, jac = cv.projectPoints(axis, rvecs, tvecs, mtx, dist)
|
|
|
|
img = draw_gizmo(img, corners2, imgpts)
|
|
|
|
cv.imshow('img', img)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|
|
cv.destroyAllWindows()
|