简体   繁体   中英

How to get low and high values of hsv color in python opencv

I am trying detect few colors in python opencv. For this I need to define the low and high hsv values so that the code can read it and detect colors. Now the issue I am facing is how do I get the high and low hsv colors. I am referring to below image

在此处输入图片说明

I need to detect this jacket and thus need to input its high and low hsv. For this I got a reference to this code which allows to select any part of image and will output the high and low hsv values for it. But as far as I know, hsv value cannot be larger than 100 but this code and most of the other codes online gives hsv values which are greater than 100, and this is very I am getting confused as to how these values can be greater than 100.

Can anyone please explain how can we get the values of low and high hsv values

Try below code:

import cv2
import numpy as np

img = cv2.imread("jacket.jpg")

hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

# mask of green (36,25,25) ~ (86, 255,255)
mask = cv2.inRange(hsv, (36, 25, 25), (70, 255,255))

green = cv2.bitwise_and(img,img, mask= mask)    

cv2.imshow('Image', green)
cv2.waitKey(0)
cv2.destroyAllWindowss()

output:

在此处输入图片说明

Check this stackoverflow discussion on how to correctly select the upper and lower hsv values for color detection.

Couldnt find the resource but found something like this and made it useful, thanks to the author

import cv2
import imutils  
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("myimage.jpeg")  # pick.py my.png
    image_src = imutils.resize(image_src, height=800)
    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()

Image loaded will look like this:

在此处输入图片说明

After clicking on the ball you will get an image like,

在此处输入图片说明

And finally: true BGR value, lower and upper HSV boundaries will be printed in the terminal as follows,

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM