Skip to content

Tutorials

Tutorial: Remove background – opencv 3.2 with python 3

Access community, courses and source codes
Logo

AI Vision Academy

Access the code of this tutorial, computer vision courses and an exclusive community on AI Vision Academy

  • Access to over 50+ source codes from Pysource.com/blog
  • Dedicated video courses about computer vision
  • Access to an exclusive community of professionals
  • Real-World AI Projects – Get hands-on experience building practical AI Computer Vision solutions with a structured path.
  • Monthly Coaching Calls – get support and any of your questions answered

Subscribe to our newsletter to learn more

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)

One comment

  1. 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

Join the discussion