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

Image preprocessing

Computer vision image preprocessing refers to a series of processing and conversion of images before image processing to facilitate 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 framework diagram
Today we mainly talk about color space conversion. Others are mentioned in the image enhancement algorithm and sharpening algorithm .

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. Here are several common color space conversion methods:

RGB to HSV conversion

The RGB color space contains three components: red, green, and blue, while the HSV color space contains three components: 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:
		如果max_val = min_val,则Hue = 0(定义为红色)
		如果max_val = R,则Hue = (G - B) / (max_val - min_val)
		如果max_val = G,则Hue = 2 + (B - R) / (max_val - min_val)
		如果max_val = B,则Hue = 4 + (R - G) / (max_val - min_val)
  • Calculate saturation:
	如果max_val = 0,则Saturation = 0
	否则,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

    # 计算色调
    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))

    # 计算饱和度
    if max_val != 0:
        saturation = (max_val - min_val) / max_val

    # 计算值
    value = max_val
  • C++ implementation
void RGBtoHSV(int r, int g, int b, double& h, double& s, double& 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;

    // 计算色调 (Hue)
    if (delta == 0) {
    
    
        h = 0;  // 无色彩,色调为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;  // 调整为0到360度之间
    }

    // 计算饱和度 (Saturation)
    if (maxVal == 0) {
    
    
        s = 0;
    }
    else {
    
    
        s = delta / maxVal;
    }

    // 计算值 (Value)
    v = maxVal;
}

HSV to RGB conversion

The hue (Hue), saturation (Saturation) and value (Value) in the HSV color space can be converted into 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 based on the value of Hue, and perform the corresponding calculation:
	将Hue乘以6得到一个角度值,记为H
	将H向下取整得到一个整数部分,记为I
	计算H减去I后的小数部分,记为F
	计算P = Value × (1 - Saturation)
	计算Q = Value × (1 - Saturation × F)
	计算T = Value × (1 - Saturation × (1 - F)
	根据I的值选择相应的计算公式计算R、G、B的值:
		如果I = 0,则R = Value,G = T,B = P
		如果I = 1,则R = Q,G = Value,B = P
		如果I = 2,则R = P,G = Value,B = T
		如果I = 3,则R = P,G = Q,B = Value
		如果I = 4,则R = T,G = P,B = Value
		如果I = 5,则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& r, int& g, int& 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 the hue should be between 0 and 360, and the saturation and value should be between 0 and 1. The RGB value returned will be between 0 and 255.

RGB to CMYK conversion

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

  • Calculate Cyan: Cyan = 1 - (Red / 255)
  • Calculation Magenta: Magenta = 1 - (Green / 255)
  • Calculate Yellow: Yellow = 1 - (Blue / 255)
  • Calculation 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& c, double& m, double& y, double& k) {
    
    
    double red = r / 255.0;
    double green = g / 255.0;
    double blue = b / 255.0;

    double maxVal = std::max({
    
     red, green, blue });

    // 计算黑色分量 (Black)
    k = 1 - maxVal;

    // 避免除以0的情况
    if (k == 1) {
    
    
        c = m = y = 0;
    }
    else {
    
    
        // 计算青色分量 (Cyan)
        c = (1 - red - k) / (1 - k);
        // 计算洋红分量 (Magenta)
        m = (1 - green - k) / (1 - k);
        // 计算黄色分量 (Yellow)
        y = (1 - blue - k) / (1 - k);
    }
}

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

The above are all commonly used color space conversion methods. Suitable conversion methods can be adopted according to project requirements to achieve image color space conversion.

Guess you like

Origin blog.csdn.net/weixin_43504942/article/details/130854615