0

I'm trying to detect color-range using OpenCV Python.

When I use RGB colors on HSV converted image, it works fine. But when I change lower and upper colors to HSV, it cannot detect colors.

Here is a bit dirty code. lower and upper variables are BGR and I try to convert them below with hsv_lower and hsv_upper but when passed them to cv2.inRange() function, it results with empty black mask/screen.

image = cv2.imread('images/circles.jpg', 1)

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


green = np.uint8([[[0, 255, 0]]])
hsv_green = cv2.cvtColor(green, cv2.COLOR_BGR2HSV)
print hsv_green


lower = np.array([0, 100, 100], np.uint8);
upper = np.array([10, 255, 255], np.uint8);

lowerz = np.uint8([[[0, 100, 100]]]);
hsv_lower = cv2.cvtColor(lowerz, cv2.COLOR_BGR2HSV)
print hsv_lower

upperz = np.uint8([[[10, 255, 255]]]);
hsv_upper = cv2.cvtColor(upperz, cv2.COLOR_BGR2HSV)
print hsv_upper


# Threshold the HSV image to get only blue colors
mask = cv2.inRange(hsv, hsv_lower, hsv_upper)

output = cv2.bitwise_and(image, image, mask=mask)

cv2.imshow("images", np.hstack([image, output]))


cv2.imshow('frame', image)
cv2.imshow('mask', mask)
cv2.imshow('res', output)
#cv2.waitKey(0)
k = cv2.waitKey(0) & 0xFF
if k == 27:
    cv2.destroyAllWindows()
she hates me
  • 1,212
  • 5
  • 25
  • 44
  • not completely sure what you are doing, but opencv's hue channel is scaled by 0.5 to fit 360 degrees in 8 bit. So maybe your expected hue values are wrong? – Micka Feb 24 '18 at 21:26
  • @Micka thank you, I will check this. I am trying to detect cocoons in a soil. in fact i dont know much about HSV and colors and it could be wrong. – she hates me Feb 24 '18 at 21:29
  • You should set `HSV range` in HSV-space, not simple convert it. This may helps: [what-are-recommended-color-spaces-for-detecting-orange-color-in-open-cv](https://stackoverflow.com/questions/48528754/what-are-recommended-color-spaces-for-detecting-orange-color-in-open-cv/48533523#48533523) – Kinght 金 Feb 25 '18 at 01:02

0 Answers0