Invisibility cloak— using OpenCV

Soumya Patil
3 min readOct 14, 2019

--

Have you always wondered how life would be if you had the superpower of being invisible, here’s how you can experience one — obviously this is not possible in real life but can be achieved with few lines of code in OpenCV.

If you are a Harry Potter fan like me, you would know what an Invisibility Cloak is. Yes! It’s the cloak which Harry Potter uses to become invisible.

In this post, you will learn how to create your own “Invisible Cloak”. All you need is a Red Cloth and a couple of installations to be done in your PC.

Let’s take a look at it

Environment Setup required :

  1. Jupyter Notebook
  2. Python — OpenCV, numpy

“Why just Red? Can’t I use any other colour?”

Of course, you can :)

You just need to update the HSV value of the colour you choose accordingly in the code.

Well, “What is HSV?”

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.

Installation Steps :

Step 1: Install Anaconda (a python distribution)

Step 2: Create a Virtual Environment

conda create --name opencv-env python=3.6

activate opencv-env

Step 3: Install OpenCV and other packages.

pip install numpy scipy matplotlib scikit-learn jupyter

Test your Installation :

import cv2

cv2.__version__

Algorithm:

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. [ harry.avi ]

import numpy as np
import cv2
import timecap=cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('harry.avi' , fourcc, 20.0, (640,480))
time.sleep(2)
background = 0#capturing background
for i in range(30):
ret, background = cap.read()#capturing image
while(cap.isOpened()):
ret, img = cap.read()

if not ret:
break

hsv=cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
lower_red = np.array([0,120,70])
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 #OR
mask1=cv2.morphologyEx(mask1, cv2.MORPH_OPEN ,np.ones((3,3) , np.uint8) , iterations=2)

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

mask2 = cv2.bitwise_not(mask1)

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('Harry' , final_output)
k=cv2.waitKey(10)
if k==27:
break

cap.release()
cv2.destroyAllWindows()

Summary:

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

--

--

Soumya Patil
Soumya Patil

Responses (1)