Invisibility Cloak Using OpenCV | Python Project

In this article, we will learn how to create your own Invisibility Cloak using simple computer vision techniques in OpenCV . We wrote this code in Python here because it provides a comprehensive and sufficient library to build this program.

Here we will create this magical experience using image processing techniques. To run this code, you need a video in video.mp4 mp4 named “ video.mp4 “. You must have the fabric of the same color, and it must not have a different color on it. We take the red cloth. If you change clothes, the code will remain the same, but with minor changes.

Why red? Is green my favorite?
Surely we could use green, isn`t the magician`s red? Jokes aside, colors like green or blue will also work well with small changes to the code. 
This method is the opposite of green screening . On the green screen, we are removing the background, but here we are removing the foreground frame. So let`s start our code.

Algorithm :

1. Capture and store the background frame [This will be done for some seconds]
2. Detect the red colored cloth using color detection and segmentation algorithm.
3. Segment out the red colored cloth by generating a mask. [used in code]
4. Generate the final augmented output to create a magical effect. [video.mp4]

Below is the code:

import cv2

import numpy as np

import time

  
# replace red pixels (or unwanted area) with
# background pixels to create invisibility function.

 
## 1. Hue: This channel encodes color information. Hue can be
# thought angle where 0 degrees corresponds to red
# 120 degrees corresponds to green and 240 degrees
# matches blue.

  
## 2. Saturation: this channel encodes the intensity / purity of the color.
# For example, pink is less saturated, than red.

 
## 3. Meaning: This channel encodes the brightness of the color .
# Shading and gloss components of the image appear in this
# video image reader

 
# to check the cv2 version

print (cv2 .__ version__) 

 
# taking video.mp4 as input
# Make your own path according to your needs

capture_video = cv2.VideoCapture ( "video.mp4" )

 
# let the camera warm up

time.sleep ( 1

count = 0  

background = 0  

 
# capture the background in the 60 range
# you have there should be a video that has a few seconds
# outlined with a background frame so that
# can easily save background image

for i in range ( 60 ):

return_val, background = capture_video.read ()

if return_val = = False :

continue  

 

background = np.flip (background, axis = 1 ) # frame flip

 
# we read from the video

while (capture_video.isOpened ()):

return_val, img = capture_video.read ()

if not return_val:

break  

count = count + 1

  img = np.flip (img, axis = 1 )

 

# convert image - BGR to HSV

# how we focused on red detection

 

# convert BGR to HSV for better

# detection or you can convert it to gray

hsv = cv 2.cvtColor (img, cv2.COLOR_BGR2HSV) 

 

# ------------------------------------- BLOCK ----------- ----------------- #

# ranges should be chosen carefully

# setting the lower and upper ranges for mask1

lower_red = np.array ([ 100 , 40 , 40 ]) 

upper_red = np.array ([ 100 , 255 , 255 ])

mask1 = cv2.inRange (hsv, lower_red, upper_red)

# set low and high range for mask2

lower_red = np.array ([ 155 , 40 , 40 ])

upper_red = np.array ([ 180 ,  255 , 255 ])

mask2 = cv2 .inRange (hsv, lower_red, upper_red)

# -------- ----------------------------------------- --------- ------------ #

 

# the above code block can be replaced with

  # different code depending on the color of your fabric

mask1 = mask1 + mask2

 

  # Refine the mask to match the detected red

  mask1 = cv2.morphologyEx (mask1, cv2.MORPH_OPEN, np.ones (( 3 , 3 ),

np.uint8), iterations = 2 )

mask1 = cv2.dilate (mask1, np.ones (( 3 , 3 ), np. uint8), iterations = 1 )

  mask2 = cv2.bitwise_not (mask1)

 

# Generate the final result

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 MAN" , final_output)

k = cv2.waitKey ( 10 )

  if k = = 27 :

break

Exit :

You can check the source code in the project github repository for video input and more details —  here

Link: http://datasciencenthusiast.com/?p=71