Abracadabra to make objects disappear using OpenCV in python

A simple Python code which makes objects disappear using Open Source Computer Vision

Diva Coders
Bloggers Bay
4 min readOct 12, 2020

--

Photo by Aron Visuals on Unsplash (A picture where normal hiding of moon by trees is portrayed as a visual trick as the disappearance of the moon)

Magic is a mysterious form of art that deals with distorting perceptions and creating visual illusions and sometimes even controlling the human mind. Mostly magic will have a hidden logic where the magicians will try to make us accept the factor of change occurred using their quick and witty tricks.

So as engineers are all-rounders and are capable to make a mark in all domains, we tried to play a small trick with the use of technology

Our goal is to do magic which makes the object present on the screen disappear at a magical spell

Here we proceed with the use of Open source Computer Vision (OpenCV) a Python Image Processing library

What is OpenCV?

OpenCV is expanded as Open Source Computer Vision which aims at real-time image processing through computer’s perspective OpenCV is a combination of various other python libraries like NumPy and hereby works on mathematical computations made on images that are captured.

OpenCV is deployed here for making objects disappear like this:

Amazed???

Now we shall ponder into the backstage of the show,

Working:

What exactly happens here?

If you have observed keenly, you would have noticed we have allowed the OpenCV camera to observe the background for at least 10 seconds, we do this for the Application to capture the background image and then we place a ball in the scene, our magical spell here would be a red cloth once I cover the object using a red cloth the background image captured earlier is forged with the screen where ever red color is seen by the camera.

Now, How it happens?

Let us indulge technically into this,

All the necessary packages and libraries are imported, cv2 is for Computer Vision, Time is for managing the video capturing and code running and Numpy is Numerical Python which is used to give mathematical computations to the OpenCV module.

This piece of code is to display the captured and forged video with the specified screen size and desired output video

This is to capture the scene, background using OpenCV through the system’s webcam

We allow the application to start its camera in 3 seconds for making our setup ready and initialize count and background values to be 0

We make our program to read the background image of the scene and store for flipping or forging later

These lines are the heart of the program,

While condition specifies all the images from the webcam are to be read and store till the camera is turned off

We then convert the BGR format (Blue Green Red corresponding order of areas is reversed from RGB) to HSV format (HSV means Hue-Saturation-Value, it stores color numberings in cylindrical representation of RGB color points. Hue value varies from 0–179, Saturation value varies from 0–255 and Value value varies from 0–255 depending upon various colors observed)

Now we create masks or boundary points for red color in general by setting the HSV and assigning it for masking

We do segmentation for cutting out the region in red color from the video which is streamed and we create a inverted mask for this.

We work on bitwise or pixel wise morphing the input red image with previously captured background image

After inverting we write it to the output stream of video for the display

Finally we close all windows on execution of the code

Full working of the source code:

Yay!!! We accomplished our trick… Now one more magical spell left…

“Abracadabra… turn all my readers into magicians !!!”

Is that even possible?

Yes, open your Spyder console which comes with Anaconda Navigator and install the necessary packages with “PIP”

Run the source code in your Ipython Console or as a Python file

Boom all of you who have tried this code have turned into magicians…

PS: Again technically my spell worked…

You can find the full source code at:

https://github.com/PradeepaK1/Abracadabra-to-make-objects-disappear-using-opencv-in-python

Contributors:

Anjana M P — https://anjana21it.wixsite.com/mysite

Pradeepa K — https://ptljkpd.wixsite.com/pradeepa

--

--