Implementation
I will be performing all the implementation in anaconda’s jupyter notebook. You may use google colab as well (implementation of OpenCV functions in google colab will be different, so it is preferred to use a jupyter notebook).
System Prerequisites:
Prerequisites for the cloth are as follows:
- Choose a single-colored cloth, and if the cloth's color is red, make sure your background isn't red. Because if that color is present in the background, it will cause issues.
- We are using red color cloth in this project, but you can use any color by changing the values for the visibilities of the color, which is simple to do.
Importing Libraries
We will be implementing three libraries, i.e., numpy, time, and OpenCV. OpenCV allows us to access the webcam and also provides other features. Numpy and time will handle all the other operations.
import numpy as np
import cv2
import time

You can also try this code with Online Python Compiler
Run Code
Taking Video Feed Using Webcam
To take the video feed, we will be using the OpenCV library. Also, we will use the time function so that we can capture the background for the first three seconds. The zero value in the VideoCapture function helps us to access the webcam that is inbuilt in your system (primary webcam).
cap = cv2.VideoCapture(0)
time.sleep(3)
background = 0

You can also try this code with Online Python Compiler
Run Code
Capturing Background
Here we will capture the background for the first three seconds. We will convert the captured video into HSV format. HSV stands for Hue Saturation Value.
for i in range(50):
ret, background = cap.read()

You can also try this code with Online Python Compiler
Run Code
Capturing the Video Feed and Setting the Values for the Cloak
Now, we will concentrate on detecting the red region of the image in this step. Because RGB (red-blue-green) values are particularly sensitive to illumination, we'll convert them to HSV (hue-saturation-value). It's time to set the color range to identify the red color in the video after converting RGB to HSV. The color values for red are:
while(cap.isOpened()):
ret, img = cap.read()
if not ret:
break
hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
lower_red = np.array([0,120,70])
# values for red colour Cloth
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)
#Combining masks to view it in one frame.
mask1 = mask1 +mask2

You can also try this code with Online Python Compiler
Run Code
Morphological Transformations to Remove Noise from the Cloth
Morphological transformations: cv2.MORPH CLOSE simply removes extraneous details that aren't required, such as the small black holes that appear on the screen in this project. cv2.MORPH OPEN will remove the white region on the cloth's boundary that isn't required.
(Continue the following code in the above discussed while loop)
mask1 = cv2.morphologyEx(mask1, cv2.MORPH_OPEN, np.ones((3,3), np.uint8), iterations = 2)
mask1 = cv2.morphologyEx(mask1, cv2.MORPH_DILATE, np.ones( (3,3), np.uint8), iterations = 1)
mask2 =cv2.bitwise_not(mask1)

You can also try this code with Online Python Compiler
Run Code
Combining all the Masks
In the final step, we will utilize a bitwise operation stored in res1 to combine the masks we've created and save them in the res1 variable.
We will use the cv2.addWeighted function to create a sharp image.
res1 = cv2.bitwise_and(background,background,mask=mask1)
res2 = cv2.bitwise_and(img,img,mask=mask2)
final_output = cv2.addWeighted(res1,1,res2,1,0)
cv2.imshow('Invisible Cloak',final_output)
if cv2.waitKey(33) == ord('a'):
break
cap.release()
cv2.destroyAllWindows()
cv2.waitKey(1)

You can also try this code with Online Python Compiler
Run Code
Note: Our model captures the background for the first three seconds. So, to get accurate results, try not to be in the frame for the first three seconds. Our model can work on only red-colored clothes. You can also do it for any other color by slightly changing the configuration of the model.
Read about Bitwise Operators in C here.
FAQs
1. What is masking in image processing?
Masking is an image processing technique in which a small 'image piece' is defined and used to alter a bigger image. Many types of image processing, such as motion detection, edge detection, and noise reduction, rely on the process of masking.
2. What is the importance of morphological operations in image processing?
The goal of employing morphological techniques is to eliminate flaws in the image structure. The majority of the operations here combine two processes: dilatation and erosion.
3. What is OpenCV used for?
OpenCV is an excellent tool for image processing and computer vision. It's an open-source library that may be used for a variety of applications, including object tracking, face detection, landmark detection, and more.
4. What is image processing?
Image processing is a technique of performing several operations on an image in order to extract relevant information from it. It's a sort of signal processing in which the input is an image, and the output is either that image or its characteristics/features.
Key Takeaways
In this article, we have used simple masking techniques and Morphological Operations in this project to remove the colored cloth from the frame and replace it with the background.
Want to learn more about Machine Learning? Here is an excellent course that can guide you in learning.
Happy Coding!