[Main Color Extraction] Formulas and codes for converting HSV color space and RGB color space into each other

Table of Contents of Series Articles

 


Chapter 2 Main Color Extraction and Color Space Transformation


Table of contents

Table of Contents of Series Articles

Article directory

Preface

1. HSV and RGB color space

2. Color conversion

1. RGB to HSV

2. HSV to RGB

3. Complete code


Preface

Conversion between HSV color space and RGB color space .


1. HSV and RGB color spaces

RGB colors are the most commonly used, namely red (R), green (G) and blue (B).

H(hue) in HSV color means hue, which is identified by color name, such as red, orange, green. It is measured in angle, from -180° to 180°, or from 0° to 360°; S(saturation) means  Chroma or saturation refers to the depth of a color. For example, red can also be divided into dark red and light red due to different concentrations. S is measured in percentage, from 0% to 100% of full saturation; V (value) represents Brightness refers to the lightness or darkness of a color, usually measured as a percentage, from 0% black to 100% white.

2. Color conversion

1. RGB to HSV

RGB to HSV is relatively simple to implement.

Before calculating the formula, there are three assumptions:

  • Let the value of (r, g, b) be a real number between 0 and 1
  • Let max be equal to the largest of r, g, b
  • Let min equal to the smallest of r, g, b

 The formula is as shown below:

 code show as below:


def RGB2HSV(r,g,b):
    r, g, b = r / 255, g / 255, b / 255
    ma = max(r, g, b)
    mi = min(r, g, b)
    diff = ma - mi
    if ma == mi:
        h = 0
    elif ma == r and g >= b:
        h = 60 * ((g - b)/diff) + 0
    elif ma == r and g < b:
        h = 60 * ((g - b)/diff) + 360
    elif ma == g:
        h = 60 * ((b - r)/diff) + 120
    elif ma == b:
        h = 60 * ((r - g)/diff) + 240

    if ma == 0:
        s = 0
    else:
        s = diff / ma

    v = ma
    return h, s, v

2. HSV to RGB

Converting HSV to RGB is more difficult than the above.

Two points to note:

1 Parameter input range h(0~360), s(0% ~ 100%), v(0% ~ 100%) 
2 Conversion results R(0~255), G(0~255), B(0~255 ), if you need to convert to 0~100, just change the following multiplication by 255 to multiplication by 100

The formula is as follows:

 The implementation code is as follows:



def HSV2RGB(h, s, v):
    # s, v = s / 100, v /100

    i = round(h / 60) % 6
    f = (h / 60) - i
    p = v * (1 - s)
    q = v * (1 - f * s)
    t = v * (1 - (1 - f) * s)

    R, G, B = 0, 0, 0
    if i == 0:
        R, G, B = v, t, p
    elif i == 1:
        R, G, B = q, v, p
    elif i == 2:
        R, G, B = p, v, t
    elif i == 3:
        R, G, B = p, q, v
    elif i == 4:
        R, G, B = t, p, v
    elif i == 5:
        R, G, B = v, p, q

    r, g, b = round(R * 255), round(G * 255), round(B * 255)
    return r, g, b

3. Complete code

The complete code can be downloaded on GitHub. If you are interested, you might as well give me a star, haha.

连接:GitHub - Liangliangb/Image-main-color-extraction: Use the k-means algorithm to extract the dominant color of the picture.Use the k-means algorithm to extract the dominant color of the picture. - GitHub - Liangliangb/Image-main-color-extraction: Use the k-means algorithm to extract the dominant color of the picture.https://github.com/Liangliangb/Image-main-color-extraction.git

Guess you like

Origin blog.csdn.net/m0_51816252/article/details/128651140