[Computer Vision 1]–Graphics preprocessing (color space conversion)

Article directory

  • image preprocessing
    • color space conversion
      • RGB to HSV conversion
      • HSV to RGB conversion
      • RGB to CMYK conversion

Image preprocessing

Computer vision image preprocessing refers to a series of processing and conversion of images before image processing, so as to better perform subsequent image processing and analysis. Its main purpose is to enable images to be recognized, processed and analyzed by computers while retaining as much useful information as possible.

Image preprocessing frame diagram
Today we mainly talk about color space conversion, and others are mentioned in image enhancement algorithms and sharpening algorithms.

Color space conversion

Color space conversion refers to the process of converting the representation of an image from one color space to another. Common color spaces include RGB, HSV, CMYK, etc. The following are several common color space conversion methods:

RGB to HSV conversion

The RGB color space contains three components of red, green, and blue, while the HSV color space contains three components of hue (Hue), saturation (Saturation) and value (Value). The conversion method is as follows:

  • Calculate the maximum and minimum values: max_val = max(R, G, B), min_val = min(R, G, B).
  • Calculate Hue (Hue):
 If max_val = min_val, then Hue = 0 (defined as red)
If max_val = R, then Hue = (G - B) / (max_val - min_val)
If max_val = G, then Hue = 2 + (B - R) / (max_val - min_val)
If max_val = B, then Hue = 4 + (R - G) / (max_val - min_val)
  • Calculate Saturation:
 If max_val = 0, then Saturation = 0
Otherwise, Saturation = (max_val - min_val) / max_val
  • Calculated value (Value): Value = max_val

  • Python implementation

def rgb_to_hsv(r, g, b):
    r, g, b = r / 255.0, g / 255.0, b / 255.0
    max_val = max(r, g, b)
    min_val = min(r, g, b)
    hue, saturation, value = 0, 0, 0

    # calculate hue
    if max_val == min_val:
        hue = 0
    elif max_val == r:
        hue = 60 * (0 + (g - b) / (max_val - min_val))
    elif max_val == g:
        hue = 60 * (2 + (b - r) / (max_val - min_val))
    elif max_val == b:
        hue = 60 * (4 + (r - g) / (max_val - min_val))

    # calculate saturation
    if max_val != 0:
        saturation = (max_val - min_val) / max_val

    # Calculated
    value = max_val
  • C++ implementation
void RGBtoHSV(int r, int g, int b, double & amp; h, double & amp; s, double & amp; v) {<!-- -->
    double red = r / 255.0;
    double green = g / 255.0;
    double blue = b / 255.0;

    double maxVal = std::max({<!-- --> red, green, blue });
    double minVal = std::min({<!-- --> red, green, blue });
    double delta = maxVal - minVal;

    // calculate hue (Hue)
    if (delta == 0) {<!-- -->
        h = 0; // no color, hue is 0
    }
    else if (maxVal == red) {<!-- -->
        h = 60 * ((green - blue) / delta);
    }
    else if (maxVal == green) {<!-- -->
        h = 60 * ((blue - red) / delta + 2);
    }
    else if (maxVal == blue) {<!-- -->
        h = 60 * ((red - green) / delta + 4);
    }

    if (h < 0) {<!-- -->
        h + = 360; // adjust between 0 and 360 degrees
    }

    // Calculate Saturation
    if (maxVal == 0) {<!-- -->
        s = 0;
    }
    else {<!-- -->
        s = delta / maxVal;
    }

    // computed value (Value)
    v = maxVal;
}

HSV to RGB conversion

The hue (Hue), saturation (Saturation) and value (Value) in the HSV color space can be converted to the red, green, and blue components in the RGB color space. The conversion method is as follows:

  • If Saturation = 0, then R = G = B = Value
  • Otherwise, calculate the corresponding interval according to the value of Hue, and perform corresponding calculations:
 Multiply Hue by 6 to get an angle value, denoted as H
