Omee Omee - 5 months ago 43
Python Question

How To Detect Red Color In OpenCV Python?

I am trying to detect red color from the video that's being taken from my webcam. The following code example given below is taken from OpenCV Documentation.
The code is given below:

import cv2
import numpy as np

cap = cv2.VideoCapture(0)


# Take each frame
_, frame =

# Convert BGR to HSV
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

# define range of blue color in HSV
lower_blue = np.array([110,50,50])
upper_blue = np.array([130,255,255])

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

# Bitwise-AND mask and original image
res = cv2.bitwise_and(frame,frame, mask= mask)

k = cv2.waitKey(5) & 0xFF
if k == 27:


The line
lower_blue = np.array([110,50,50])
has the lower range Blue HSV value and the line
upper_blue = np.array([130,255,255])
has the higher range Blue HSV value. I have looked for the upper value and lower value of Red color on internet but I couldn't find it. It would be very helpful if anyone could tell the HSV value of Red for OpenCV (OpenCV H value ranges from 0 - 179).
Thanks a lot for help (In Advance).

I have also tried running the following to find the range of Red but I was unable to pick proper value maybe. What I tried was this(for red):

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

This was also taken from OpenCV documentation.
Please tell me or help me find the RANGE of RED COLOR for OpenCV.

Answer Source

Running the same code for red seems to work:

>>> red = numpy.uint8([[[0,0,255]]])
>>> hsv_red = cv2.cvtColor(red,cv2.COLOR_BGR2HSV)
>>> print(hsv_red)
[[[  0 255 255]]]

And then you can try different colors that appear reddish. Beware that the red range includes both numbers slightly greater than 0 and numbers slightly smaller than 179 (e.g. red = numpy.uint8([[[0,31,255]]]) results in [[[ 4 255 255]]] whereas red = numpy.uint8([[[31,0,255]]]) results in [[[176 255 255]]].