1
votes

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)

while(1):

    # Take each frame
    _, frame = cap.read()

    # 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)

    cv2.imshow('frame',frame)
    cv2.imshow('mask',mask)
    cv2.imshow('res',res)
    k = cv2.waitKey(5) & 0xFF
    if k == 27:
        break

cv2.destroyAllWindows()

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.

2
See this question -- you can use the color wheel there and divide the value (in the 0-360 range) to find out the Hue value (as noted in the accepted answer). You can also take a look at this example. To briefly answer your question, red is around the 0 and 180 values (since colors "wrap around")Greg Sadetsky
Possible duplicate of OpenCV better detection of red color?Miki
I don't really understand that how do I get the S and V values. Can you please explain? Thanks a lot (in advance)! @MikiOmee

2 Answers

3
votes

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]]].

3
votes

Here is a program to determine color you need by choosing the 6 arrays parameters.(work on Opencv 3.2). You chose your image or a "color range barre" input image and you move cursors and see which arrays values are the ones you need to isolate your color! Color range program screen pic

here is the code:(can easily be adapted for video input). image.jpg->(your image) color_bar.jpg->(any image you want just to display a windows,try anything)

import cv2
import numpy as np
from matplotlib import pyplot as plt

def nothing(x):
    pass

def main():

    window_name='color range parameter'
    cv2.namedWindow(window_name)
    # Create a black image, a window
    im = cv2.imread('image.jpg')
    cb = cv2.imread('color_bar.jpg')
    hsv = cv2.cvtColor(im,cv2.COLOR_BGR2HSV)

    print ('lower_color = np.array([a1,a2,a3])')
    print ('upper_color = np.array([b1,b2,b3])')


    # create trackbars for color change
    cv2.createTrackbar('a1',window_name,0,255,nothing)
    cv2.createTrackbar('a2',window_name,0,255,nothing)
    cv2.createTrackbar('a3',window_name,0,255,nothing)

    cv2.createTrackbar('b1',window_name,150,255,nothing)
    cv2.createTrackbar('b2',window_name,150,255,nothing)
    cv2.createTrackbar('b3',window_name,150,255,nothing)

    while(1):
        a1 = cv2.getTrackbarPos('a1',window_name)
        a2 = cv2.getTrackbarPos('a2',window_name)
        a3 = cv2.getTrackbarPos('a3',window_name)

        b1 = cv2.getTrackbarPos('b1',window_name)
        b2 = cv2.getTrackbarPos('b2',window_name)
        b3 = cv2.getTrackbarPos('b3',window_name)

        # hsv hue sat value
        lower_color = np.array([a1,a2,a3])
        upper_color = np.array([b1,b2,b3])
        mask = cv2.inRange(hsv, lower_color, upper_color)
        res = cv2.bitwise_and(im, im, mask = mask)

        cv2.imshow('mask',mask)
        cv2.imshow('res',res)
        cv2.imshow('im',im)
        cv2.imshow(window_name,cb)

        k = cv2.waitKey(1) & 0xFF
        if k == 27:         # wait for ESC key to exit
            break
        elif k == ord('s'): # wait for 's' key to save and exit
            cv2.imwrite('Img_screen_mask.jpg',mask)
            cv2.imwrite('Img_screen_res.jpg',res)
            break


    cv2.destroyAllWindows()


#Run Main
if __name__ == "__main__" :
    main()