-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinv_cloak.py
43 lines (32 loc) · 1.38 KB
/
inv_cloak.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 cv2
import numpy as np #mathematical library for image handling
cap=cv2.VideoCapture(0)
background=cv2.imread('./image.jpg')
while cap.isOpened():
ret, current_frame = cap.read()
if ret:
hsv_frame = cv2.cvtColor(current_frame, cv2.COLOR_BGR2HSV)
# range for lower red
l_red = np.array([0, 120, 70])
u_red = np.array([10, 255, 255])
mask1 = cv2.inRange(hsv_frame, l_red, u_red)
#range for upper red
l_red = np.array([170, 120, 70])
u_red = np.array([180, 255, 255])
mask2 = cv2.inRange(hsv_frame, l_red, u_red)
red_mask = mask1 + mask2
#morpphoogy function in open cv
red_mask = cv2.morphologyEx(red_mask, cv2.MORPH_OPEN, np.ones((3, 3), np.uint8), iterations=10)
red_mask = cv2.morphologyEx(red_mask, cv2.MORPH_DILATE, np.ones((3, 3), np.uint8), iterations=1)
# subsituting the red portion with background image
part1 = cv2.bitwise_and(background, background, mask=red_mask)
# detecting things which are not red
red_free = cv2.bitwise_not(red_mask)
# if cloak is not present show the current image
part2 = cv2.bitwise_and(current_frame, current_frame, mask=red_free)
# final output
cv2.imshow("cloak", part1 + part2)
if cv2.waitKey(5) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()