Abracadabra to make objects disappear using OpenCV in python
A simple Python code which makes objects disappear using Open Source Computer Vision
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,
import cv2
import time
import numpy as np
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.
fourcc = cv2.VideoWriter_fourcc(*’XVID’)
out = cv2.VideoWriter(‘output.avi’,fourcc,20.0, (640,480))
This piece of code is to display the captured and forged video with the specified screen size and desired output video
cap = cv2.VideoCapture(0)
This is to capture the scene, background using OpenCV through the system’s webcam
time.sleep(3)
count = 0
background = 0
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
for i in range(60):
ret,background = cap.read()
background = np.flip(background,axis=1)
We make our program to read the background image of the scene and store for flipping or forging later
while(cap.isOpened()):
ret, img = cap.read()
if not ret:
break
count+=1
img = np.flip(img,axis=1)
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
lower_red = np.array([0,120,50])
upper_red = np.array([10,255,255])
mask1 = cv2.inRange(hsv,lower_red,upper_red)
lower_red = np.array([170,120,70])
upper_red = np.array([180,255,255])
mask2 = cv2.inRange(hsv,lower_red,upper_red)mask1 = mask1+mask2
mask1 = cv2.morphologyEx(mask1, cv2.MORPH_OPEN, np.ones((3,3),np.uint8))
mask1 = cv2.morphologyEx(mask1, cv2.MORPH_DILATE, np.ones((3,3),np.uint8))
mask2 = cv2.bitwise_not(mask1)
res1 = cv2.bitwise_and(img,img,mask=mask2)
res2 = cv2.bitwise_and(background, background, mask = mask1)
finalOutput = cv2.addWeighted(res1,1,res2,1,0)
out.write(finalOutput)
cv2.imshow(“magic”,finalOutput)
cv2.waitKey(1)
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
cap.release()
out.release()
cv2.destroyAllWindows()
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