This is simply a program that uses color segmentation and a bit of image processing to replace the foreground object of interest with the background.
You can take a unicolor bedsheet and apply color segmentation on it. After that take a picture of the background and place the bedsheet over yourself to virtually disappear, or make some part of your body disappear.
- Python 3
- Numpy
- OpenCV (Python)
Visit: https://www.python.org/downloads/ and download the version suitable for your System
Open Command Prompt (Windows) or Terminal (Linux) and type in the following command:
pip install numpy
Visit: https://scipy.org/install.html for details.
Open Command Prompt (Windows) or Terminal (Linux) and type in the following command:
pip install opencv-python
Visit: https://pypi.org/project/opencv-python/ for details.
- Convert RGB to HSV and select a range, which is used to segment the color
- Create a mask with the segmented HSV color
- Apply Morphological Operations (Closing) to fill out holes in the mask. Make sure you use appropiate kernel/filter size. 10 works for my case.
- Find all the contours using OpenCV function and then arrange them in descending order in terms of Area.
- Select the largest contour and create a complete mask of that using polynomial fill function.
- With that mask, create object and background masks
- Bitwise OR the background and object masks in order to replace the object of interest with the background
- Repeat this for every frame
- Voila!
- Run the ColorSegmentation.py (While Running it, make sure nobody is infront of the webcam)
- An window showing the background image will pop up.
- Press any key while being on that window to start real time frame-by-frame Color segmentation.
The HSV values are calculated for the following cloth color. But you may have to re-define your values to make it work for other colors.
The algorithm is very similar in principle to green screening. But unlike green screening where we remove the background, in this application, we remove the foreground! We are using a red colored cloth as our cloak. Why red? Why not green? Sure, we could have used green, isn’t red the magician’s color? Jokes aside, colors like green or blue will also work fine with a little bit of tweaking.
The basic idea is given below:
- Capture and store the background frame.
- Detect the red colored cloth using color detection algorithm.
- Segment out the red colored cloth by generating a mask.
- Generate the final augmented output to create the magical effect.