Home > database >  Color Space Segmentation not detecting white spaces?
Color Space Segmentation not detecting white spaces?

Time:09-01

I am using colour space segmentation to detect traffic signs of multiple colours.

This is my code:

img = cv.imread('055_0039.png')
img_hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)
img_hsv = cv.GaussianBlur(img_hsv, (5, 5), 0)
low_red_hsv = (0, 70, 50)
high_red_hsv = (10, 255, 255)

low_red_light = (170, 70, 50)
high_red_light = (180, 255, 255)

low_blue = (100,150,0)
high_blue = (120,255,255)

low_yellow = (20, 100, 100)
high_yellow = (30, 255, 255)

mask_yellow = cv.inRange(img_hsv, low_yellow, high_yellow)
mask_blue = cv.inRange(img_hsv, low_blue, high_blue)
mask_red = cv.inRange(img_hsv, low_red_hsv, high_red_hsv)
mask_red_light = cv.inRange(img_hsv, low_red_light, high_red_light)

mask = mask_red | mask_red_light | mask_blue | mask_yellow
#mask = cv.inRange(img_hsv, low_blue, high_blue)
res = cv.bitwise_and(img, img, mask = mask)

result = cv.cvtColor(res, cv.COLOR_BGR2RGB)

plt.subplot(1,2,1)
plt.imshow(mask, cmap = 'gray')
plt.subplot(1,2,2)
plt.imshow(result)
plt.show()

for images with no white colours in it, this will be the output: enter image description here

but I want to also segment the white colours of these images: enter image description here enter image description here

Original Images: enter image description here enter image description here

Am I doing something wrong with my code here?

CodePudding user response:

I think you should read: Tracking white color using python opencv.

Since you are not applying any filter to get the white color, you don't see it in the result image. You can plot the intermediate masked image to see it (mask_yellow, mask_blue, etc.)

You could do something like this in order to get white color:

import cv2 as cv
import numpy as np

img = cv.imread('img.png')
img_hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)
img_hsv = cv.GaussianBlur(img_hsv, (5, 5), 0)

sensitivity = 35
lower_white = np.array([0,0,255-sensitivity])
upper_white = np.array([255,sensitivity,255])

low_red_hsv = (0, 70, 50)
high_red_hsv = (10, 255, 255)

low_red_light = (170, 70, 50)
high_red_light = (180, 255, 255)

low_blue = (100,150,0)
high_blue = (120,255,255)

low_yellow = (20, 100, 100)
high_yellow = (30, 255, 255)

mask_yellow = cv.inRange(img_hsv, low_yellow, high_yellow)
mask_blue = cv.inRange(img_hsv, low_blue, high_blue)
mask_red = cv.inRange(img_hsv, low_red_hsv, high_red_hsv)
mask_red_light = cv.inRange(img_hsv, low_red_light, high_red_light)
mask_white = cv.inRange(img_hsv, lower_white, upper_white)

mask = mask_red | mask_red_light | mask_blue | mask_yellow | mask_white
res = cv.bitwise_and(img, img, mask = mask)

result = cv.cvtColor(res, cv.COLOR_BGR2RGB)
  • Related