👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
In this article, we will learn how to create your own Invisibility Cloak using simple computer vision techniques in OpenCV . We wrote this code in Python here because it provides a comprehensive and sufficient library to build this program.
Here we will create this magical experience using image processing techniques. To run this code, you need a video in video.mp4
mp4 named " video.mp4
". You must have the fabric of the same color, and it must not have a different color on it. We take the red cloth. If you change clothes, the code will remain the same, but with minor changes.
Why red? Is green my favorite?
Surely we could use green, isn’t the magician’s red? Jokes aside, colors like green or blue will also work well with small changes to the code.
This method is the opposite of green screening . On the green screen, we are removing the background, but here we are removing the foreground frame. So let’s start our code.
Algorithm :
1. Capture and store the background frame [This will be done for some seconds]
2. Detect the red colored cloth using color detection and segmentation algorithm.
3. Segment out the red colored cloth by generating a mask. [used in code]
4. Generate the final augmented output to create a magical effect. [video.mp4]
Below is the code:
import
cv2
import
numpy as np
import
time
# replace red pixels (or unwanted area) with
# background pixels to create invisibility function.
## 1. Hue: This channel encodes color information. Hue can be
# thought angle where 0 degrees corresponds to red
# 120 degrees corresponds to green and 240 degrees
# matches blue.
## 2. Saturation: this channel encodes the intensity / purity of the color.
# For example, pink is less saturated, than red.
## 3. Meaning: This channel encodes the brightness of the color .
# Shading and gloss components of the image appear in this
# video image reader
# to check the cv2 version
print
(cv2 .__ version__)
# taking video.mp4 as input
# Make your own path according to your needs
capture_video
=
cv2.VideoCapture (
"video.mp4"
)
# let the camera warm up
time.sleep (
1
)
count
=
0
background
=
0
# capture the background in the 60 range
# you have there should be a video that has a few seconds
# outlined with a background frame so that
# can easily save background image
for
i
in
range
(
60
):
return_val, background
=
capture_video.read ()
if
return_val
=
=
False
:
continue
background
=
np.flip (background, axis
=
1
)
# frame flip
# we read from the video
while
(capture_video.isOpened ()):
return_val, img
=
capture_video.read ()
if
not
return_val:
break
count
=
count
+
1
img
=
np.flip (img, axis
=
1
)
# convert image - BGR to HSV
# how we focused on red detection
# convert BGR to HSV for better
# detection or you can convert it to gray
hsv
=
cv 2.cvtColor (img, cv2.COLOR_BGR2HSV)
# ------------------------------------- BLOCK ----------- ----------------- #
# ranges should be chosen carefully
# setting the lower and upper ranges for mask1
lower_red
=
np.array ([
100
,
40
,
40
])
upper_red
=
np.array ([
100
,
255
,
255
])
mask1
=
cv2.inRange (hsv, lower_red, upper_red)
# set low and high range for mask2
lower_red
=
np.array ([
155
,
40
,
40
])
upper_red
=
np.array ([
180
,
255
,
255
])
mask2
=
cv2 .inRange (hsv, lower_red, upper_red)
# -------- ----------------------------------------- --------- ------------ #
# the above code block can be replaced with
# different code depending on the color of your fabric
mask1
=
mask1
+
mask2
# Refine the mask to match the detected red
mask1
=
cv2.morphologyEx (mask1, cv2.MORPH_OPEN, np.ones ((
3
,
3
),
np.uint8), iterations
=
2
)
mask1
=
cv2.dilate (mask1, np.ones ((
3
,
3
), np. uint8), iterations
=
1
)
mask2
=
cv2.bitwise_not (mask1)
# Generate the final result
res1
=
cv2.bitwise_and (background, background, mask
=
mask1)
res2
=
cv2.bitwise_and (img, img, mask
=
mask2)
final_output
=
cv2.addWeighted (res1,
1
, res2,
1
,
0
)
cv2.imshow (
"INVISIBLE MAN"
, final_output)
k
=
cv2.waitKey (
10
)
if
k
=
=
27
:
break
Exit :
You can check the source code in the project github repository for video input and more details — here
Link: http://datasciencenthusiast.com/?p=71
👻 Read also: what is the best laptop for engineering students?
We hope this article has helped you to resolve the problem. Apart from Invisibility Cloak Using OpenCV | Python Project, check other __main__ Python module-related topics.
Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.
By the way, this material is also available in other languages:
- Italiano Invisibility Cloak Using OpenCV | Python Project
- Deutsch Invisibility Cloak Using OpenCV | Python Project
- Français Invisibility Cloak Using OpenCV | Python Project
- Español Invisibility Cloak Using OpenCV | Python Project
- Türk Invisibility Cloak Using OpenCV | Python Project
- Русский Invisibility Cloak Using OpenCV | Python Project
- Português Invisibility Cloak Using OpenCV | Python Project
- Polski Invisibility Cloak Using OpenCV | Python Project
- Nederlandse Invisibility Cloak Using OpenCV | Python Project
- 中文 Invisibility Cloak Using OpenCV | Python Project
- 한국어 Invisibility Cloak Using OpenCV | Python Project
- 日本語 Invisibility Cloak Using OpenCV | Python Project
- हिन्दी Invisibility Cloak Using OpenCV | Python Project
Rome | 2023-02-01
I was preparing for my coding interview, thanks for clarifying this - Invisibility Cloak Using OpenCV | Python Project in Python is not the simplest one. I just hope that will not emerge anymore
Shanghai | 2023-02-01
http Python module is always a bit confusing 😭 Invisibility Cloak Using OpenCV | Python Project is not the only problem I encountered. Checked yesterday, it works!
Shanghai | 2023-02-01
I was preparing for my coding interview, thanks for clarifying this - Invisibility Cloak Using OpenCV | Python Project in Python is not the simplest one. Checked yesterday, it works!