Masking an area in a video in OpenCV in Python— Harry Potter Invisible Cloak example

Venkatesh Chandra
Analytics Vidhya
Published in
4 min readFeb 15, 2020
Watch the video to see the action

Most of you are aware of The Cloak of Invisibility, the magical artefact which Dumbledore gave Harry Potter as a Christmas present anonymously. Well, you can buy it online now! But, let’s say you want to check how it looks on you before buying this product. What will you do?

If you are thinking about an answer, then you are on the right webpage! The trick is to use the library OpenCV in Python.

In this tutorial, we will learn how to create a masked video in Python for live cam streamed videos.

Before we begin, we need to install the OpenCV (Open Source Computer Vision) library which is built to help developers carry out tasks related to computer vision. We need to do a pip install for the OpenCV library.

install opencv-python

Let us now build the system in Python

Now, we have the required libraries installed. We will start implementing the codes as shown below. I have explained each block of code to help you understand what is happening in the background.

Feel free to skip to the end of this page to get the link to full code in Jupyter Notebook.

Step 1: Open Spyder

Step 2: Import the libraries

import cv2
import numpy as np
import time

Step 3: Reference your video to your webcam

video_capture = cv2.VideoCapture(0)

Step 4: Define the output of the video

fourcc = cv2.VideoWriter_fourcc(‘m’,’p’,’4',’v’)# note the lower case
frame_width = int(video_capture.get(3))
frame_height = int(video_capture.get(4))
out = cv2.VideoWriter(‘Harry_Potter.mp4’,fourcc , 10, (frame_width,frame_height), True)

Step 5: Specify wait time for your system to fire up

time.sleep(3)
background = 0

Step 6: Capture the usual environment. This code is necessary to make sure that the color you mask has details of the objects in the environment. The codes are explained in multiple blocks below:

Block 1:

while(video_capture.isOpened()):
ret, image = video_capture.read()
if not ret:
break
image = np.flip(image,axis=1)

# Change to HSV
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

Here, we process one frame at a time. The frame is extracted using cv2 library which captures the frame in BGR (Blue-Green-Red) colors, while the mask library uses HSV format. Hence we flip the color code of the frame.

Block 2:

#Create masks with coordinates to detect the color
lower_blue = np.array([94, 80, 2])
upper_blue = np.array([126, 255, 255])
mask_all = cv2.inRange(hsv,lower_blue,upper_blue)
mask_all = cv2.morphologyEx(mask_all, cv2.MORPH_OPEN, np.ones((3,3),np.uint8))
mask_all = cv2.morphologyEx(mask_all, cv2.MORPH_DILATE, np.ones((3,3),np.uint8))

The code in this block creates mask objects with coordinates to detect the color specified. Colors are specified in the HSV format in the range. In this example, we are specifying light blue color. For code snippets for other colors, see below.

Block 3:

#Hide the blue part away
mask2 = cv2.bitwise_not(mask_all)

streamA = cv2.bitwise_and(image,image,mask=mask2)
#Copy the masked area's original part
streamB = cv2.bitwise_and(background, background, mask = mask_all)

This block has codes to delete the detected colored area and add the layer of the background stream on it.

Block 4: Write the code to write the video on your hard drive

#Write the video in the file specified in the previous block
output = cv2.addWeighted(streamA,1,streamB,1,0)
out.write(output)
cv2.imshow("cloak_trick",output)
if cv2.waitKey(25) == 13:
break

Step 8: All captured videos must be released.

video_capture.release()
out.release()
cv2.destroyAllWindows()

Run the program in the command line

The next step is to save the file in .py format and run it in command line/Anaconda prompt.

I ran it in Anaconda prompt by first navigating to the folder using the command cd.

cd <folder path>

Run the python file

python filename.py

You will see a pop-up window with the video playing. Let the video capture the environment for about 45 seconds. After that, you can play around with blue colored objects/jackets/sweatshirts or even cloaks!

Wohoo! You have successfully built an invisible cloak replacement code in Python.

Try experimenting on videos with different objects and ideas and let me know how it goes in the comments section below.

If you face any issues, let me know in the comments section.

Additional code snippets for other colors

Red color
low_red = np.array([161, 155, 84]) high_red = np.array([179, 255, 255]) red_mask = cv2.inRange(hsv_frame, low_red, high_red) red = cv2.bitwise_and(frame, frame, mask=red_mask)
Blue color
low_blue = np.array([94, 80, 2]) high_blue = np.array([126, 255, 255]) blue_mask = cv2.inRange(hsv_frame, low_blue, high_blue) blue = cv2.bitwise_and(frame, frame, mask=blue_mask)
Green color
low_green = np.array([25, 52, 72]) high_green = np.array([102, 255, 255]) green_mask = cv2.inRange(hsv_frame, low_green, high_green) green = cv2.bitwise_and(frame, frame, mask=green_mask)
Every color except white
low = np.array([0, 42, 0]) high = np.array([179, 255, 255]) mask = cv2.inRange(hsv_frame, low, high) result = cv2.bitwise_and(frame, frame, mask=mask)

Codes

Related links

Real-Time Face Detection System in Python — Windows and macOS

Vehicle Detection in Python using OpenCV — Windows and macOS

Pedestrian Detection in Python using OpenCV — Windows and macOS

Saving output of object recognition in macOS

Where to find me 🤓

  1. Connect with me on LinkedIn/ GitHub / My website
  2. Feeling generous? Buy me a coffee here ☕️

--

--