-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalibrate.py
executable file
·200 lines (159 loc) · 6.62 KB
/
calibrate.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/usr/bin/env python
'''
camera calibration for distorted images with chess board samples
reads distorted images, calculates the calibration and write undistorted images
usage:
calibrate.py [--debug <output path>] [--square_size] [<image mask>]
default values:
--debug: ./output/
--square_size: 1.0
<image mask> defaults to ../data/left*.jpg
'''
from distutils.log import debug
import numpy as np
import cv2 as cv
from pathlib import Path
import sys
# built-in modules
import os
import glob
import argparse
class FileSource():
def __init__(self, input_dir, ext='bmp'):
search = f'{input_dir}/*'
self.files = sorted(glob.glob(search))
self.index = 0
def read(self, backwards=False):
if backwards:
self.index -= 1
else:
self.index += 1
if self.index >= len(self.files):
self.index = 0
elif self.index < 0:
self.index = len(self.files) - 1
print(f'Index: {self.index} File: {self.files[self.index]}')
img = cv.imread(self.files[self.index])
return True, img
def isOpened(self):
if len(self.files) > 0:
return True
return False
class BlobDetector():
def __init__(self):
# termination criteria
self.criteria = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 30, 0.001)
########################################Blob Detector##############################################
# Setup SimpleBlobDetector parameters.
blobParams = cv.SimpleBlobDetector_Params()
# Change thresholds
blobParams.minThreshold = 8
blobParams.maxThreshold = 255
# Filter by Area.
blobParams.filterByArea = True
blobParams.minArea = 100 # minArea may be adjusted to suit for your experiment
blobParams.maxArea = 4500 # maxArea may be adjusted to suit for your experiment
# Filter by Circularity
blobParams.filterByCircularity = True
blobParams.minCircularity = 0.1
# Filter by Convexity
blobParams.filterByConvexity = True
blobParams.minConvexity = 0.87
# Filter by Inertia
blobParams.filterByInertia = True
blobParams.minInertiaRatio = 0.01
# Create a detector with the parameters
self.blobDetector = cv.SimpleBlobDetector_create(blobParams)
###################################################################################################
###################################################################################################
# Original blob coordinates, supposing all blobs are of z-coordinates 0
# And, the distance between every two neighbour blob circle centers is 72 centimetres
# In fact, any number can be used to replace 72.
# Namely, the real size of the circle is pointless while calculating camera calibration parameters.
self.objp = np.zeros((55, 3), np.float32)
index = 0
for x in range(0, 11):
for y in range(0, 4):
self.objp[index] = (x * 33, y * 33, 0)
index += 1
###################################################################################################
# Arrays to store object points and image points from all the images.
self.objpoints = [] # 3d point in real world space
self.imgpoints = [] # 2d points in image plane.
self.found = 0
def _verify_gray(self, img):
if len(img.shape) > 2:
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
else:
gray = img
return gray
def detect(self, img):
gray = self._verify_gray(img)
keypoints = self.blobDetector.detect(gray) # Detect blobs.
im_with_keypoints = cv.drawKeypoints(gray, keypoints, np.array([]), (0,255,0), cv.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
im_with_keypoints_gray = cv.cvtColor(im_with_keypoints, cv.COLOR_BGR2GRAY)
ret, corners = cv.findCirclesGrid(gray, (5,11), blobDetector = self.blobDetector, flags = (cv.CALIB_CB_ASYMMETRIC_GRID + cv.CALIB_CB_CLUSTERING))
print(f'Number of keypoints: {len(keypoints)} findCirclesGrid ret: {ret}')
if ret == True:
print(f'Found corners: {len(corners)}')
self.objpoints.append(self.objp) # Certainly, every loop objp is the same, in 3D.
corners2 = cv.cornerSubPix(im_with_keypoints_gray, corners, (11,11), (-1,-1), self.criteria) # Refines the corner locations.
self.imgpoints.append(corners2)
# Draw and display the corners.
im_with_keypoints = cv.drawChessboardCorners(gray, (5,11), corners2, ret)
self.found += 1
else:
print('No valid corners found')
return im_with_keypoints
def calibrate(self, img):
gray = self._verify_gray(img)
h, w = gray.shape
print(f'Num objpoints: {len(self.objpoints)} Num imgpoints: {len(self.imgpoints)}')
ret, mtx, dist, rvecs, tvecs = cv.calibrateCamera(self.objpoints, self.imgpoints, (w, h), None, None)
print(f'Ret: {ret} mtx: {mtx} dist: {dist}')
def main():
parser = argparse.ArgumentParser(description='OpenCV Acircle camera cal')
parser.add_argument('-i', '--input', type=str, default='',
help='Specify input directory')
parser.add_argument('-o', '--output', type=str, default='calout',
help='Output debug files')
args = parser.parse_args()
debug_dir = Path(args.output)
if not debug_dir.exists():
debug_dir.mkdir()
pattern_size = (9, 6)
pattern_points = np.zeros((np.prod(pattern_size), 3), np.float32)
pattern_points[:, :2] = np.indices(pattern_size).T.reshape(-1, 2)
#pattern_points *= square_size
bd = BlobDetector()
def show(img, winname='img'):
#small = cv.resize(img, (1280,720))
small = img
cv.imshow(winname, small)
return cv.waitKey(1)
fs = FileSource(args.input)
ret, img = fs.read()
update = False
while True:
key = show(img)
if key == ord('2'):
ret, img = fs.read()
update = True
elif key == ord('1'):
ret, img = fs.read(True)
update = True
elif key == ord('q'):
break
elif key == ord('c'):
bd.calibrate(img)
elif key == ord('u'):
update = True
if update:
update = False
img = cv.rotate(img, cv.ROTATE_180)
drawimg = bd.detect(img)
show(drawimg, 'blobs')
if __name__ == '__main__':
print(__doc__)
main()
cv.destroyAllWindows()