-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdet_shape.py
More file actions
38 lines (34 loc) · 1.2 KB
/
det_shape.py
File metadata and controls
38 lines (34 loc) · 1.2 KB
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
import cv2
import numpy as np
cam = cv2.VideoCapture(0)
# frame = cv2.imread('game_pieces.jpg')
while(1):
ret, frame = cam.read()
output = frame.copy()
frame_blur = cv2.GaussianBlur(frame,(5,5),cv2.BORDER_DEFAULT)
# prob add a blur here
if not ret:
print("failed to grab frame")
break
hsv = cv2.cvtColor(frame_blur, cv2.COLOR_BGR2HSV)
lower_yellow = np.array([0,50,50])
upper_yellow = np.array([50, 255, 255])
mask = cv2.inRange(hsv, lower_yellow, upper_yellow)
# output = cv2.bitwise_and(frame,frame, mask= mask)
ret,thresh = cv2.threshold(mask, 40, 255, 0)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
if len(contours) != 0:
# cv2.drawContours(frame, contours, -1, 255, 3)
c = max(contours, key = cv2.contourArea)
cv2.drawContours(output, c, -1, 255, 3)
# x,y,w,h = cv2.boundingRect(c)
# cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2)
cv2.imshow('frame',frame)
# cv2.imshow('frame_blur',frame_blur)
cv2.imshow('mask',mask)
cv2.imshow('output',output)
k = cv2.waitKey(1)
if k%256 == 27:
break
cam.release()
cv2.destroyAllWindows()