-
Notifications
You must be signed in to change notification settings - Fork 0
/
card_separation.py
79 lines (66 loc) · 2.67 KB
/
card_separation.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
import math
import cv2
import numpy as np
CARD_RATIO = 4.3/3.3 # dimensions from Amazon
CARD_IMG_WIDTH = 1000
CARD_IMG_HEIGHT = int(CARD_IMG_WIDTH/CARD_RATIO)
WHITE_MIN = np.array([0, 0, 150],np.uint8)
WHITE_MAX = np.array([180, 100, 255],np.uint8)
def card_segment(img):
# Extract white cards
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
img_thresh = cv2.inRange(img_hsv, WHITE_MIN, WHITE_MAX)
# Find Contours
contours, hierarchy = cv2.findContours(img_thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
img_copy1 = img.copy()
cv2.drawContours(img_copy1, contours, -1, (0, 255, 0), 2, cv2.LINE_AA)
#mmcv2.imshow("image", img_copy1)
# Find Cards
card_contours = []
for contour in contours:
# Get rid of all the contours that are not four-sided
perimeter = cv2.arcLength(contour, True)
vertices = cv2.approxPolyDP(contour, 0.03 * perimeter, True)
if len(vertices) != 4:
continue
card_contours.append(vertices)
# We know there are exactly 12 cards, so find the 12 largest contours
card_contours.sort(key=cv2.contourArea, reverse=True)
card_contours = card_contours[:12]
# Sort
card_contours.sort(key=lambda k:k[0][0][1])
card_contours_sorted = []
for i in range(0,12,3):
card_contours_sorted += sorted(card_contours[i:i+3], key=lambda k:k[0][0][0])
card_contours = card_contours_sorted
img_copy2 = img.copy()
cv2.drawContours(img_copy2, card_contours, -1, (0, 255, 0), 2, cv2.LINE_AA)
# Perspective-skew each card
vertices = np.float32([[0,CARD_IMG_HEIGHT], [0,0], [CARD_IMG_WIDTH,0], [CARD_IMG_WIDTH,CARD_IMG_HEIGHT]])
cards = []
for card in card_contours:
# Reformat card contour because findContour gives strange results
card = card.reshape(4,2)
card = np.array(card, np.float32)
# Orient points the right way
lengths = []
for i in range(3):
p1, p2 = card[i], card[i-1]
dist = math.sqrt((p1[0]-p2[0])**2 + (p1[1]-p2[1])**2)
lengths.append(dist)
if (lengths[0] < lengths[1]):
card = np.roll(card, 2)
# Dewarp
matrix = cv2.getPerspectiveTransform(card, vertices)
card_dewarped = cv2.warpPerspective(img, matrix, (CARD_IMG_WIDTH, CARD_IMG_HEIGHT))
card_dewarped = cv2.flip(card_dewarped, 1) # Temporary fix until I figure out what's flipping it
cards.append(card_dewarped)
return cards
if __name__ == "__main__":
# Read image
img = cv2.imread("test/set.jpg")
# Output
cards = card_segment(img)
for i in range(len(cards)):
cv2.imwrite(f"cards/card{i}.png", cards[i])
cv2.waitKey()