Invisible Cloak : In Python using Open CV

Are you a Harry Potter fan like me?

If yes, then let’s get seated to go on a trip to Hogwarts…Keep reading and you will turn out to be a magician!

Every great magic trick consists of three parts or acts.

The first part is called “The Pledge”. The magician shows you something ordinary: a deck of cards, a bird or a man. He shows you this object. Perhaps he asks you to inspect it to see if it is indeed real, unaltered, normal. But of course… it probably isn’t.

The Pledge:

Environment Setup details

  1. Install Anaconda
  2. Jupyter Notebook
  3. 3.Open CV ( What is it ? well , open source computer vision and machine learning s library.)
  4. Numpy (is a library for the Python programming language, adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays)

The second act is called “The Turn”. The magician takes the ordinary something and makes it do something extraordinary. Now you’re looking for the secret… but you won’t find it, because of course you’re not really looking. You don’t really want to know. You want to be fooled. But you wouldn’t clap yet.

Setup Steps :

Step 1: Create a Virtual Environment

conda create --name opencv-env python=3.6

activate opencv-env

Step 2: Install OpenCV and other packages.

pip install numpy scipy matplotlib scikit-learn jupyter

Test your Installation :

import cv2


Every magic trick has a third act, the hardest part, the part we callThe Prestige


Now how are actually going to create an Invisibility Cloak?

This technique is opposite to the Green Screening. In green screening, we remove background but here we will remove the foreground frame.

  1. Capture and store the background frame [ This will be done for some seconds ]
  2. Detect the red coloured cloth using colour detection and segmentation algorithm.
  3. Segment out the red coloured cloth by generating a mask. [ used in code ]
  4. Generate the final augmented output to create a magical effect.
import cv2
import time
import numpy as np

##reading from the webcam
cap = cv2.VideoCapture(0)

## Allow the system to sleep for 3 seconds before the webcam starts
count = 0
background = 0

## Capture the background in range of 60
for i in range(60):
ret, background =
background = np.flip(background, axis=1)

## Read every frame from the webcam, until the camera is open
while (cap.isOpened()):
ret, img =
if not ret:
count += 1
img = np.flip(img, axis=1)

## Convert the color space from BGR to HSV
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

## Generat masks to detect red color

lower_red = np.array([0, 120, 50])
upper_red = np.array([10, 255,255])
mask1 = cv2.inRange(hsv, lower_red, upper_red)

lower_red = np.array([100, 40, 40])
upper_red = np.array([100, 255, 255])
mask1 = cv2.inRange(hsv, lower_red, upper_red)
# setting the lower and upper 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 block of code could be replaced with
# some other code depending upon the color of your cloth
mask1 = mask1 + mask2

# Refining the mask corresponding to the detected red color
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)

# Generating the final output
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(1)
if k == 27:


#colors code

#skin color Values
#lower_red = np.array([0, 0, 70])
#upper_red = np.array([100, 255,255])
# mask1 = cv2.inRange(hsv, lower_red, upper_red

Here, you can also refer it on my github

They’ll beg you and they’ll flatter you for the secret, but as soon as you give it up… you’ll be nothing to them

The secret impresses no one. The trick you use it for is everything

Developer and part time tech blogger