Mohamed Kamal
Mohamed Kamal

Reputation: 175

detect yellow color in opencv

i convert image to HSV after it i make the threshold the yellow color so the code is cvInRangeS(imgHSV, cvScalar(112, 100, 100), cvScalar(124, 255, 255), imgThreshed); but it doesn't work always give me black image .

Upvotes: 8

Views: 71813

Answers (5)

Luca
Luca

Reputation: 109

I know question you ask is for c++, I have python script to detect yellow color which may help you or somebody else..

def colorDetection(image):
    hsv = cv2.cvtColor(image,cv2.COLOR_BGR2HSV)

    '''Red'''
    # Range for lower red
    red_lower = np.array([0,120,70])
    red_upper = np.array([10,255,255])
    mask_red1 = cv2.inRange(hsv, red_lower, red_upper)

    # Range for upper range
    red_lower = np.array([170,120,70])
    red_upper = np.array([180,255,255])
    mask_red2 = cv2.inRange(hsv, red_lower, red_upper)

    mask_red = mask_red1 + mask_red2

    red_output = cv2.bitwise_and(image, image, mask=mask_red)

    red_ratio=(cv2.countNonZero(mask_red))/(image.size/3)

    print("Red in image", np.round(red_ratio*100, 2))



    '''yellow'''
    # Range for upper range
    yellow_lower = np.array([20, 100, 100])
    yellow_upper = np.array([30, 255, 255])
    mask_yellow = cv2.inRange(hsv, yellow_lower, yellow_upper)

    yellow_output = cv2.bitwise_and(image, image, mask=mask_yellow)

    yellow_ratio =(cv2.countNonZero(mask_yellow))/(image.size/3)

    print("Yellow in image", np.round(yellow_ratio*100, 2))

Upvotes: 6

Abid Rahman K
Abid Rahman K

Reputation: 52646

You should try this tutorial for "tracking yellow objects".

It gives a HSV range of cvInRangeS(imgHSV, cvScalar(20, 100, 100), cvScalar(30, 255, 255), imgThreshed) for yellow object.

If you have any doubt on selecting color, try this : http://www.yafla.com/yaflaColor/ColorRGBHSL.aspx

Upvotes: 16

Ayush joshi
Ayush joshi

Reputation: 325

for yellow color the range as should be from 23 to 40 for example as per i am using in my yellow object tracking program

//Thresholding the frame for yellow

   cvInRangeS(hsvframe,cvScalar(23,41,133),cvScalar(40,150,255),threshy);

Upvotes: 3

andrea
andrea

Reputation: 1358

you can also convert RGB into HUE

http://en.wikipedia.org/wiki/Hue

in the link you have the formula, then you know that yellow has a HUE value around 60.

Upvotes: 2

aardvarkk
aardvarkk

Reputation: 15996

I think your hue values may be incorrect. I'm not sure where you're getting the 112-124 hue range from if you're trying to detect yellow. I would expect the values to be closer to 40, so maybe try a range like 34-46.

Upvotes: 1

Related Questions