Skip to content

Commit 9c583d5

Browse files
committed
Add Invisibility Cloak
1 parent b4a7024 commit 9c583d5

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed
+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# completed
2+
3+
import cv2
4+
import numpy as np
5+
font = cv2.FONT_HERSHEY_COMPLEX
6+
7+
cap = cv2.VideoCapture(0)
8+
cv2.namedWindow("Magic",cv2.WINDOW_AUTOSIZE)
9+
img = np.zeros((600, 600, 3), np.uint8)
10+
cv2.putText(img, "Press 'S' to continue.", (30,30), font, 1, (0,255,125))
11+
12+
#Getting background image.
13+
while(True):
14+
cv2.imshow("Magic",img)
15+
ret, frame = cap.read()
16+
if cv2.waitKey(0) == ord('s'):
17+
bg = frame
18+
cv2.destroyWindow("Magic")
19+
cv2.imshow("Press 'Y' to confirm background for magic.", bg)
20+
if cv2.waitKey(0) == ord('y'):
21+
cv2.imwrite("bg.jpg",bg)
22+
break
23+
else:
24+
cv2.destroyWindow("Press 'Y' to confirm background for magic.")
25+
continue
26+
cv2.destroyAllWindows()
27+
28+
#from hsv_detector.py
29+
hl = 0 #<----INPUT----
30+
sl = 63 #<----INPUT----
31+
vl = 39 #<----INPUT----
32+
hh = 28 #<----INPUT----
33+
sh = 255 #<----INPUT----
34+
vh = 146 #<----INPUT----
35+
36+
#Real "magic"
37+
while(True):
38+
ret, frame = cap.read()
39+
40+
#bilateral filtering (to preserve the edges)
41+
bfilter = cv2.bilateralFilter(frame,3,95,95)
42+
#cv2.imshow("Bilateral Filter",bfilter)
43+
44+
#Gaussian blurring
45+
gblur = cv2.GaussianBlur(bfilter, (9, 9), 0)
46+
#cv2.imshow("Gaussian Blur", gblur)
47+
48+
#threshing
49+
hsv = cv2.cvtColor(gblur, cv2.COLOR_BGR2HSV)
50+
cv2.imshow("HSV", hsv)
51+
ran = cv2.inRange(hsv,(hl,sl,vl),(hh,sh,vh))
52+
#cv2.imshow("Threshold", ran)
53+
54+
#erosion and dilation
55+
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
56+
opening0 = cv2.morphologyEx(ran, cv2.MORPH_OPEN, kernel)
57+
closing0 = cv2.morphologyEx(opening0, cv2.MORPH_CLOSE, kernel)
58+
opening = cv2.morphologyEx(closing0, cv2.MORPH_OPEN, kernel)
59+
closing = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, kernel)
60+
cv2.imshow("Closing",closing)
61+
thresh = closing
62+
63+
#removing selected region and layering on top of previosuly saved image
64+
back = cv2.imread("bg.jpg", cv2.IMREAD_COLOR)
65+
back_res = cv2.resize(back, (frame.shape[1], frame.shape[0]), interpolation = cv2.INTER_LINEAR)
66+
masked_bg = cv2.bitwise_and(back, back, mask = thresh)
67+
masked_fg = cv2.bitwise_and(frame, frame, mask = cv2.bitwise_not(thresh))
68+
final = cv2.addWeighted(masked_bg, 1, masked_fg, 1, 0)
69+
70+
# showing final frame
71+
cv2.imshow("Final", final)
72+
if cv2.waitKey(1) == ord('q'):
73+
break
74+
75+
cv2.destroyAllWindows()

Invisibility-Cloak/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Invisibility Cloak
2+
Written in python3, this is a simple implementation of chroma-key filter.
3+
4+
1. Use hsv-detector.py to choose hsv values for a particular colour.
5+
2. Input those values in the InvisibilityCloak.py script.

Invisibility-Cloak/hsv-detector.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#Colours
2+
3+
import numpy as np
4+
import cv2
5+
6+
def nothing(x) :
7+
pass
8+
9+
cap = cv2.VideoCapture(0)
10+
11+
#hsv = cv2.cvtColor(frame,cv2.COLOR_GRAY2HSV)
12+
13+
cv2.namedWindow("image",cv2.WINDOW_NORMAL)
14+
15+
cv2.createTrackbar('Hue low','image',0,179,nothing)
16+
cv2.createTrackbar('Sat low','image',0,255,nothing)
17+
cv2.createTrackbar('Val low','image',0,255,nothing)
18+
cv2.createTrackbar('Hue high','image',0,179,nothing)
19+
cv2.createTrackbar('Sat high','image',0,255,nothing)
20+
cv2.createTrackbar('Val high','image',0,255,nothing)
21+
while(1) :
22+
ret, frame = cap.read()
23+
hsv = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
24+
25+
hl = cv2.getTrackbarPos('Hue low','image')
26+
sl = cv2.getTrackbarPos('Sat low','image')
27+
vl = cv2.getTrackbarPos('Val low','image')
28+
hh = cv2.getTrackbarPos('Hue high','image')
29+
sh = cv2.getTrackbarPos('Sat high','image')
30+
vh = cv2.getTrackbarPos('Val high','image')
31+
32+
ran = cv2.inRange(hsv,(hl,sl,vl),(hh,sh,vh))
33+
34+
cv2.imshow("image",ran)
35+
36+
if cv2.waitKey(1) == 27 :
37+
break
38+
39+
cv2.destroyAllWindows()
40+
print (hl)
41+
print (hh)
42+
print (sl)
43+
print (sh)
44+
print (vl)
45+
print (vh)

0 commit comments

Comments
 (0)