-
Notifications
You must be signed in to change notification settings - Fork 0
/
undistort.py
49 lines (42 loc) · 1.3 KB
/
undistort.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
import cv2 as cv
import numpy as np
import time
# calib = np.load("calibration_params.npz")
# calib = np.load("wide_angle_calibration.npz")
calib = np.load("left_calibration.npz")
mtx = calib['camera_matrix']
dist = calib['distortion']
print(mtx)
print(dist)
cap = cv.VideoCapture(0)
if not cap.isOpened():
print( "Error opening camera" )
exit(0)
else:
while True:
ret, frame = cap.read()
if ret:
h, w = frame.shape[:2]
w = 910
newcameramtx, roi = cv.getOptimalNewCameraMatrix(mtx, dist, (w,h), 1, (w,h))
break
print("=== Optimal camera matrix ===")
print(newcameramtx)
print("=== ROI ===")
print(roi)
# map1, map2 = cv.fisheye.initUndistortRectifyMap(mtx, dist, np.eye(3), mtx, (w, h), cv.CV_16SC2)
while True:
ret, frame = cap.read()
if not ret:
print("Dropped image")
time.sleep(0.025)
continue
frame = frame[:, :910, :]
# dst = cv.undistort(frame, mtx, dist, None, mtx)
dst = cv.undistort(frame, mtx, dist, None, newcameramtx)
# dst = cv.remap(frame, map1, map2, interpolation=cv.INTER_LINEAR, borderMode=cv.BORDER_CONSTANT)
cv.imshow("Raw image stream", frame)
x, y, w, h = roi
cv.imshow("Undistorted", dst[y:y+h, x:x+w])
# cv.imshow("Undistorted", dst)
key = cv.waitKey(50)