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):
pass
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)

Hi there, I’m the founder of Pysource.
I’m a Computer Vision Consultant, developer and Course instructor.
I help Companies and Developers to build efficient computer vision software.
Hi, your videos are really helpful. I’m trying to detect the court in a basketball game; with Canny and Hough Transform I’ve got the lines of the court but also I’ve got another no necessary lines, how can I isolate just the lines of the court ignoring the others?
Thanks in advance