如何定义BGR颜色范围?将颜色代码映射到颜色名称 [英] How to define BGR color range? Map color code to color name

查看:594
本文介绍了如何定义BGR颜色范围?将颜色代码映射到颜色名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建颜色映射,定义一些颜色名称和边界,这些颜色应在其范围内.例如(BGR格式),

I want to create color mapping, define few color names and boundaries in range of which those colors should fall. For example (BGR format),

colors = {
    'red': ((0, 0, 255), (125, 125, 255)),
    'blue': ((255, 0, 0), (255, 125, 125)),
    'yellow' ....
}

所以,如果我收到颜色,可以说(255,50,119)我可以称它为蓝色.我想至少对彩虹色加上灰色,黑色,白色进行这种映射.使用Python和openCV.

So if I receive color, let's say (255, 50, 119) I can call it blue. I want to make such mapping for at least colors of rainbow plus gray, black, white. Using Python and openCV.

问题是我真的不知道从哪里获得这些边界值,蓝色,红色等是否存在最低/最高值?

The problem is that I don't really understand where to get those values for boundaries, is there kind of lowest / highest value for blue, red and so on?

推荐答案

我建议使用 HSV 色彩空间来比较颜色,因为它对可变照明的敏感度低于 RGB ,其中阳光下的绿色可能是rgb(20,255,10),而阴影中的绿色可能是rgb(3,45,2),而两者在HSV色彩空间中的 Hue 都非常相似.

I would suggest using HSV colourspace for comparing colours because it is less sensitive to variable lighting than RGB, where green in the sunlight might be rgb(20,255,10), but green in a shadow might be rgb(3,45,2), whereas both will have a very similar Hue in HSV colourspace.

所以,开始吧...

创建一个10x1的numpy数组,使第一个像素为红色,第二个为橙色,然后为黄色,绿色,蓝色,靛蓝,紫罗兰色,然后为黑色,中灰和白色. 此处.

Create a little 10x1 numpy array and make the first pixel red, the second orange, then yellow, green, blue, indigo, violet then black, mid-grey and white. There's a table here.

然后转换为 HSV 色彩空间并记下色调值.

Then convert to HSV colourspace and note the Hue values.

我已经开始了一些代码...

I have started some code...

#!/usr/local/bin/python3
import numpy as np
import imageio
import cv2

# Create black image 10x1
im = np.zeros([1,10,3], dtype=np.uint8)

# Fill with colours of rainbow and greys
im[0,0,:]=[255,0,0]       # red
im[0,1,:]=[255,165,0]     # orange
im[0,2,:]=[255,255,0]     # yellow
im[0,3,:]=[0,255,0]       # green
im[0,4,:]=[0,0,255]       # blue
im[0,5,:]=[75,0,130]      # indigo
im[0,6,:]=[238,130,238]   # violet
im[0,7,:]=[0,0,0]         # black
im[0,8,:]=[127,127,127]   # grey
im[0,9,:]=[255,255,255]   # white
imageio.imwrite("result.png",im)

hsv=cv2.cvtColor(im,cv2.COLOR_RGB2HSV)
print(hsv)

查看图片:

也可以使用Imagemagick检查颜色:

Check colours with Imagemagick too:

convert result.png txt:

# ImageMagick pixel enumeration: 10,1,65535,srgb
0,0: (65535,0,0)  #FF0000  red
1,0: (65535,42405,0)  #FFA500  orange
2,0: (65535,65535,0)  #FFFF00  yellow
3,0: (0,65535,0)  #00FF00  lime
4,0: (0,0,65535)  #0000FF  blue
5,0: (19275,0,33410)  #4B0082  indigo
6,0: (61166,33410,61166)  #EE82EE  violet
7,0: (0,0,0)  #000000  black
8,0: (32639,32639,32639)  #7F7F7F  grey50
9,0: (65535,65535,65535)  #FFFFFF  white

现在查看下面的HSV数组-特别是第一列( Hue ).您可以看到红色的色相= 0,橙色的色相为19,黄色的色相为30,依此类推.还要注意,黑色,灰色和白色的饱和度均为零,黑色的低,灰色的中,白色的值高.

