-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteract_with_webcam_via_mouse.py
More file actions
49 lines (40 loc) · 1.48 KB
/
interact_with_webcam_via_mouse.py
File metadata and controls
49 lines (40 loc) · 1.48 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
39
40
41
42
43
44
45
46
47
48
49
import cv2
import numpy as np
def draw_rectangle(event, x, y, flags, params):
global x_init, y_init, drawing, conv1, conv2
# print (x,y)
if event == cv2.EVENT_LBUTTONDOWN:
drawing = True
x_init, y_init = x, y
elif event == cv2.EVENT_MOUSEMOVE:
if drawing:
conv2 = (min(x_init, x), min(y_init, y))
conv1 = (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
conv2 = (min(x_init, x), min(y_init, y))
conv1 = (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
conv1, conv2 = (-1, -1), (-1, -1)
cap = cv2.VideoCapture(0)
# check if Webcam is open correctly
if not cap.isOpened():
raise Exception("Webcam not opened")
cv2.namedWindow('Webcam')
cv2.setMouseCallback('Webcam', draw_rectangle)
while True:
ret, frame = cap.read()
img = cv2.resize(frame, None, fx = 1, fy = 1, interpolation = cv2.INTER_AREA)
cv2.imshow('Webcam', img)
(x0, y0), (x1, y1) = conv1, conv2
# print "*"
# print conv1
img[y0:y1, x0:x1] = 255 - img[y0:y1, x0:x1]
c = cv2.waitKey(1)
if c == 27:
break
cap.release()
cv2.destroyAllWindows()