Ask Your Question
0

How to define the “lower” and “upper” range of a color?

asked 2017-03-15 22:22:10 -0500

Mark K gravatar image

updated 2017-03-16 00:47:05 -0500

Hi, could you please help me with a question defining the range of a certain color?

Learning from the question and answers in the site /31305/why-is-this-simple-mask-not-working/

The lines limiting the red are 2 arrays:

lower = n.array([0,100,100])
upper = n.array([20,255,255])

how are these arrays are formed?

The RGB of red is (255, 0, 0) and its HSV is (0°, 100°, 100°)

How’s the RGB 255,0,0 to be relevant into ([0,100,100]) and ([20,255,255])? (reading it BGR shall be 0,0,255)

Thank you.

edit retag flag offensive close merge delete

Comments

the ranges are specific to a colorspace, so for which colorspace is it ?

berak gravatar imageberak ( 2017-03-16 02:41:01 -0500 )edit

@berak, thank you for the comment. "colorspace" is a new concept to me. Do you mean I need to convert the RGB to colorspace?

Mark K gravatar imageMark K ( 2017-03-16 02:45:55 -0500 )edit

no, but ranges for BGR are different from ranges for HSV, so you have to be explicit about what you want.

berak gravatar imageberak ( 2017-03-16 02:51:21 -0500 )edit

@berak, thanks. I just want to know what values to put in the arrays. i.e. how do I know between ([0,100,100]) and ([20,255,255]) is red?

Mark K gravatar imageMark K ( 2017-03-16 02:54:14 -0500 )edit

in hsv space: [0,100,100] and [20,255,255]

in bgr space: [255 255 0] and [0 0 255]

berak gravatar imageberak ( 2017-03-16 03:01:57 -0500 )edit

from where can I find the "hsv space" (lower and upper) for all colors?

Mark K gravatar imageMark K ( 2017-03-16 03:06:52 -0500 )edit

1 answer

Sort by » oldest newest most voted
1

answered 2017-03-16 03:28:50 -0500

berak gravatar image

from where can I find the "hsv space" (lower and upper) for all colors?

maybe use a "color picker" prog, like this: (it will print out the pixel value, lower and upper bound for each mouseclick)

import cv2
import numpy as np

image_hsv = None   # global ;(
pixel = (20,60,80) # some stupid default

# mouse callback function
def pick_color(event,x,y,flags,param):
    if event == cv2.EVENT_LBUTTONDOWN:
        pixel = image_hsv[y,x]

        #you might want to adjust the ranges(+-10, etc):
        upper =  np.array([pixel[0] + 10, pixel[1] + 10, pixel[2] + 40])
        lower =  np.array([pixel[0] - 10, pixel[1] - 10, pixel[2] - 40])
        print(pixel, lower, upper)

        image_mask = cv2.inRange(image_hsv,lower,upper)
        cv2.imshow("mask",image_mask)

def main():
    import sys
    global image_hsv, pixel # so we can use it in mouse callback

    image_src = cv2.imread(sys.argv[1])  # pick.py my.png
    if image_src is None:
        print ("the image read is None............")
        return
    cv2.imshow("bgr",image_src)

    ## NEW ##
    cv2.namedWindow('hsv')
    cv2.setMouseCallback('hsv', pick_color)

    # now click into the hsv img , and look at values:
    image_hsv = cv2.cvtColor(image_src,cv2.COLOR_BGR2HSV)
    cv2.imshow("hsv",image_hsv)

    cv2.waitKey(0)
    cv2.destroyAllWindows()

if __name__=='__main__':
    main()
edit flag offensive delete link more

Comments

@berak, thank you. could you please also give me samples of green and yellow's lower and upper arrays?

Mark K gravatar imageMark K ( 2017-03-16 03:40:23 -0500 )edit

no i can't (or rather won't)

your green is not my green, you have to use values specific to your situation

berak gravatar imageberak ( 2017-03-16 04:11:29 -0500 )edit

@berak, thanks again.

Mark K gravatar imageMark K ( 2017-03-16 04:25:21 -0500 )edit

thank you so much for the solution . This is really helpful!!

tanya gravatar imagetanya ( 2018-06-08 02:04:40 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2017-03-15 22:22:10 -0500

Seen: 21,137 times

Last updated: Mar 16 '17