Harry Potter’s Invisible Cloak : using Image Processing and Computer Vision

Devashi Choudhary
3 min readJul 5, 2020

It’s the cloak that Harry Potter uses to become invisible. Since childhood, I am potter head and fan of all his magic, but it’s not real. All these are graphics and visualization. In childhood, I always thought about what if I had the same invisibility-cloak. But, as I grow I realized in reality, it’s nothing like that, but in the virtual world, it’s possible using simple concepts of image processing and OpenCV. The project of the current project is to implement the invisible-cloak (Adrishya Jadui Choga).

Table of Content :

  1. Dependencies
  2. About Algorithm
  3. Conclusion

Dependencies

You need to install the following:

  1. Python
  2. OpenCV
  3. NumPy

You just need Red Cloth, you can use other color cloth also but required to change the HSV value. I will tell you what HSV is when we proceed further.

About Algorithm

  1. Capture and store the background frame.
  2. Detect the red colored cloth using color detection algorithm.
  3. Segment out the red colored cloth by generating a mask.
  4. Generate the final augmented output to create the magical effect.

Capture and store the background frame.

The key idea is to replace the current frame pixels corresponding to the cloth with the background pixels to generate the effect of an invisibility cloak. For this, we need to store a frame of the background (Line 1–4). As the background is static, the image captured is a bit dark compared to a multiple frames image. This is because the camera is just getting started on capturing frames and hence its parameters are not stable yet. Hence capturing multiple images of static background with a for loop and also averaging over multiple frames also reduces noise.

Detect the red colored cloth using color detection algorithm.

Since we are using a red color cloth to convert it into an invisibility cloak we will focus on detection of red color in the frame. We have an RGB (Red-Green-Blue) image and it is tempting to simply threshold the R channel and get our mask since the RGB values are highly sensitive to illumination. We have to transform the color space (Line 11–26) of our image from RGB to HSV (Hue — Saturation — Value).

What is HSV?

  1. Hue : This channel encodes color color information. Hue can be thought of an angle where 0 degree corresponds to the red color, 120 degrees corresponds to the green color, and 240 degrees corresponds to the blue color.
  2. Saturation : This channel encodes the intensity/purity of color. For example, pink is less saturated than red.
  3. Value : This channel encodes the brightness of color. Shading and gloss components of an image appear in this channel. Unlike RGB which is defined in relation to primary colors, HSV is defined in a way that is similar to how humans perceive color.

In our case, We use the range 0–10 and 170–180 to avoid detection of skin as red. High range of 120–255 for saturation is used because our cloth should be of highly saturated red color. The lower range of value is 70 so that we can detect red color in the wrinkles of the cloth as well.

mask1 = mask1 + mask2

Using the above line, we combine masks generated for both the red color range.

Segmenting out the detected red colored cloth

We generated a mask to determine the region in the frame corresponding to the detected color. We refine this mask and then use it for segmenting out the cloth from the frame (Line 27).

Generate the final augmented output to create the magical effect

Finally, we replace the pixel values of the detected red color region with corresponding pixel values of the static background and finally generate an augmented output which creates the magical effect, converting our cloth into an invisibility cloak (Line 30–32).

You are a magician now!!!

Conclusion

It is so easy and fun. In few lines of code, magic is happening. 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.

References

It’s always good to give references

  1. Invisible Cloak

The code is available at Devashi-Choudhary/Invisible-Cloak-or-Adrishya-Choga. For any questions or doubts, feel free to contact me directly at github.com/Devashi-Choudhary.

--

--

Devashi Choudhary

Data Scientist at NetApp | IIIT-DELHI | IET-DAVV | Machine Learner, Researcher and Part Time Photographer💯