-
Notifications
You must be signed in to change notification settings - Fork 1
/
undistort.py
103 lines (81 loc) · 3.25 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
"""
Demonstrating how to undistort images.
Reads in the given calibration file, parses it, and uses it to undistort the given
image. Then display both the original and undistorted images.
To use:
python undistort.py image calibration_file
"""
import numpy as np
import cv2
import matplotlib.pyplot as plt
import argparse
import re
import os
class Undistort(object):
def __init__(self, fin, scale=1.0, fmask=None):
self.fin = fin
# read in distort
with open(fin, 'r') as f:
#chunks = f.readline().rstrip().split(' ')
header = f.readline().rstrip()
chunks = re.sub(r'[^0-9,]', '', header).split(',')
self.mapu = np.zeros((int(chunks[1]),int(chunks[0])),
dtype=np.float32)
self.mapv = np.zeros((int(chunks[1]),int(chunks[0])),
dtype=np.float32)
for line in f.readlines():
chunks = line.rstrip().split(' ')
self.mapu[int(chunks[0]),int(chunks[1])] = float(chunks[3])
self.mapv[int(chunks[0]),int(chunks[1])] = float(chunks[2])
# generate a mask
self.mask = np.ones(self.mapu.shape, dtype=np.uint8)
self.mask = cv2.remap(self.mask, self.mapu, self.mapv, cv2.INTER_LINEAR)
kernel = np.ones((30,30),np.uint8)
self.mask = cv2.erode(self.mask, kernel, iterations=1)
"""
Optionally, define a mask
"""
def set_mask(fmask):
# add in the additional mask passed in as fmask
if fmask:
mask = cv2.cvtColor(cv2.imread(fmask), cv2.COLOR_BGR2GRAY)
self.mask = self.mask & mask
new_shape = (int(self.mask.shape[1]*scale), int(self.mask.shape[0]*scale))
self.mask = cv2.resize(self.mask, new_shape,
interpolation=cv2.INTER_CUBIC)
#plt.figure(1)
#plt.imshow(self.mask, cmap='gray')
#plt.show()
"""
Use OpenCV to undistorted the given image
"""
def undistort(self, img):
return cv2.resize(cv2.remap(img, self.mapu, self.mapv, cv2.INTER_LINEAR),
(self.mask.shape[1], self.mask.shape[0]),
interpolation=cv2.INTER_CUBIC)
def main():
parser = argparse.ArgumentParser(description="Undistort images")
parser.add_argument('image', metavar='img', type=str, help='image to undistort')
parser.add_argument('map', metavar='map', type=str, help='undistortion map')
args = parser.parse_args()
undistort = Undistort(args.map)
print 'Loaded camera calibration'
im = cv2.imread(args.image)
# cv2.namedWindow('Image', cv2.WINDOW_NORMAL)
# cv2.imshow('Image', im)
im_undistorted = undistort.undistort(im)
path_dir, path_name = os.path.split(args.image)
name, ext = os.path.splitext(path_name)
dir_out = os.path.join(path_dir, 'undistored')
if not os.path.exists(dir_out):
os.makedirs(dir_out)
path_out = os.path.join(dir_out, '%s.png'%name)
print ('Writing to: %s'%path_out)
cv2.imwrite(path_out, im_undistorted)
print ('Done!')
# cv2.namedWindow('Undistorted Image', cv2.WINDOW_NORMAL)
# cv2.imshow('Undistorted Image', im_undistorted)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
if __name__ == "__main__":
main()