Code360 powered by Coding Ninjas X Code360 powered by Coding Ninjas X
Table of contents
Working of Invisible Cloak
Prerequisites for the cloth are as follows:
Importing Libraries
Taking Video Feed Using Webcam
Capturing Background
Capturing the Video Feed and Setting the Values for the Cloak
Morphological Transformations to Remove Noise from the Cloth
Combining all the Masks
Key Takeaways
Last Updated: Mar 27, 2024

Invisible Cloak using OpenCV

Author soham Medewar
1 upvote
Master Python: Predicting weather forecasts
Ashwin Goyal
Product Manager @


Everyone must have watched the movie harry potter. Isn't it amazing to see the invisible cloak that Harry used to wear? Obviously, in practical life, it is not possible to make a cloak like that. But using the technology of computer vision, we can get the experience of having an invisible cloak. So, in this blog, we will be implementing a small model of an invisible cloak.

To understand this blog, you need to have basic knowledge of python and OpenCV. Before going on to the implementation part, let us understand how the model works.

Working of Invisible Cloak

This method is completely opposite to the green screening technique. We eliminate the background in green screening, but here we will remove the foreground frame.

  • Take a picture of the background and save it.
  • Using a color detection and segmentation method, find the defined color.
  • Develop a mask to segment out the defined colored part.
  • Create a magical effect by generating the final augmented output.
    Also read, Sampling and Quantization
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job


I will be performing all the implementation in anaconda’s jupyter notebook. You may use google colab as well (implementation of OpenCV functions in google colab will be different, so it is preferred to use a jupyter notebook).

System Prerequisites:

Prerequisites for the cloth are as follows:

  • Choose a single-colored cloth, and if the cloth's color is red, make sure your background isn't red. Because if that color is present in the background, it will cause issues.
  • We are using red color cloth in this project, but you can use any color by changing the values for the visibilities of the color, which is simple to do.

Importing Libraries

We will be implementing three libraries, i.e., numpy, time, and OpenCV. OpenCV allows us to access the webcam and also provides other features. Numpy and time will handle all the other operations.

import numpy as np
import cv2
import time

Taking Video Feed Using Webcam

To take the video feed, we will be using the OpenCV library. Also, we will use the time function so that we can capture the background for the first three seconds. The zero value in the VideoCapture function helps us to access the webcam that is inbuilt in your system (primary webcam).

cap = cv2.VideoCapture(0)
background = 0

Capturing Background

Here we will capture the background for the first three seconds. We will convert the captured video into HSV format. HSV stands for Hue Saturation Value.

for i in range(50):
    ret, background =

Capturing the Video Feed and Setting the Values for the Cloak

Now, we will concentrate on detecting the red region of the image in this step. Because RGB (red-blue-green) values are particularly sensitive to illumination, we'll convert them to HSV (hue-saturation-value). It's time to set the color range to identify the red color in the video after converting RGB to HSV. The color values for red are:

    ret, img =
    if not ret:
    hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
    lower_red = np.array([0,120,70])
    # values for red colour Cloth
    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)
    #Combining masks to view it in one frame.
    mask1 = mask1 +mask2

Morphological Transformations to Remove Noise from the Cloth

Morphological transformations: cv2.MORPH CLOSE simply removes extraneous details that aren't required, such as the small black holes that appear on the screen in this project. cv2.MORPH OPEN will remove the white region on the cloth's boundary that isn't required. 

(Continue the following code in the above discussed while loop)

    mask1 = cv2.morphologyEx(mask1, cv2.MORPH_OPEN, np.ones((3,3), np.uint8), iterations = 2)
    mask1 = cv2.morphologyEx(mask1, cv2.MORPH_DILATE, np.ones( (3,3), np.uint8), iterations = 1)
    mask2 =cv2.bitwise_not(mask1)

Combining all the Masks

In the final step, we will utilize a bitwise operation stored in res1 to combine the masks we've created and save them in the res1 variable.

We will use the cv2.addWeighted function to create a sharp image.

    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 Cloak',final_output)
    if cv2.waitKey(33) == ord('a'):

Note: Our model captures the background for the first three seconds. So, to get accurate results, try not to be in the frame for the first three seconds. Our model can work on only red-colored clothes. You can also do it for any other color by slightly changing the configuration of the model.

Read about Bitwise Operators in C here.


1. What is masking in image processing?

Masking is an image processing technique in which a small 'image piece' is defined and used to alter a bigger image. Many types of image processing, such as motion detection, edge detection, and noise reduction, rely on the process of masking.


2. What is the importance of morphological operations in image processing?

The goal of employing morphological techniques is to eliminate flaws in the image structure. The majority of the operations here combine two processes: dilatation and erosion.


3. What is OpenCV used for?

OpenCV is an excellent tool for image processing and computer vision. It's an open-source library that may be used for a variety of applications, including object tracking, face detection, landmark detection, and more.

4. What is image processing?

Image processing is a technique of performing several operations on an image in order to extract relevant information from it. It's a sort of signal processing in which the input is an image, and the output is either that image or its characteristics/features.

Key Takeaways

In this article, we have used simple masking techniques and Morphological Operations in this project to remove the colored cloth from the frame and replace it with the background.

Want to learn more about Machine Learning? Here is an excellent course that can guide you in learning. 

Happy Coding!

Next article
Vehicle Detection using OpenCV
Live masterclass