-1

I am trying to detect green colour from images, for that I using cv2, by defining upper and lower limit of green colour as [40, 0, 0] and [90, 255, 255], this works well,

but, when I tested this image, I got detected the white region also, the output is mask image, the expected mask output is only black colour

the code is

frame = cv2.imread('img.jpg')
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
lower = np.array([40, 0, 0])
upper = np.array([90, 255, 255])
mask = cv2.inRange(hsv, lower, upper)
res = cv2.bitwise_and(frame,frame, mask= mask)
cv2.imshow('mask',mask)

Why the upper and lower bound of green detects the white regions in the input image, and is there any other way to detect only green colour from the image

thanks

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
vishak raj
  • 13
  • 4

2 Answers2

1

Here are the colors that can be found in this image.

enter image description here

Where is "your" green ?

1

The problem you are having is because the color [50,0,255] (which is in your range) is white, as seen here. (in this picture divide Hue by 2 and multiply Saturation and value with 2.55.)

If you only want green you should set your limits something like the following: Lower [40,70,120] Upper [90,255,255] This way you make sure that white and black that have a hue between 40 and 90 don't get picked up in your program.

Epsi
  • 115
  • 7