forked from sam0holix/invisibility_cloak_py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloak.py
38 lines (30 loc) · 1019 Bytes
/
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
import cv2 as cv
import time
import numpy as np
cap = cv.VideoCapture(0)
time.sleep(1)
#define which color you want to mask out using the given plot of HSV. It's green here.
lower_range = np.array([40,30,80])
upper_range = np.array([45,255,255])
for x in range(60):
ret,background=cap.read()
if not ret:
continue
while(cap.isOpened()):
return_val,image = cap.read()
if not return_val:
break
#if the image is flipped, use image = np.flip(image, axis = 1)
hsv = cv.cvtColor(image, cv.COLOR_BGR2HSV)
mask = cv.inRange(hsv,lower_range,upper_range)
mask = cv.morphologyEx(mask, cv.MORPH_OPEN, np.opes((3,3),np.uint8),iterations = 2)
mask = cv.dilate(mask, np.ones((3,3),np.uint8),iterations = 1)
mask1 = mask.bitwise_not(mask)
result1 = cv.bitwise_and(background, background, mask = mask)
result2 = cv.bitwise_and(image, image, mask = mask1)
output = cv.addWeighted(result1, 1, result2, 1, 0)
cv.imshow("Result",output)
if cv.waitKey(10) == ord('q'):
break
cap.release()
cv.destroyAllWindows()