0

I am beginner in Python OpenCV and I would like to extract the green color from the attached figure. I use the following code

img = cv2.imread('C:/Python27/Caputure.jpg')
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)    
lower_green = np.array([84,113,85])
upper_green = np.array([84,113,185])
mask = cv2.inRange(hsv, lower_green, upper_green)
res = cv2.bitwise_and(img,img, mask= mask)
cv2.imshow("img",img)
cv2.imshow("mask",mask)
cv2.imshow("res",res)
cv2.waitKey(0)

However, all I got is a black image!! Is that normal? Do you have any idea why the green is not selected?

Thank you very much.

The image I use

Hajar Elhammouti
  • 103
  • 2
  • 10
  • Please post your mask and res results. We will try to figure out what went wrong. – Kamil Szelag Oct 06 '17 at 18:25
  • You're only allowing the values in your matrix that are between [84, 113, 85] and [84, 113, 185]. If you don't have any values with a hue of 84 or a saturation of 113, then you won't get any values in your mask. Using `inRange` is just identical to thresholding with < or >. The resulting mask is the same as saying [84, 113, 85] <= img <= [84 113, 185]. – alkasm Oct 06 '17 at 18:30
  • I don't think you feed the right green range in hsv space. – Kinght 金 Oct 06 '17 at 18:32
  • You're using the wrong range. See the values in the dupe – Miki Oct 06 '17 at 20:55
  • Yes, thank you very much, I changed the range, it is now lower_green = np.array([50,100,100]), and upper_green = np.array([100,255,255]), and it works. Thanks a lot for all of you guys! – Hajar Elhammouti Oct 07 '17 at 17:45

0 Answers0