In this tutorial, we will see how to delete the background using only OpenCV and python. In this case, I will identify the color through HSV, and for more details I suggest you see my most recent article on the subject: Detecting colors (Hsv Color Space) – Opencv with Python or use other techniques, always with OpenCV, such as Background Subtraction – OpenCV 3.4 with python 3 Tutorial 32 .

How to remove the background?

With this procedure it is good to have a background with a uniform color, it will make everything easier. The first step is to create a window with the trackbar to easily select which color to remove.

def nothing(x):

cv2.createTrackbar('L – h', 'panel', 0, 179, nothing)
cv2.createTrackbar('U – h', 'panel', 179, 179, nothing)

cv2.createTrackbar('L – s', 'panel', 0, 255, nothing)
cv2.createTrackbar('U – s', 'panel', 255, 255, nothing)

cv2.createTrackbar('L – v', 'panel', 0, 255, nothing)
cv2.createTrackbar('U – v', 'panel', 255, 255, nothing)

cv2.createTrackbar('S ROWS', 'panel', 0, 480, nothing)
cv2.createTrackbar('E ROWS', 'panel', 480, 480, nothing)
cv2.createTrackbar('S COL', 'panel', 0, 640, nothing)
cv2.createTrackbar('E COL', 'panel', 640, 640, nothing)