MITB Banner

My Fun AI Project – How To Be Invisible?

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.

Share

invisible using opencv

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.

Importing libraries 

import numpy as np

import cv2

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[0]], 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[0]], color=(0,0,0))

    background = np.bitwise_and(contour_mask, background)

    final_img = cv2.bitwise_or(object_mask, background

    return final_img

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

cv2.imshow('back_img', background)

cv2.waitKey(0)

cv2.destroyAllWindows()

invisible using opencv

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

while(True):

    ret, frame = cap.read()

    frame = cv2.resize(frame, (500, 500))

    image = detect_blue(frame, background)

    output.write(image)

Step – 4 – Displaying the results 

cv2.imshow('Img',image)

    if cv2.waitKey(1) & 0xFF == ord('q'):

cap.release()

output.release()

cv2.destroyAllWindows()

Final Output:-

Video Link 

Conclusion

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. 

Share
Picture of Prudhvi varma

Prudhvi varma

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.
Related Posts

CORPORATE TRAINING PROGRAMS ON GENERATIVE AI

Generative AI Skilling for Enterprises

Our customized corporate training program on Generative AI provides a unique opportunity to empower, retain, and advance your talent.

Upcoming Large format Conference

May 30 and 31, 2024 | 📍 Bangalore, India

Download the easiest way to
stay informed

Subscribe to The Belamy: Our Weekly Newsletter

Biggest AI stories, delivered to your inbox every week.

AI Forum for India

Our Discord Community for AI Ecosystem, In collaboration with NVIDIA. 

Flagship Events

Rising 2024 | DE&I in Tech Summit

April 4 and 5, 2024 | 📍 Hilton Convention Center, Manyata Tech Park, Bangalore

MachineCon GCC Summit 2024

June 28 2024 | 📍Bangalore, India

MachineCon USA 2024

26 July 2024 | 583 Park Avenue, New York

Cypher India 2024

September 25-27, 2024 | 📍Bangalore, India

Cypher USA 2024

Nov 21-22 2024 | 📍Santa Clara Convention Center, California, USA

Data Engineering Summit 2024

May 30 and 31, 2024 | 📍 Bangalore, India

Subscribe to Our Newsletter

The Belamy, our weekly Newsletter is a rage. Just enter your email below.