VICTOR
VICTOR

Reputation: 1942

OpenCV: Finding the color intensity

I want to write a program to see whether the red color intensity is the dominant color. If the brown color intensity is greater than the threshold. Then the program will print out "Detected".

enter image description here

For example, the red color in the photo is the dominant color, so the program should print out "Detected"!.

I have written somethings like this:

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)

However, it only converts the color of the image, but the giving the intensity. How can I get the Boolean value that the image has more red color or not?

Upvotes: 2

Views: 3099

Answers (1)

Aleksey Petrov
Aleksey Petrov

Reputation: 370

You should convert your image to the HSV color space. It is simple to separate red color in that space: red color will have Hue near [0-10] and [160-180]. Then you can check whether ratio of red color is greater than threshold.

(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

Upvotes: 2

Related Questions