-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimageCalib.py
37 lines (29 loc) · 1.11 KB
/
imageCalib.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
import numpy as np
import cv2 as cv
import pickle
import glob
import os
with open('cameraMatrix.pkl', 'rb') as f:
cameraMatrix = pickle.load(f)
with open('dist.pkl', 'rb') as f:
dist = pickle.load(f)
if not(os.path.exists("imageCalib")):
os.mkdir("imageCalib")
if not(os.path.exists("imageCalib/Result1")):
os.mkdir("imageCalib/Result1")
if not(os.path.exists("imageCalib/Result2")):
os.mkdir("imageCalib/Result2")
for imagePath in glob.glob("images/camera1/**"):
img = cv.imread(imagePath)
name = imagePath.split("\\")[-1]
h, w = img.shape[:2]
newCameraMatrix, roi = cv.getOptimalNewCameraMatrix(cameraMatrix, dist, (w,h), 1, (w,h))
dst = cv.undistort(img, cameraMatrix, dist, None, newCameraMatrix)
x, y, w, h = roi
dst = dst[y:y+h, x:x+w]
cv.imwrite(f'imageCalib/Result1/{name[:-4]}_caliResult1.png', dst)
mapx, mapy = cv.initUndistortRectifyMap(cameraMatrix, dist, None, newCameraMatrix, (w,h), 5)
dst = cv.remap(img, mapx, mapy, cv.INTER_LINEAR)
x, y, w, h = roi
dst = dst[y:y+h, x:x+w]
cv.imwrite(f'imageCalib/Result2/{name[:-4]}_caliResult2.png', dst)