1

im using the following code to color screen a photo. We are trying to locate the orange circle within the image. Is there a way to eliminate some of the background noise shown in the second photo? Tweaking the color range some may help but its never enough to fully eliminate the background noise. I've also considered trying to locate circle shapes within the image but i am unsure how to do that. Any help would be amazing!

hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

Lower_bound = np.array([0, 80, 165]) # COLORS NEEDED
Upper_bound = np.array([75, 255, 255])

mask = cv2.inRange(hsv, Lower_bound, Upper_bound)

enter image description here

enter image description here

1 Answers1

3

Option 1 (HSV color space):

If you want to continue using HSV color space robustly, you must check out this post. There you can control the variations across the three channels using trackbars.

Option 2 (LAB color space):

Here I will be using the LAB color space where dominant colors can be segmented pretty easily. LAB space stores image across three channels (1 brightness channel) and (2 color channels):

  • L-channel: amount of lightness in the image
  • A-channel: amount of red/green in the image
  • B-channel: amount of blue/yellow in the image

Since orange is a close neighbor of color red, using the A-channel can help segment it.

Code:

img = cv2.imread('image_path')

# Convert to LAB color space
lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
cv2.imshow('A-channel', lab[:,:,1])

enter image description here

The above image is the A-channel, where the object of interest is pretty visible.

# Apply threshold
th = cv2.threshold(lab[:,:,1],150,255,cv2.THRESH_BINARY)[1]

enter image description here

Why does this work?

Looking at the LAB color plot, the red color is on one end of A-axis (a+) while green color is on the opposite end of the same axis (a-). This means, higher values in this channel represents colors close to red, while the lower values values represents colors close to green.

enter image description here

The same can be done along the b-channel while trying to segment yellow/blue color in the image.

Post-processing:

From here onwards for every frame:

  • Identify the largest contour in the binary threshold image th.
  • Mask it over the frame using cv2.bitwise_and()

Note: this LAB color space can help segment dominant colors easily. You need to test them before using it for other colors.

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87