forked from chanmol1999/hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopencv.py
57 lines (38 loc) · 1.2 KB
/
opencv.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
[1]:
import cv2
cap = cv2.VideoCapture(0)
# CALLBACK FUNCTION
def draw_rectangle(event,x,y,flags,param):
global pt1,pt2,topLeft_clicked,botRight_clicked
if event == cv2.EVENT_LBUTTONDOWN:
# RESET THE RECTANGLE
if topLeft_clicked and botRight_clicked:
pt1 = (0,0)
pt2 = (0,0)
topLeft_clicked = False
botRight_clicked = False
if topLeft_clicked == False:
pt1 = (x,y)
topLeft_clicked = True
elif botRight_clicked == False:
pt2=(x,y)
botRight_clicked=True
# GLOBAL VARIABLES
pt1 = (0,0)
pt2 = (0,0)
topLeft_clicked = False
botRight_clicked = False
# CONNECT CALLBACK
cv2.namedWindow('Test')
cv2.setMouseCallback('Test',draw_rectangle)
while True:
ret,frame = cap.read()
if topLeft_clicked:
cv2.circle(frame,center=pt1,radius=2,color=(0,0,255),thickness=-1)
if topLeft_clicked and botRight_clicked:
cv2.rectangle(frame,pt1,pt2,(0,0,255),1)
cv2.imshow('Test',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()