Now look at the HSV array below - specifically the first column (Hue). You can see Red has a Hue=0, Orange is 19, Yellow is 30 and so on. Note too that the Black, Grey and White all have zero Saturation and Black has a low Value, Grey has a medium Value and White has a high Value.

[[[  0 255 255]
  [ 19 255 255]
  [ 30 255 255]
  [ 60 255 255]
  [120 255 255]
  [137 255 130]
  [150 116 238]
  [  0   0   0]
  [  0   0 127]
  [  0   0 255]]]

现在,您可以在Python中创建一种存储每种颜色的数据结构:

Now you can make a data-structure in Python that stores, for each colour:

  • 最低收录了顺化
  • 收录的最高色相
  • 名称

因此,您可以使用:

... see note at bottom for Red
14,23,"Orange"
25,35,"Yellow"
55,65,"Green"
115,125,"Blue"
...

以此类推-省略表格中的黑色,灰色和白色.

and so on - omit Black, Grey and White from the table.

那么,您怎么使用呢?

好吧,当您要检查颜色时,请先将R,G和B值转换为 HSV ,然后查看生成的 Saturation (饱和度)-颜色的鲜艳度.扎眼的颜色将具有较高的饱和度,而灰暗的颜色将具有较低的饱和度.

Well, When you get a colour to check, first convert the R, G and B values to HSV and look at the resulting Saturation - which is a measure of vividness of the colour. Garish colours will have high saturation, whereas lacklustre, greyish colours will have low saturation.

因此,请查看饱和度是否超过最大可能值的10%,例如大于25,范围为0-255.

So, see if the Saturation is more than say 10% of the max possible, e.g. more than 25 on a scale of 0-255.

如果饱和度低于限制,请检查 Value ,如果 Value 低,则将其分配为黑色;如果将其置于中等,则将其分配为灰色;将 >值很高.

If the Saturation is below the limit, check the Value and assign Black if Value low, Grey if middling and White if Value is high.

如果饱和度超出限制,请检查它是否在记录的色调之一的上下限之内,并相应地命名.

If the Saturation is above the limit, check if it is within the lower and upper limits of one of your recorded Hues and name it accordingly.

所以代码是这样的:

def ColorNameFromRGB(R,G,B)
    # Calculate HSV from R,G,B - something like this
    # Make a single pixel from the parameters 
    onepx=np.reshape(np.array([R,G,B],dtype=np.uint8),(1,1,3))
    # Convert it to HSV
    onepxHSV=cv2.cvtColor(onepx,cv2.COLOR_RGB2HSV)
    ...
    ...
    if S<25:
        if V<85:
           return "black"
        elsif V<170:
           return "grey"
        return "white"
    # This is a saturated colour
    Iterate through colour names table and return name of entry with matching Hue


有两件事要注意:


There are 2 things to be aware of:

  • 红色的Hue值不连续,因为HSV色轮是一个圆形轮,红色的Hue值呈0度角,因此大于350且小于10的值均为红色.碰巧的是,OpenCV用2除以在0-360范围内缩放,这意味着它以0-180的形式出现……恰好适合单个无符号字节.因此,对于Red,您需要检查色相大于175且小于5的情况.

  • There is a discontinuity in the Hue values for Red, because the HSV colour wheel is a circular wheel and the Hue value for Red is at an angle of 0, so values above 350 and below 10 are all Reds. It so happens that OpenCV scales the 0-360 range by dividing by 2, meaning it comes out as 0-180... which neatly fits in a single unsigned byte. So, for Red, you need to check for Hue greater than 175 and less than 5, say.

在查找颜色时请务必始终生成8位图像,因为在16位和浮点图像上,色相值的缩放比例不同.

Be careful to always generate an 8-bit image when looking up colours, as the Hue values are scaled differently on 16-bit and float images.

这篇关于如何定义BGR颜色范围?将颜色代码映射到颜色名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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