OpenCV:寻找颜色强度

OpenCV: Finding the color intensity

我想编写一个程序来查看红色颜色强度是否是主色。如果棕色强度大于阈值。然后程序会打印出"Detected".

例如,照片中的红色是主色,所以程序应该打印出"Detected"!。

我写过这样的东西:

lower_red = np.array([110, 50, 50], dtype=np.uint8)
upper_red = np.array([130,255,255], dtype=np.uint8)
mask = cv2.inRange(hsv, lower_red, upper_red)
res = cv2.bitwise_and(frame,frame, mask= mask)

然而,它只转换图像的颜色,而不是给出强度。如何获取图像是否偏红的布尔值?

您应该将图像转换为 HSV 颜色 space。分离红色很简单,因为space:红色的色调接近[0-10]和[160-180]。 然后你可以检查红色的比例是否大于阈值。

(pseudocode)

fun isRedColorGreaterThanThreshold(image, threshold)
  imageHSV = convertToHSV(image)
  channels = split(imageHSV)
  Hue = channels[0]
  ratio = countNonZero((0 < Hue < 10) or (160 < Hue < 180)) / image.total()

  return ratio > threshold