Harry Potter Invisible Cloak using OpenCV — Python
By: Ishaan Gupta
Tags: Python, Harry Potter, Opencv, Invisible

Remember Harry Potter’s Invisible Cloak? In our childhood we all wanted to wear that cloak,don’t we? If Yes!! then in this post, we will build the same cloak which Harry Potter uses to become invisible. Yes, we are not building it in a real way but it is all about graphics trickery.

How it works (Algorithm)

This technique is opposite to the Green Screening. In green screening, we remove background but here we will remove the foreground frame.

Capture and store the background frame Detect the defined color using color detection and segmentation algorithm. Segment out the defined colored part by generating a mask. Generate the final augmented output to create a magical effect.

Let’s Start

You need the following libraries

If you get an error then u might have not installed them.

Capturing the video

VideoCapture object is used for reading a video file.Arguments in the brackets is the device camera index(0 for integrated web cam ,1 for external webcam and so on)

Allow the system to sleep for 3 seconds before the webcam starts

Time sleep function is used to add a delay.

time.sleep(t) means that when this line is executed then the next line of code will be executed after t seconds.Also define a variable named background which we’ll use later on in this program

Capturing and flipping background

Here we are capturing the background in the range of 45 and flipping(y axis) the background

Color detection and segmentation

Here we create a while loop to capture the frame and keep running it. capture.read() returns a boolean value(True/False). If the frame is read correctly, it is True. “ret” stores the return value from getting the camera frame, either true or false and “frame” stores the next frame in the camera.

“if not ret:” means the frame is not returned and hence we break the loop

For color conversion, we use the function cv2.cvtColor(image, arg) where arg determines the type of conversion. For BGR to HSV, we use the arg cv2.COLOR_BGR2HSV. “cap.read” captures the frame in BGR (Blue-Green-Red) colors, while we use HSV format for masking. Hence we flip the color code of the frame.

Creating a range for our cloth color(blue)

We are here creating a range for our cloth color(blue), where we define the lowest and the highest part of blue color (lowest and highest values of HSV). The “inRange” function returns a binary mask, in which white pixels (255) represent pixels that fall into the upper and lower limit range and black pixels (0) do not. We now generate a mask to determine the region in the frame corresponding to the detected blue color

Now we have to refine this mask and then use it for segmenting out our cloth from the “frame”.

These operations are called morphological operations.

cv2.morphologyEx() needs three inputs, one is our original image, operators and kernel size. “np.ones((3,3),np.uint8)” is the kernel where (3,3) is the size and “np.unit8” is the data type.

Here we replace the pixel values of the detected blue color region with corresponding pixel values of the static background region which gives us an augmented output creating the effect, converting our cloth into an invisibility cloak. To do this we use bitwise_and, bitwise_not operations.

Here we use the “addWeighted” object for unsharp masking. “cv2.imshow()” displays the image in a window. The first argument is the window name. The second argument is our image.

“if cv2.waitKey(25) == 27:” waitKey function displays the image for specified milliseconds. Otherwise, it won’t display the image.

27 is the keycode for key ‘Esc’. Press “Esc” to stop the video.

This releases the webcam, then closes all of the windows in imshow().

Congratulations. You are a WIZARD now!!