-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdraw.py
43 lines (33 loc) · 1.23 KB
/
draw.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
import numpy as np
import cv2
import test
def draw_rectangle(event, x, y, flags, params):
global x_init, y_init, drawing, top_left_pt, bottom_right_pt
if event == cv2.EVENT_LBUTTONDOWN:
drawing = True
x_init, y_init = x, y
elif event == cv2.EVENT_MOUSEMOVE:
if drawing:
top_left_pt = (min(x_init, x), min(y_init, y))
bottom_right_pt = (max(x_init, x), max(y_init, y))
img[y_init:y, x_init:x] = 255 - img[y_init:y, x_init:x]
elif event == cv2.EVENT_LBUTTONUP:
drawing = False
top_left_pt = (min(x_init, x), min(y_init, y))
bottom_right_pt = (max(x_init, x), max(y_init, y))
img[y_init:y, x_init:x] = 255 - img[y_init:y, x_init:x]
if __name__=='__main__':
drawing= False
top_left_pt, bottom_right_pt=(-1,-1), (-1,-1)
cap = cv2.VideoCapture(0)
cv2.namedWindow('Webcam')
cv2.setMouseCallback('Webcam',draw_rectangle)
while True:
ret, frame =cap.read()
img=cv2.resize(frame, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_AREA)
img[y0:y1, x0:x1] =255-img[y0:y1, x0:x1]
cv2.imshow('Webcam', img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()