Amazing applications of Image Processing
Virtual Switch with OpenCV
Using hand gestures as a switch
A switch is simply a button to toggle the current state of a thing. So what we will do here is to implement a switch and call the desired function whenever the switch is pressed.
The potential applications of this are many like we can use it to change the image btw black and white and color, or controlling a media player when a particular switch is pressed, etc.
TL;DR — Full code is here.
Idea -
So the idea behind the working of the switch will be, whenever we will take our hand to the selected region, it will cause noise in that region due to a change in pixels. So we will determine the total no. of (changed pixels/amount of noise) and activate the switch if it exceeds a certain threshold.
Algorithm -
To implement the switch we will use Background Subtraction, which is a technique used to remove the background and to detect only the foreground region. In simple words, it works by calculating the difference between the current frame and the average of previous frames. If there is a large change in values between the two, then we can infer that motion has taken place within that region.
To know more about Background Subtraction have a quick look here.
Working -
The full code is here.
The implementation of Virtual Switch is quite simple, basically, we will use -
- BackgroundSubtractorMOG2 which is already implemented in OpenCV to detect the movements (changes within a region).
- It will give us the number of noise/pixels changed within a region. (The white pixels within the box in gif.)
- If the number of pixels changed is more than a threshold, then we will call the function to perform the desired action. (Here the change color function.)
- The function can be mapped to any action like converting the image to black and white or to put a text.
Code -
This line will initialize the background subtractor object. Set detectShadows
to True, if you want to remove shadows from detection.
backgroundobject = cv2.createBackgroundSubtractorMOG2(detectShadows = False)
Applying the object to a particular region of the frame. This will give the motion within that region.
x, y, w, h = bbox
region = frame[y:y+h, x:x+w]
fgmask = backgroundobject.apply(region)
Now we will find the number of pixels changed by finding all the pixel equal to 255 in the mask. If it is greater than a threshold, it can be used to call a function.
switch_thresh = np.sum(fgmask==255)
if switch_thresh > ACTION_THRESHOLD:
do_action()
Full Code -
Keep In Mind-
- Make sure that the video quality is decent and there is proper light. As it works by calculating noise, so low image quality or low light may cause some anomaly.
- You can play with the history parameter of the background subtractor and change the threshold of the switch according to your needs.
So this was my project on Virtual Switch with OpenCV. The concept is pretty simple and how you use it depends on your imagination.
If you want to see an application of this for playing Tekken then have a look at this project on Playing Tekken by Body Gestures.
If you liked my work, consider supporting it by giving this a clap (as much as 50 times).
Thanks for reading.