-
Notifications
You must be signed in to change notification settings - Fork 1
/
card_detection.py
61 lines (48 loc) · 1.94 KB
/
card_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
import numpy as np
import cv2
from matplotlib import pyplot as plt
from mpl_toolkits.axes_grid1 import ImageGrid
import math
def rectify(h):
h = h.reshape((4,2))
hnew = np.zeros((4,2),dtype = np.float32)
add = h.sum(1)
hnew[0] = h[np.argmin(add)]
hnew[2] = h[np.argmax(add)]
diff = np.diff(h,axis = 1)
hnew[1] = h[np.argmin(diff)]
hnew[3] = h[np.argmax(diff)]
return hnew
def Imagedetection(imagepath,numcards,epsilon=0.03):
img = cv2.imread(imagepath)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray,(1,1),1000)
flag, thresh = cv2.threshold(blur, 120, 255, cv2.THRESH_BINARY)
# Find contours
image,contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
contours = sorted(contours, key=cv2.contourArea,reverse=True)
# Select long perimeters only
perimeters = [cv2.arcLength(contours[i],True) for i in range(len(contours))]
listindex=[i for i in range(numcards) if perimeters[i]>perimeters[0]/2]
# Show image
imgcont = img.copy()
[cv2.drawContours(imgcont, [contours[i]], 0, (0,255,0), 5) for i in listindex]
warp = list(range(numcards))
for i in range(numcards):
card = contours[i]
peri = 0.02*cv2.arcLength(card,True)
approx = cv2.approxPolyDP(card,peri,True)
rect = cv2.minAreaRect(contours[i])
r = cv2.boxPoints(rect)
h = np.float32([[0,0],[399,0],[399,399],[0,399]])
approx = np.float32([item for sublist in approx for item in sublist])
print(approx.shape)
transform = cv2.getPerspectiveTransform(approx,h)
warp[i] = cv2.warpPerspective(img,transform,(400,400))
# Show perspective correction
new_img_list = []
for i in range(numcards):
new_img = cv2.cvtColor(warp[i],cv2.COLOR_BGR2RGB)
new_img_list.append(new_img)
cv2.imwrite(f"static/images/cropped/Image{i}.jpg",warp[i])
return new_img_list