Invisibility Cloak -using OpenCV

If you are a Harry Potter fan , you would know what an Invisibility Cloak is. Yes! It’s the cloak that makes Harry Potter invisible. We will make this happen with few line of python code in OpenCV.

How it works

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
  2. Detect the defined color using color detection and segmentation algorithm.
  3. Segment out the defined colored part by generating a mask.
  4. Generate the final augmented output to create a magical effect. [ output.avi ]

Let’s Start

You need the following libraries

import cv2
import time
import numpy as np

You will need to update the HSV value of the colour you choose accordingly in the code. Like if you have red cloth or blue cloth that you want to make invisible than you will need to set value accordingly.

Well, “What is HSV?”

Hue Saturation Value

H : Hue

Hue is the colour portion of the model, expressed as a number from 0 to 360 degrees:

  • Red falls between 0 and 60 degrees.
  • Yellow falls between 61 and 120 degrees.
  • Green falls between 121–180 degrees.
  • Cyan falls between 181–240 degrees.
  • Blue falls between 241–300 degrees.
  • Magenta falls between 301–360 degrees.

S : Saturation

Saturation describes the amount of grey in a particular colour, from 0 to 100 percent. Reducing this component toward zero introduces more grey and produces a faded effect. Sometimes, saturation appears as a range from just 0–1, where 0 is grey, and 1 is a primary colour.

V : Value (Brightness)

Value works in conjunction with saturation and describes the brightness or intensity of the colour, from 0–100 percent, where 0 is completely black, and 100 is the brightest and reveals the most colour.

Recording and caching the background for each frame.

We are replacing the red colored pixels with the background pixels to create the invisible effect in the video. For doing this, we have to store the background image for each frame.

##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)

Detecting the define color portion In each frame

In this step, we will put our focus on detecting the red part in the image. We will convert the RGB (red-blue-green) to HSV(hue-saturation-value) because RGB values are highly sensitive to illumination. After the conversion of RGB to HSV, it is time to specify the range of color to detect red color in the video. Below color values are for red color

## 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, 125, 50])
upper_red = np.array([10, 255,255])
mask1 = cv2.inRange(hsv, lower_red, upper_red)
lower_red = np.array([170, 120, 70])
upper_red = np.array([180, 255, 255])
mask2 = cv2.inRange(hsv, lower_red, upper_red)
mask1 = mask1 + mask2

Replacing the red portion with a mask image in each frame

Now, we have a red part of the video in the ‘mask’ image, we will segment the mask part from the frames. We will do a morphology open and dilation for that.

## Open and Dilate the mask image
mask1 = cv2.morphologyEx(mask1, cv2.MORPH_OPEN, np.ones((3, 3), np.uint8))
mask1 = cv2.morphologyEx(mask1, cv2.MORPH_DILATE, np.ones((3, 3), np.uint8))
## Create an inverted mask to segment out the red color from the frame
mask2 = cv2.bitwise_not(mask1)
## Segment the red color part out of the frame using bitwise and with the inverted mask
res1 = cv2.bitwise_and(img, img, mask=mask2)
## Create image showing static background frame pixels only for the masked region
res2 = cv2.bitwise_and(background, background, mask=mask1)

Full Code

Github Repo Link —


Once you run the code wait for a few seconds, let the webcam capture the background image then you can come inside the frame with your magical Cloak and be invisible ; )

Hurray!! You are a magician now ;)

Tech Lead and Founder at @XenonStudio. #Mobile #AI. Visit :-