Round H down to get an integer part, denoted as I
Calculate the fractional part of H minus I, denoted as F
Calculate P = Value × (1 - Saturation)
Calculate Q = Value × (1 - Saturation × F)
Calculate T = Value × (1 - Saturation × (1 - F))
According to the value of I, select the corresponding calculation formula to calculate the value of R, G, B:
If I = 0, then R = Value, G = T, B = P
If I = 1, then R = Q, G = Value, B = P
If I = 2, then R = P, G = Value, B = T
If I = 3, then R = P, G = Q, B = Value
If I = 4, then R = T, G = P, B = Value
If I = 5, then R = Value, G = P, B = Q
  • Python implementation
def hsv_to_rgb(h, s, v):
    if s == 0:
        r = g = b = v
    else:
        h /= 60
        i = int(h)
        f = h - i
        p = v * (1 - s)
        q = v * (1 - s * f)
        t = v * (1 - s * (1 - f))

        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
        else:
            r, g, b = v, p, q
  • C++ implementation
void HSVtoRGB(double h, double s, double v, int & amp; r, int & amp; g, int & amp; b) {<!-- -->
    if (s == 0) {<!-- -->
        r = g = b = v * 255;
        return;
    }

    h /= 60;
    int i = static_cast<int>(h);
    double f = h - i;
    double p = v * (1 - s);
    double q = v * (1 - s * f);
    double t = v * (1 - s * (1 - f));

    switch (i) {<!-- -->
        case 0:
            r = static_cast<int>(v * 255);
            g = static_cast<int>(t * 255);
            b = static_cast<int>(p * 255);
            break;
        case 1:
            r = static_cast<int>(q * 255);
            g = static_cast<int>(v * 255);
            b = static_cast<int>(p * 255);
            break;
        case 2:
            r = static_cast<int>(p * 255);
            g = static_cast<int>(v * 255);
            b = static_cast<int>(t * 255);
            break;
        case 3:
            r = static_cast<int>(p * 255);
            g = static_cast<int>(q * 255);
            b = static_cast<int>(v * 255);
            break;
        case 4:
            r = static_cast<int>(t * 255);
            g = static_cast<int>(p * 255);
            b = static_cast<int>(v * 255);
            break;
        default:
            r = static_cast<int>(v * 255);
            g = static_cast<int>(p * 255);
            b = static_cast<int>(q * 255);
            break;
    }
}

Note that Hue should be between 0 and 360 and Saturation and Value should be between 0 and 1. The returned RGB value will be between 0 and 255.

RGB to CMYK conversion

The red, green, and blue components in the RGB color space can be converted to the cyan, yellow, magenta, and black components in the CMYK color space. The conversion method is as follows:

  • Calculate Cyan: Cyan = 1 – (Red / 255)
  • Calculate Magenta: Magenta = 1 – (Green / 255)
  • Calculate Yellow: Yellow = 1 – (Blue / 255)
  • Calculate Black: Black = min(Cyan, Magenta, Yellow)
  • Python implementation
def rgb_to_cmyk(r, g, b):
    r, g, b = r / 255.0, g / 255.0, b / 255.0

    k = 1 - max(r, g, b)
    if k == 1:
        return 0, 0, 0, 1

    c = (1 - r - k) / (1 - k)
    m = (1 - g - k) / (1 - k)
    y = (1 - b - k) / (1 - k)
  • C++ implementation
void RGBtoCMYK(int r, int g, int b, double & amp; c, double & amp; m, double & amp; y, double & amp; k) {<!-- -->
    double red = r / 255.0;
    double green = g / 255.0;
    double blue = b / 255.0;

    double maxVal = std::max({<!-- --> red, green, blue });

    // Calculate the black component (Black)
    k = 1 - maxVal;

    // Avoid dividing by 0
    if (k == 1) {<!-- -->
        c = m = y = 0;
    }
    else {<!-- -->
        // Calculate the cyan component (Cyan)
        c = (1 - red - k) / (1 - k);
        // calculate magenta component (Magenta)
        m = (1 - green - k) / (1 - k);
        // Calculate the yellow component (Yellow)
        y = (1 - blue - k) / (1 - k);
    }
}

The returned CMYK component will be between 0 and 1, where 0 means no color component and 1 means maximum (black). Note that for the black component, a value of 1 means pure black.

The above are commonly used color space conversion methods, and a suitable conversion method can be adopted according to the project requirements to realize the conversion of the image color space.