Most of you must have heard about the Harry Potter series. In that series, Harry potter used an invisibility cloak, that makes him invisible…! Are you wondering how they made invisible cloaks…..? That’s not as tough as rocket science to build this invisibility cloak. By using simple techniques in the OpenCV we can implement this invisible cloak. This concept is similar to the green mat used in movie shootings. After shooting, they add graphics in the place of a green mat to give the output.
In this implementation, we will learn how to create our own ‘Invisibility Cloak’ using simple computer vision techniques in OpenCV. Here we are using OpenCV because it provides the best-inbuilt libraries to implement this in a few steps.
Steps used for this implementation on Being invisible using OpenCV:-
- Defining the function
- Capturing and storing the background
- Capturing the live video using a webcam
- Displaying the output
So, now let’s begin with this interesting implementation.
import numpy as np
Step – 1 – Defining the function
def detect_func(frame, background):
hsv_image = cv2.cvtColor(frame, cv2.COLOR_RGB2HSV)
Using the below code snippet, we will assign the values of sensitivity, Hue, and range for blue color.
sensitivity = 20
H_Value = 20
light_blue = np.array([H_Value - sensitivity, 60, 60])
dark_blue = np.array([H_Value + sensitivity, 255, 255])
In the above implementation, we have used blue color values you can replace with another color just by changing those values
Using the below code snippet, we will use morphing techniques to create the mask for the blue-colored region.
create_mask = cv2.inRange(hsv_image, light_blue, dark_blue)
kernel_size = 15
kernel = np.ones((kernel_size, kernel_size), np.uint8)
closing = cv2.morphologyEx(create_mask, cv2.MORPH_CLOSE, kernel)
contours, _ = cv2.findContours(closing, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
sorted = sorted(contours, key=cv2.contourArea, reverse=True)[:5]
mask = cv2.fillPoly(np.zeros((500, 500, 3), dtype=np.uint8), pts =[cont_sorted], color=(255,255,255))
In the above implementation, the bigger sized kernel gives fewer gaps on the image
In the below code snippet, we will use the cv2.fillPoly function to merge the mask and background frames which displays the final output.
object_mask = cv2.fillPoly(frame, pts =[cont_sorted], color=(0,0,0))
background = np.bitwise_and(contour_mask, background)
final_img = cv2.bitwise_or(object_mask, background
To manipulate the video by using a mask, we have used bitwise_and and bitwise_or operations.
Step – 2 – Capturing and storing the background
cap = cv2.VideoCapture(0)
ret, background = cap.read()
background = cv2.resize(background, (500, 500))
Step – 3 – Capturing the live video and applying an invisible function to it.
fourcc = cv2.VideoWriter_fourcc(*'MP4V')
output = cv2.VideoWriter('output.mp4', fourcc, 30.0, (500,500))
ret, frame = cap.read()
frame = cv2.resize(frame, (500, 500))
image = detect_blue(frame, background)
Step – 4 – Displaying the results
if cv2.waitKey(1) & 0xFF == ord('q'):
In the above demonstration, we demonstrated how to create an invisible cloak by using very simple functions in OpenCV. It is a simple and interesting implementation that requires using a few lines of code.
Join Our Telegram Group. Be part of an engaging online community. Join Here.
Subscribe to our NewsletterGet the latest updates and relevant offers by sharing your email.
AI enthusiast, Currently working with Analytics India Magazine. I have experience of working with Machine learning, Deep learning real-time problems, Neural networks, structuring and machine learning projects. I am a Computer Vision researcher and I am Interested in solving real-time computer vision problems.