Invisible Cloak Using OpenCV

  • Ripul Agrawal
  • Jul 31, 2020
  • Artificial Intelligence
  • Deep Learning
Invisible Cloak Using OpenCV title banner

“Let us step into the night and pursue that flighty temptress, adventure.” - Harry Potter Movie




In the last few blogs, we have discussed OpenCV with some of its functions and applications in different fields including Computer Vision, facial recognition, live motion detection, object detection, image processing, tracking of 3D objects, and many more.


Other than that, we have also have covered how to use an external camera device with OpenCV and stream results on your laptop/PCs with the help of third-party applications like IP webcam in case of integration with an android camera. ( Link to that blog)


As already discussed, all the OpenCV applications, valid for web cameras, can be implemented with the external camera devices too, like, a low budget home surveillance system without using CCTV cameras.


Moving forward with this now, its time to work on some fun projects with the help of OpenCV, i.e., so we are considering here the “Invisible Cloak” like “Harry Potter had which he got from Dumbledore at Hogwarts.”


It’s not anything new to the Harry Potter fans, but this is new for the people who don’t have an idea about it, so it’s a technique to render physical objects invisible. In the below image you can see how Potter’s body is not visible, its just a miracle of the cloak.

 This image demonstrates the glimpse of Harry Potter's invisible cloak

A glimpse of Potter’s Invisible Cloak

You will find more such a demo here.


Cloak with OpenCV


Inspired by Harry Potter, I felt of creating one for me, just for fun, so I just dived into it and came up with the same using OpenCV.  Follow this to create your own invisible cloak.


Obviously, it will not be any real but will be graphical. It will be based on the image processing approach i.e. color detection and masking which means it will work for some particular colors as per our choice. 


For a brief, let’s suppose you have chosen the red color, then after you run the code. After two seconds bring any red color cloth and hide your self in that, and then you will not be visible on the screen. 


We will be using both the web-camera and android camera for the testing of Invisible Cloak.


  1. Get Started with Coding

  • Import the required libraries

    • OpenCV will be used for image processing part i.e. to start the camera, read each frame in code, color detection, color masking, and streaming final results

    • Numpy will be used to deal with arrays as images in python are a series of Numpy array

    • Time 

This image demonstrates the code to import libraries required to develop invisible cloak.

Import the libraries

  • Initialize video Capture object from a web camera and suspends execution for 2 seconds.

This image demonstrates the code to initialize video capture objects to stream using a web camera.

Video capture object

  • Initialize background to be visible in place of the invisible object whenever any red color ( or any other color) object will come in front of the camera. 

In the beginning, it will be equals to 0 but as soon as the camera will start it will record the frame for 30 seconds and then stored the same in the background variable.

This image demonstrates the code to initialize background to first frame for 30 seconds.

Record the background 

  • Initialize while loop to read each frame through the web camera till any keypress, in this ESC  key of the keyboard. 

  • Next, convert the image from RGB format into HSV format (hue-saturation-value) as RGB is highly sensitive to illumination.

    • Hue contains the color information in degrees where,

      •  0 degree - Red color

      • 120 degree- green color

      • 240 degree - white color

    • Saturation used to encode the intensity of color i.e. amount of gray in a particular color.

    • Value describes the brightness of the color.

This image demonstrates the code to read the frame by web-camera and conversion to HSV format

Get the HSV format of each frame

  • Color Detection: Now, its time to detect the red color from the frames for that first need to define the range both the lower bound and upper bound to like as below. Repeat the same step for mask 2 and combined both of the masks with OR operations.

This image demonstrates the code to create masks of red color

Create a mask for the red color

In the above code, wherever red color will find it will apply thresholding so as change its value or in other words will save the portion of red color in variables i.e. mask1 & mask2


  • Color Segmentation: Now we have retrieved the mask of the red color and now will segment both of those using morphology and dilation.

This image demonstrates the code to dilate the frame.

Dilation of the image

  • Create an inverted mask to segment out the particular out of it using bitwise_not().

  • Generate the final output by combining the background captured and one with the masked red color following by the help of bitwise_and() operations.

This image demonstrates the code to produce final output of invisible cloak by bitwise_and of background and image with the color mask.

Final output by color masking

Here you can check the output of my invisible cloak that, where I have used red pillow instead of red cloth.

This image demonstrates the invisible Cloak developed for a web camera.

My Invisible Cloak

Invisible Cloak using Android Camera


Above we have created invisible cloak using web-camera and it worked fine. What if you want to use your android phone for the same. It will be simple and just similar to the case that we have discussed. In the previous post, we have covered, how to access the android camera in OpenCV using the third party applications. So let’s combine both of them as below.


Get Start with Coding

  • Import the required packages,

    • urllib to open internet websites by using their URLs. Here it will be used to access the camera with their IPs.

    • OpenCV will be used for image processing.

This image demonstrates the code to import the libraries to access android camera using OpenCV

Import the required package to work with android camera

  • Next, initialize the for loop to start the camera and then access the live image from the URL (captured by android phone) following by the conversion into NumPy using cv2.imdecode().

  • After that initialize the background with the frames for 30 seconds as we did above.

This image demonstrates the code to capture the background that will be required to replace the invisible object in the output frame.

Capture background for 30 seconds


  • Now use a while loop to start the camera for creating invisible cloak.

  • Use Urllib to access the image from the android and convert it into a NumPy format. Once done with this step, now repeat the same steps that we have used for web-camera as the conversion of an image into HSV format, color detection, color segmentation, and then the final output of invisible cloak.


This image demonstrates the code to create an invisible cloak for android phones following by color detection and color segmentation

code for an invisible cloak with android

In the following image, you can see the output of this invisible cloak as a real screen vs cloak screen.

This image demonstrates the invisible Cloak for android phones.

Invisible Cloak for android camera




OpenCV is a vast field and has many practical applications available in the industry like object detection, motion detection, facial recognition, and nowadays many organizations are using the same in the name of AI and Computer Vision for developing surveillance systems to keep track of criminal activities. 


Another reason for its useful its execution speed and efficiency as compared to other image processing libraries. Once you dive into it, will open the way for many innovative ideas and fun projects like you can control a racing game with gloves in your hand or else you can be invisible in your online video classrooms and many more.


This blog is all about the developing Invisible Cloak that our childhood hero Harry Potter used in Hogwarts acquired from Dumbledore. It will not be the real cloak but some graphical tricks with OpenCV and Python to make things invisible in front of the camera based on color detection and color segmentation techniques. The same has been done for an android camera using OpenCV and Python.


Ripul Agrawal

Computer Vision and Data Science enthusiast with expertise in Python and its libraries like Open CV, pandas, Numpy, matplotlib, sklearn, Keras, nltk, etc. I have worked on projects in • Machine Learning, • Natural Language Processing, • Deep Learning, • Image Processing. Apart from this, I have practiced, • Android Development with Java and Flutter (Dart), Firebase • Gmail add-ons development with Google App Script

Trending blogs

  • What is the OpenAI GPT-3?

  • Introduction to Time Series Analysis: Time-Series Forecasting Machine learning Methods & Models

  • How is Artificial Intelligence (AI) Making TikTok Tick?

  • 6 Major Branches of Artificial Intelligence (AI)

  • 7 Types of Activation Functions in Neural Network

  • 7 types of regression techniques you should know in Machine Learning

  • Reliance Jio and JioMart: Marketing Strategy, SWOT Analysis, and Working Ecosystem

  • Top 10 Big Data Technologies in 2020

  • Introduction to Logistic Regression - Sigmoid Function, Code Explanation

  • What is K-means Clustering in Machine Learning?

Write a BLOG