Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 43a859c

Browse files
Merge pull request #577 from Cartikx3/main
Invisible Clock using Python #417
2 parents c69ef33 + dc3a99c commit 43a859c

File tree

4 files changed

+119
-0
lines changed

4 files changed

+119
-0
lines changed
10.5 MB
Loading[フレーム]
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Invisibility Cloak
2+
3+
# Aim
4+
Hello EVERYONE,lets do some magic using computer vision. I hope you all know about ‘invisible cloak’, this will make you invisible. We will see how we can do the same magic trick with the help of computer vision. I will code with python and use the opencv and numpy library.
5+
6+
# Purpose
7+
Our purpose is to become Invisible using a cloak with the help of various Python libraries.
8+
9+
# Setup instructions
10+
11+
1. Install the latest python 3.5 and above and once you finish installing it .
12+
2. Add opencv library.
13+
3. Add Numpy library.
14+
15+
# Detailed explanation
16+
17+
We wll learn how to create our own ‘Invisibility Cloak’ using simple computer vision techniques in OpenCV. Here we have written this code in Python because it provides exhaustive and sufficient library to build this program.
18+
19+
Here, we will create this magical experience using an image processing technique called Color detection and segmentation. In order to run this code, you need an mp4 video named "video.mp4". You must have a cloth of same color and no other color should be visible into that cloth. We are taking the red cloth. If you are taking some other cloth, the code will remain the same but with minute changes.
20+
21+
firstly the program when you run it it will capture the background image and then. When you put a red colored colored cloth in front of camera the program will detect the colored cloak using color detection and segmentation algorithm . And the program will generate a mask to determine the region in the frame corresponding to the detected color(red in this case). Program will refine this mask and then use it for segmenting out the cloth from the frame. Program will replace the pixels value of the detected red color region with corresponding pixel values of the static background.
22+
23+
# Compilation Steps
24+
25+
1. Capture and store the background frame
26+
2. Detect the defined color using color detection and segmentation algorithm.
27+
3. Segment out the defined colored part by generating a mask.
28+
4. Generate the final augmented output to create a magical effect.
29+
30+
31+
32+
# Output
33+
34+
35+
![](Media/output.gif)
36+
37+
38+
39+
40+
41+
# Author(s):-
42+
[Kartik Srivardhan](http://github.com/Cartikx3)
43+
44+
45+
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import cv2
2+
import time
3+
import numpy as np
4+
5+
## Preparation for writing the ouput video
6+
fourcc = cv2.VideoWriter_fourcc(*'XVID')
7+
out = cv2.VideoWriter('output.avi',fourcc,20.0, (640,480))
8+
9+
##video capturing from the webcam
10+
cap = cv2.VideoCapture(0)
11+
12+
## Allow the system to sleep for 3 seconds before the webcam starts
13+
time.sleep(3)
14+
count = 0
15+
background = 0
16+
17+
## Capture the background in range of 60
18+
for i in range(60):
19+
ret,background = cap.read()
20+
background = np.flip(background,axis=1)
21+
22+
23+
## Read every frame from the webcam, until the camera is open
24+
while(cap.isOpened()):
25+
ret, img = cap.read()
26+
if not ret:
27+
break
28+
count+=1
29+
img = np.flip(img,axis=1)
30+
31+
## Convert the color space from BGR to HSV
32+
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
33+
34+
## Generate masks to detect red color
35+
lower_red = np.array([0,120,50])
36+
upper_red = np.array([10,255,255])
37+
mask1 = cv2.inRange(hsv,lower_red,upper_red)
38+
39+
lower_red = np.array([170,120,70])
40+
upper_red = np.array([180,255,255])
41+
mask2 = cv2.inRange(hsv,lower_red,upper_red)
42+
43+
mask1 = mask1+mask2
44+
45+
## Open and Dilate the mask image
46+
mask1 = cv2.morphologyEx(mask1, cv2.MORPH_OPEN, np.ones((3,3),np.uint8))
47+
mask1 = cv2.morphologyEx(mask1, cv2.MORPH_DILATE, np.ones((3,3),np.uint8))
48+
49+
50+
## Create an inverted mask to segment out the red color from the frame
51+
mask2 = cv2.bitwise_not(mask1)
52+
53+
54+
## Segment the red color part out of the frame using bitwise and with the inverted mask
55+
res1 = cv2.bitwise_and(img,img,mask=mask2)
56+
57+
## Create image showing static background frame pixels only for the masked region
58+
res2 = cv2.bitwise_and(background, background, mask = mask1)
59+
60+
61+
## Generating the final output
62+
finalOutput = cv2.addWeighted(res1,1,res2,1,0)
63+
out.write(finalOutput)
64+
cv2.imshow("magic",finalOutput)
65+
cv2.waitKey(1)
66+
67+
cap.release()
68+
out.release()
69+
cv2.destroyAllWindows()
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
This projects requires latest Python version (i.e 3.5 and above) that has PIP built in and the libraries that are imported are :-
2+
3+
1. openCV Library.
4+
2. Numpy Library.
5+
3. time Module.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /