Invisibility Cloak Using OpenCV | Python Project

| | | | | | | | | | | | | | | | | | | | | |

👻 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:



Carlo Krasiko

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

Manuel Richtgofen

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!

Javier Emmerson

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!

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically