HSV OpenCv颜色范围 [英] HSV OpenCv colour range

查看:109
本文介绍了HSV OpenCv颜色范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以告诉我网站的名称或我可以从中获得基本颜色HSV上下范围的任何地方的名称

黄色,绿色,红色,蓝色,黑色,白色,橙色

实际上,我正在制作一个机器人,该机器人首先会遵循黑色线条,然后在该线条的中间出现另一种颜色,该颜色是将3种不同颜色的不同线条分开的.跟随.为此,我需要适当的hsv颜色范围

解决方案

灵感来自此处的文档

HSV的范围从H到0-179,S到V从0-255,因此,对于您对下限范围和上限范围示例的要求,您可以对[p]的任何给定[h,s,v]执行

[h-10,s-40,v-40] 降低

[h + 10,s + 10,v + 40] 黄色,绿色,红色,蓝色,黑色,白色,橙色 rgb值.

上面的代码用于当您想要通过单击所需的颜色直接从捕获的图像或视频中选择HSV范围时.

如果要预定义范围,则可以使用内置的python库 colorsys 使用编写简单的代码段,使用 colorsys.rgb_to_hsv 函数

将rbg转换为hsv>

文档中的示例

请注意,此函数仅接受0到1范围内的rgb值,并且也提供0到1范围内的hsv值,因此要使用相同的值,则需要对opencv进行归一化

代码段

 导入colorsys'''将给定的rgb转换为hsv opencv格式'''def rgb_hsv_converter(rgb):(r,g,b)= rgb_normalizer(rgb)hsv = colorsys.rgb_to_hsv(r,g,b)(h,s,v)= hsv_normalizer(hsv)upper_band = [h + 10,s + 40,v + 40]lower_band = [h-10,s-40,v-40]返回 {'upper_band':upper_band,'lower_band':lower_band}def rgb_normalizer(rgb):(r,g,b)= rgb返回(r/255,g/255,b/255)def hsv_normalizer(hsv):(h,s,v)= hsv返回(h * 360,s * 255,v * 255)rgb_hsv_converter((255,165,0)) 

将返回

{'upper_band':[48.82352941176471,295.0,295.0],'lower_band':[28.82352941176471,215.0,215.0]}

这是您的橙色hsv乐队.

Can anyone please tell me a name of a website or any place from where I can get the upper and lower range of HSV of basic colours like

yellow,green,red,blue,black,white,orange

Actually I was making a bot which would at first follow black coloured line and then in the middle of the line there would be another colour given from where 3 different lines of different colour gets divided.The bot needs to decide which line to follow. For that I need the proper range of hsv colours

解决方案

Inspired from the answer at answers.opencv link.

According to docs here

the HSV ranges like H from 0-179, S and V from 0-255, so as for your requirements for lower range and upper range example you can do for any given [h, s, v] to

[h-10, s-40, v-40] for lower

and

[h+10, s+10, v+40] for upper for the yellow,green,red,blue,black,white,orange rgb values.

Copied code from the example :

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

Above code is for when you want to directly select the HSV range from the image or video you are capturing, by clicking on the desired color.

If you want to predefine your ranges you can just use write simple code snippet using inbuilt python library colorsys to convert rbg to hsv using colorsys.rgb_to_hsv function

example in docs

Note this function accepts rgb values in range of 0 to 1 only and gives hsv values also in 0 to 1 range so to use the same values you will need to normalize it for opencv

code snippet

import colorsys
'''
convert given rgb to hsv opencv format
'''

def rgb_hsv_converter(rgb):
    (r,g,b) = rgb_normalizer(rgb)
    hsv = colorsys.rgb_to_hsv(r,g,b)
    (h,s,v) = hsv_normalizer(hsv)
    upper_band = [h+10, s+40, v+40]
    lower_band = [h-10, s-40, v-40]
    return {
        'upper_band': upper_band,
        'lower_band': lower_band
    }

def rgb_normalizer(rgb):
    (r,g,b) = rgb
    return (r/255, g/255, b/255)

def hsv_normalizer(hsv):
    (h,s,v) = hsv
    return (h*360, s*255, v*255)

rgb_hsv_converter((255, 165, 0))

will return

{'upper_band': [48.82352941176471, 295.0, 295.0], 'lower_band': [28.82352941176471, 215.0, 215.0]}

which is your orange hsv bands.

这篇关于HSV OpenCv颜色范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