forked from GiladGecht/Barrel-And-Pincushion-Distortion-Fix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtilityFunctions.py
62 lines (51 loc) · 1.91 KB
/
UtilityFunctions.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
54
55
56
57
58
59
60
61
62
import numpy as np
import cv2
import glob
import matplotlib.pyplot as plt
import os
#Utility function for loading an RGB image using OpenCV
def loadImage(filename):
if filename.__class__ != ''.__class__:
return None
img = cv2.imread(filename)
img = img[:,:,::-1]
return img
#
def getPointsforCalibration():
#
objp = np.zeros((6 * 8, 3), np.float32)
objp[:, :2] = np.mgrid[0:8, 0:6].T.reshape(-1, 2)
#Lists to store object points and image points from all the images.
objpoints = [] # 3d points in real world space.
imgpoints = [] # 2d points in image plane.
#Create a list of calibrated images using pictures of a chessboard
os.chdir(os.getcwd() + '\ChessBoards')
images = glob.glob('GO*.jpg')
#Step through the list and search for chessboards corners
for fname in (images):
img = cv2.imread(fname)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#Find the chessboards corners
ret, corners = cv2.findChessboardCorners(gray, (8, 6), None)
#If found, add object points, image points
if ret == True:
objpoints.append(objp)
imgpoints.append(corners)
#Return trained points for image calibration.
os.chdir('..')
return objpoints, imgpoints
#
def myDistort(imageName, param):
# Load image
img = loadImage(imageName)
img_shape = (img.shape[1], img.shape[0]) # Get shape of an image where x = columns and y = rows\
# Do camera calibration given object points and image points
objpoints, imgpoints = getPointsforCalibration()
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, img_shape, None, None)
'''
mtx = Camera matrix
dist = Distance Coefficients
rvecs = Rotation Vectors
tvecs = Translation Vectors
'''
return cv2.undistort(img, mtx, dist * (param), None, mtx)