forked from Vatsalparsaniya/Realtime-OpenCV-Chess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemp.py
67 lines (58 loc) · 2.12 KB
/
temp.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
import os
import cv2
import numpy as np
from read_warp_img import get_warp_img
def nothing(X):
pass
def thresold_calibreation(img):
cv2.namedWindow("thresold_calibration")
cv2.createTrackbar("threshold", "threshold_calibration", 0, 255, nothing)
while True:
t = cv2.getTrackbarPos("threshold", "threshold_calibration")
matrix,thresold = cv2.threshold(gray,t,255,cv2.THRESH_BINARY_INV)
cv2.imshow("threhsold",thresold)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
dir_path = os.path.dirname(os.path.realpath(__file__))+"/numpy_saved/1" # path of current directory
device = cv2.VideoCapture(1)
ret , img = device.read()
img = cv2.resize(img,(800,800))
img_resize = (800,800)
img = get_warp_img(img,dir_path,img_resize)
img = cv2.resize(img,(800,800))
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cv2.imshow("Image",gray)
cv2.waitKey(0)
thresold_calibreation(gray)
matrix,thresold = cv2.threshold(gray,9,255,cv2.THRESH_BINARY_INV)
cv2.imshow("threshold",thresold)
cv2.waitKey(0)
# kernel = np.ones((3,3),np.uint8)
# erosion = cv2.erode(thresold,kernel,iterations = 1)
# cv2.imshow("erosion",erosion)
# cv2.waitKey(0)
kernel = np.ones((3,3),np.uint8)
# closing = cv2.morphologyEx(thresold, cv2.MORPH_CLOSE, kernel,iterations=5)
# cv2.imshow("closing",closing)
# cv2.waitKey(0)
contours,_= cv2.findContours(thresold,cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
required_contoures = []
required_contoures_mid_point = []
cnt_rectangle = []
for cnt in contours:
area = cv2.contourArea(cnt)
if(area > 500):
print(area)
(x, y, w, h) = cv2.boundingRect(cnt)
required_contoures.append(cnt)
required_contoures_mid_point.append([x+int(w/2),y+int(h/2)])
cnt_rectangle.append([x,y,w,h])
cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2)
cv2.imshow("final",img)
cv2.waitKey(0)
# HSV = cv2.cvtColor(thresold,cv2.COLOR_BGR2HSV)
# board_color_calibration.color_calibration(HSV,"b")
# lower__b = np.load(dir_path+"/chess_b_color_points.npz")['lower']
# upper__b = np.load(dir_path+"/chess_b_color_points.npz")['upper']
# cv2.waitKey(0)
cv2.destroyAllWindows()