Have you ever imagined if you could become INVISIBLE?

You must have heard about “The Invisibility Cloak”, the one used by Harry Potter and his wizard friends and it’s been one of the marvels in science fiction and fantasy. These magical things in Harry Potter are actually becoming real.

Do you want to try this magical cloak on yourself? Yes, it is very much possible – Thanks to this interesting technology.

GET INSIDE TECHIE !!

Everything can be broken down and converted into an algorithm and an easy to follow process-flow. Here we work with the algorithm similar to the principle of “GREEN SCREENING” by removing foreground target color. Lets get deeper and understand more.!!!

Infographic titled "Magic of OpenCV — colour detection and segmentation" listing four steps: capture and store the background frame, detect the target colour using colour detection, segment the target colour by segmenting the mask, and view the magical effect on the consolidated output.

The first and foremost step to becoming invisible using a cloak is to store pure background frame. By using this background frame we can replace the target color.

Python
import cv2
import numpy as np

cap = cv2.VideoCapture(0)

# Capture the background
while True:
    ret, frame = cap.read()
    f = cv2.flip(frame, 1)
    cv2.imshow("result", f)

    if cv2.waitKey(1) & 0xFF == ord("k"):
        break

cap.release()
cv2.destroyAllWindows()

And What’s next ???

Color detection … Do you know why?

We have to transform the color space from RGB(Red, Blue, Green) To HSV( Hue-Saturation-Value) because RGB has high sensitivity to illumination.

Wait, what is HSV?? and why is it important ??

Hue – Angle of color (00-Red,1200-Green,2400-Blue)

Saturation – Intensity/Purity of color

Value – Brightness of color (shading and gloss component of an image,appearance)

Using this we create a mask with respect to the colour of our clothes. With this, we segment the targeted color. Now that color detection is done, we can change the H-S-V range and use other mono-coloured clothes too.

Segmentation

Final Output

Waited long to see the magical effect of the invisible cloak?

One more step to go!

Here, replace the pixel values of detected color with pixel values of static background using bit-wise-and operation after that you can view the augmented output.

Python
cap = cv2.VideoCapture(0)

# Actual feed
while True:
    ret, frame = cap.read()
    frame = cv2.flip(frame, 1)

    frame1 = np.ones((2, 2), np.uint8)
    hsv_frame = cv2.cvtColor(frame, cv2.COLOR_RGB2HSV)

    # Created an HSV range by trial and error
    lhsv = np.array([10, 100, 150])
    uhsv = np.array([255, 255, 255])

    # Create your mask color based on HSV range
    mask = cv2.inRange(hsv_frame, lhsv, uhsv)

    # Apply morphology operation on the mask
    close_mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, frame1)

    mask_inverse = cv2.bitwise_not(close_mask)

    # Create two results
    # 1. Extract the first feed around the cloth
    result = cv2.bitwise_and(f, f, mask=close_mask)

    # 2. Extract the current camera feed
    result1 = cv2.bitwise_and(frame, frame, mask=mask_inverse)

    # Add both results together
    r = cv2.add(result, result1)

    # Display the mask and the final output
    cv2.imshow("frame", mask)
    # cv2.imshow("mask_inv", close_mask)

    cv2.imshow("result", r)

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

cap.release()
cv2.destroyAllWindows()

Whoa! I’m invisible now with the “Invisible cloak”

Go ahead, experiment and enjoy this crazy experience, courtesy the Innovation Lab at TEKCLAN !!