-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboardFinder.py
172 lines (152 loc) · 6.76 KB
/
boardFinder.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
import cv2
import numpy as np
from shapely.geometry import Polygon
from shapely.geometry import Point
from typing import List
from timeit import default_timer as timer
from quad import Quad
import math
from equipment import Marker
import aruco
def rint(input):
return np.int32( np.round(input))
class BoardFinder:
def idsPresent(ids):
return (ids is not None and 0 in ids and 1 in ids and 2 in ids and 3 in ids)
def markerFromId(bboxs, idAry, marker: Marker):
indexes = np.where(idAry == marker.value)[0]
marks = []
for index in indexes:
mark = bboxs[index][0]
marks.append(mark)
return marks
def cornerFromBbox(bboxs, idAry, marker : Marker):
marks = BoardFinder.markerFromId(bboxs, idAry, marker)
if (len(marks) == 1):
return marks[0]
else:
print(f'There are {len(marks)} {marker.name} markers: {marks}')
return None
def findCorners(bboxs, ids, imgShape):
#print(f'finding corners on: {img.shape}')
#bboxs, ids = aruco.findArucoMarkers(img, draw=draw)
if (ids is None):
return None
idAry = ids.flatten()
if (BoardFinder.idsPresent(idAry)):
tl = BoardFinder.cornerFromBbox(bboxs, idAry, Marker.BOARD_TOP_LEFT)
tr = BoardFinder.cornerFromBbox(bboxs, idAry, Marker.BOARD_TOP_RIGHT)
br = BoardFinder.cornerFromBbox(bboxs, idAry, Marker.BOARD_BOTTOM_RIGHT)
bl = BoardFinder.cornerFromBbox(bboxs, idAry, Marker.BOARD_BOTTOM_LEFT)
rect = [tl, tr, br, bl]
if any(x is None for x in rect):
print('failed to calibrate')
return None
else:
p : Polygon = Polygon([tl[0], tr[0], br[0], bl[0]])
totalArea = imgShape[0]*imgShape[1]
boardPct = np.round( 100 * p.area / totalArea)
if (boardPct < 50):
print (f'board area too small, {p.area} pixels is only {boardPct} of total image')
return rect
else:
print("Didn't find all corners")
return None
def between(p1, p2, distance):
x = (p2[0] - p1[0]) * distance + p1[0]
y = (p2[1] - p1[1]) * distance + p1[1]
return [int(x), int(y)]
def getSquares(warpedImg, warpWidth, warpHeight, drawPoints=True, drawSquares=True):
#print(warpedImg.shape)
#warpHeight, warpWidth, _ = warpedImg.shape
points = []
for j in range(0,9):
yl = BoardFinder.between([0,0], [0,warpHeight-1], j/8)
row = []
for i in range(0,9):
bt = BoardFinder.between(yl, [warpWidth-1,yl[1]], i/8)
#print(bt)
row.append(bt)
#cv2.circle(warpedImg, bt, 10, (255,0,0), 3)
points.append(row)
#print(points)
if drawPoints:
for row in points:
for point in row:
cv2.circle(warpedImg, point, 10, (255,0,0), 3)
squares = []
for i in range (0,8):
row = []
rowLabel = 8-i
for j in range(0,8):
columnLabel = chr(97 + j)
s = Quad.createQuad(points[i][j], points[i][j+1], points[i+1][j+1], points[i+1][j], f'{columnLabel}{rowLabel}')
row.append(s)
if drawSquares:
cv2.polylines(warpedImg, s.polyCoords(), True, thickness=3, color=(0,0,255))
cv2.putText(warpedImg, s.name, [s.bl[0] + 10, s.bl[1] - 10], cv2.FONT_HERSHEY_SIMPLEX, 2, color=(0,0,255), thickness=3, lineType=2)
squares.append(row)
return squares
#s : Square = Square(points[0][0], points[0][1], points[1][1], points[1][0], 'a1')
def getOriginalSquares(inverseMatrix, warpedSquares):
retval = []
for row in warpedSquares:
newRow = []
for square in row:
newSquare = square.warpInverse(inverseMatrix)
newRow.append(newSquare)
retval.append(newRow)
return retval
def getWarpBoard(img, rect, draw=True):
tl, tr, br, bl = rect
widthA = np.sqrt(((br[0] - bl[0]) ** 2) + ((br[1] - bl[1]) ** 2))
widthB = np.sqrt(((tr[0] - tl[0]) ** 2) + ((tr[1] - tl[1]) ** 2))
# ...and now for the height of our new image
heightA = np.sqrt(((tr[0] - br[0]) ** 2) + ((tr[1] - br[1]) ** 2))
heightB = np.sqrt(((tl[0] - bl[0]) ** 2) + ((tl[1] - bl[1]) ** 2))
# take the maximum of the width and height values to reach
# our final dimensions
maxWidth = max(int(widthA), int(widthB))
maxHeight = max(int(heightA), int(heightB))
# construct our destination points which will be used to
# map the screen to a top-down, "birds eye" view
dst = np.array([
[0, 0],
[maxWidth - 1, 0],
[maxWidth - 1, maxHeight - 1],
[0, maxHeight - 1]], dtype = "float32")
# calculate the perspective transform matrix and warp
# the perspective to grab the screen
#print(rect)
#print(dst)
warpMatrix = cv2.getPerspectiveTransform(rect, dst)
warp = None
if draw:
print("drawing warped board")
warp = cv2.warpPerspective(img, warpMatrix, (maxWidth, maxHeight))
return warp, warpMatrix, maxWidth, maxHeight
def getPieceCenter(corners, pixelLength):
(topLeft, topRight, bottomRight, bottomLeft) = corners
bottomCenter = BoardFinder.between(bottomLeft, bottomRight, .5)
sideLength = math.dist(bottomLeft, topLeft)
scale = pixelLength / sideLength
xDist = rint( (bottomLeft[0] - topLeft[0]) * scale)
yDist = rint((bottomLeft[1] - topLeft[1]) * scale)
center = [bottomCenter[0] + xDist, bottomCenter[1] + yDist]
z = math.dist(bottomCenter, center)
return center
def findCornersAtLastLocation(img, lastSeenCorners, pixelsPerMm):
cornersFound = 0
validIds = [Marker.BOARD_TOP_LEFT.value, Marker.BOARD_TOP_RIGHT.value, Marker.BOARD_BOTTOM_LEFT.value,
Marker.BOARD_BOTTOM_RIGHT.value]
for corner in lastSeenCorners:
polygon = Polygon(corner)
bufferSize = 2 * pixelsPerMm # 2mm buffer
bbox, id = aruco.findArucoMarkersInPolygon(img, polygon, bufferSize, False, False)
#cv2.rectangle(img, [rect[0], rect[1]], [rect[2], rect[3]], color=(0,0,255), thickness=1)
if (id is not None):
id = id.flatten()
if (len(id) == 1 and id[0] in validIds):
cornersFound += 1
return cornersFound
x = BoardFinder.getPieceCenter([[0, 103], [0,0],[7,104], [4, 100]], 75)