-
Notifications
You must be signed in to change notification settings - Fork 0
/
chessboard_detection.py
198 lines (166 loc) · 7.42 KB
/
chessboard_detection.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
import logging
import sys
from collections import Counter
from typing import List, Tuple
import numpy as np
from scipy.spatial import distance_matrix
import cv2
from common import Point, Points, approx_quadrilateral_hull, ChessboardFiller
import utils
LOGGER = logging.getLogger('my_logger')
def harris_corner(image: np.ndarray) -> Points:
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# magic numbers are blockSize, ksize, k
response = cv2.cornerHarris(image, 2, 3, 0.04)
# Filter out weak responses
# threshold of (0.01 * max) should return more points than the wanted corners
response = response > 0.01 * response.max()
# Extract points
points = list(zip(*np.where(response == True)))
LOGGER.debug(f'Harris corner detector found {len(points)} corners')
return np.array(points)
def clustering_filter(corners: Points) -> Points:
dist = distance_matrix(corners, corners)
kept = set()
for i in range(len(corners)):
close = False
if len(close_point_idx := np.argwhere(dist[i] < 10).flatten()) != 0:
for j in close_point_idx:
if j in kept:
close = True
break
if not close:
kept.add(i)
LOGGER.debug(f'Clustering filter: {len(corners)}->{len(kept)} corners')
return corners[list(kept)]
def connected_component_filter(corners: Points) -> Points:
graph = distance_matrix(corners, corners) < 30
# Only keep the components with <2 nodes
kept = corners[np.sum(graph, axis=0) <= 2] # <=2 because each node is connected to itself
LOGGER.debug(f'Connected component filter: {len(corners)}->{len(kept)} corners')
return kept
class SquareResponseFilter:
def __init__(self, size=7, half_smoothing_window_size=4):
self.size = size
self.half_smoothing_window_size = half_smoothing_window_size
@staticmethod
def _OTSU_binarization(image: np.ndarray) -> np.ndarray:
grayscaled = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
_, binarized = cv2.threshold(grayscaled, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
return binarized
def _majority_vote_smoothing(self, response: List[int]) -> np.ndarray:
"""Smooth a 1D binary array consists of 255 and 0 with majority vote."""
response = np.array(response)
k = self.half_smoothing_window_size
N = len(response)
new_response = np.zeros(N)
for i in range(N):
cnt = Counter(response[np.arange(i - k, i + k + 1) % N])
new_response[i] = 255 if cnt[255] > cnt[0] else 0
return new_response
def _get_square_response(self, image: np.ndarray, point: Point) -> np.ndarray:
"""Iterate through a square centered at keypoint and get a response."""
assert len(image.shape) == 2, 'Image must be binarized.'
k = self.size
# top-left, bottom-right
tl, br = point - k, point + k
# top-right, bottom-left
tr = np.array([point[0] - k, point[1] + k])
bl = np.array([point[0] + k, point[1] - k])
response = []
for j in range(tl[1], tr[1]):
response.append(image[tl[0], j])
for i in range(tr[0], br[0]):
response.append(image[i, tr[1]])
for j in range(br[1], bl[1], -1):
response.append(image[br[0], j])
for i in range(bl[0], tl[0], -1):
response.append(image[i, bl[1]])
return self._majority_vote_smoothing(response)
@staticmethod
def _count_segments(response: np.ndarray) -> int:
"""Count how many distinct segments of 0 or 255 exist."""
prev = response[0]
cnt = 1
for cur in response[1:]:
if cur != prev:
cnt += 1
prev = cur
return cnt
def filter(self, image: np.ndarray, corners: Points) -> Points:
"""Filter the corners based on square response characteristics.
An internal corner's (represented by x) surrounding region should look like:
1110000
1110000
111x111
0000111
0000111
Recording the values on a square circumference should give us at least 4 (maybe more)
distince segments (_count_segments()).
"""
kept = []
binarized = self._OTSU_binarization(image)
for corner in corners:
try:
response = self._get_square_response(binarized, corner)
except IndexError: # If a point to too close to the border
continue
if self._count_segments(response) in [4, 5]:
kept.append(corner)
LOGGER.debug(f'Square response filter: {len(corners)}->{len(kept)} corners')
return np.array(kept)
def sum_distance_filter(corners: Points, target: int) -> Points:
to_remove = len(corners) - target
if to_remove == 0:
return corners
sum_dist = np.sum(distance_matrix(corners, corners), axis=0)
kept = corners[np.argsort(sum_dist)[:-to_remove]]
LOGGER.debug(f'Sum distance filter: {len(corners)}->{len(kept)} corners')
return kept
def find_chessboard_corners(image: np.ndarray,
pattern_size: Tuple[int, int], # (height, width)
visualize=False) -> Tuple[bool, np.ndarray]:
target = pattern_size[0] * pattern_size[1]
corners = harris_corner(image)
if visualize: utils.visualize_keypoints(image, corners)
if len(corners) < target: return False, np.array([])
corners = clustering_filter(corners)
if visualize: utils.visualize_keypoints(image, corners)
if len(corners) < target: return False, np.array([])
corners = connected_component_filter(corners)
if visualize: utils.visualize_keypoints(image, corners)
if len(corners) < target: return False, np.array([])
square_response_filter = SquareResponseFilter()
corners = square_response_filter.filter(image, corners)
if visualize: utils.visualize_keypoints(image, corners)
if len(corners) < target: return False, np.array([])
corners = sum_distance_filter(corners, target)
if visualize: utils.visualize_keypoints(image, corners)
if len(corners) != pattern_size[0] * pattern_size[1]:
return False, np.ndarray([])
quad = approx_quadrilateral_hull(corners)
if len(quad) == 0: return False, np.array([])
if visualize: utils.visualize_hull(image, quad, corners)
chessboard_filler = ChessboardFiller(pattern_size, corners, quad)
if not chessboard_filler.fill():
return False, np.array([])
corners = chessboard_filler.get()
if visualize: utils.visualize_chessboard(image, pattern_size, corners)
return True, corners
if __name__ == '__main__':
LOGGER.setLevel(logging.INFO)
LOGGER.addHandler(logging.StreamHandler(stream=sys.stdout))
images, paths = utils.read_images('chessboard_patterns/calibration-*.png')
results = []
success_cnt = 0
visualize = True if len(sys.argv) == 2 and sys.argv[1] == 'visualize' else False
for i, image in enumerate(images):
retval, corners = find_chessboard_corners(image, (9, 7), visualize)
if retval:
results.append(corners)
success_cnt += 1
LOGGER.info(f'find_chessboard_corners succeeded for {paths[i]}')
else:
LOGGER.info(f'find_chessboard_corners failed for {paths[i]}')
# find_chessboard_corners(image, (9, 7), visualize=True)
LOGGER.info(f'find_chessboard_corners succeeded for {success_cnt}/{len(images)} images')