Become a Magician using OpenCv !!

Hey Peeps !!!!

In this new blog, something interesting awaits for you all !!!!!

What’s it all about ????

Let’s write code for becoming a magician using OpenCV !!!!

Pardon for incomplete invisible effect ! Due to bad lightning its like this !! 🙂

First, we need to be clear about the libraries that we are about to use for becoming a magician, we gonna use “OpenCV” library.

We start by importing the necessary packages:

import cv2
import numpy as np
import time

“cv2” is the opencv library we are going to use and numpy is another python library used mainly for numerical calculations and time library is used to defining the time.

After importing, it’s now time to read the webcam and store the background as images in range of 60 for making the invisible effect.

#reading the webcam 
cap = cv2.VideoCapture(0)
#allowing the system to sleep for 3 seconds before the webcam fires up !
count = 0
background = 0
#Capturing the background in range of 60
for i in range(60):
    ret,background =
background = np.flip(background,axis=1)

When the webcam is fired, we read the images from the webcam and convert the RGB images to Hue-Saturation-Value (HSV) .

  1. Hue – Colours in their purest form.
  2. Saturation – intensity or chroma component of the colour.
  3. Value – relative lightness or darkness of a colour.

Then masking range for the colour cloak we are about to use is defined.

    ret, img =
    if not ret:
    img = np.flip(img,axis=1)
    # Converting the color space from BGR to HSV
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

    # Generating mask to detect red color
    lower_red = np.array([110,50,50])
    upper_red = np.array([120,255,255])
    mask1 = cv2.inRange(hsv,lower_red,upper_red)

    lower_red = np.array([120,50,50])
    upper_red = np.array([130,255,255])
    mask2 = cv2.inRange(hsv,lower_red,upper_red)

    mask1 = mask1+mask2

After this, we use this mask to detect the colour of the cloak we are using and mask the colour of the cloak by making the background visible, so that the invisible effect is created.

Voilllaaaaaa !!!!! We became a magician !!!


Full code for this project –

One Reply to “Become a Magician using OpenCv !!”

Leave a Reply

Your email address will not be published. Required fields are marked *