본문 바로가기
Computer Science/영상처리

(C/C++) Color Model

by BaekDaBang 2024. 3. 27.

1. RGB

 

2. RGB Images

// Convert
for (int i = 0; i < height; i++) {
  for (int j = 0; j < width; j++) {
    // Red
     outputImg_1[j * stride + 3 * i + 0] = inputImg[j * stride + 3 * i + 0];
     outputImg_1[j * stride + 3 * i + 1] = 0;
     outputImg_1[j * stride + 3 * i + 2] = 0;

    // Green
     outputImg_2[j * stride + 3 * i + 0] = 0;
     outputImg_2[j * stride + 3 * i + 1] = inputImg[j * stride + 3 * i + 1];
     outputImg_2[j * stride + 3 * i + 2] = 0;

    // Blue
     outputImg_3[j * stride + 3 * i + 0] = 0;
     outputImg_3[j * stride + 3 * i + 1] = 0;
     outputImg_3[j * stride + 3 * i + 2] = inputImg[j * stride + 3 * i + 2];
  }
}

 

3. YCbCr

Y : Luminance(휘도)

Cb, Cr : Color Difference

// Convert
for (int i = 0; i < height; i++) 
{
    for (int j = 0; j < width; j++) 
    {
        // Original Copy
        outputImg[j * stride + 3 * i + 0] = inputImg[j * stride + 3 * i + 0];
        outputImg[j * stride + 3 * i + 1] = inputImg[j * stride + 3 * i + 1];
        outputImg[j * stride + 3 * i + 2] = inputImg[j * stride + 3 * i + 2];

        // RGB -> YCbCr
        Y = 0.299 * inputImg[j * stride + 3 * i + 2] + 0.587 * inputImg[j * stride + 3 * i + 1] + 0.114 * inputImg[j * stride + 3 * i + 0];
        Cb = -0.169 * inputImg[j * stride + 3 * i  + 2] - 0.331 * inputImg[j * stride + 3 * i + 1] + 0.500 * inputImg[j * stride + 3 * i + 0];
        Cr = -0.500 * inputImg[j * stride + 3 * i  + 2] - 0.419 * inputImg[j * stride + 3 * i + 1] + 0.500 * inputImg[j * stride + 3 * i + 0];
        outputImg[j * stride + 3 * i + 0] = (unsigned char)(Y > 255 ? 255 : (Y < 0 ? 0 : Y));
        outputImg[j * stride + 3 * i + 1] = (unsigned char)(Cb > 255 ? 255 : (Cb < 0 ? 0 : Cb));
        outputImg[j * stride + 3 * i + 2] = (unsigned char)(Cr > 255 ? 255 : (Cr < 0 ? 0 : Cr));
    }
}
// Convert
for (int i = 0; i < height; i++) 
{
    for (int j = 0; j < width; j++) 
    {
        // Original Copy
        outputImg[j * stride + 3 * i + 0] = inputImg[j * stride + 3 * i + 0];
        outputImg[j * stride + 3 * i + 1] = inputImg[j * stride + 3 * i + 1];
        outputImg[j * stride + 3 * i + 2] = inputImg[j * stride + 3 * i + 2];

        // YCbCr -> RGB
        R = Y + 1.402 * Cr;
        G = Y - 0.714 * Cr - 0.344 * Cb;
        B = Y + 1.772 * Cb;
        outputImg[j * stride + 3 * i + 0] = (unsigned char)(R > 255 ? 255 : (R < 0 ? 0 : R));
        outputImg[j * stride + 3 * i + 1] = (unsigned char)(G > 255 ? 255 : (G < 0 ? 0 : G));
        outputImg[j * stride + 3 * i + 2] = (unsigned char)(B > 255 ? 255 : (B < 0 ? 0 : B));
    }
}

 

4. 24 Bit MAP

    // Convert
    for (int i = 0; i < height; i++) 
    {
        for (int j = 0; j < width; j++) 
        {
            // Original Copy
            outputImg[j * stride + 3 * i + 0] = inputImg[j * stride + 3 * i + 0];
            outputImg[j * stride + 3 * i + 1] = inputImg[j * stride + 3 * i + 1];
            outputImg[j * stride + 3 * i + 2] = inputImg[j * stride + 3 * i + 2];

			// RGB -> YYY
            Y = 0.299 * inputImg[j * stride + 3 * i + 2] + 0.587 * inputImg[j * stride + 3 * i + 1] + 0.114 * inputImg[j * stride + 3 * i + 0];
            outputImg[j * stride + 3 * i + 0] = (unsigned char)(Y > 255 ? 255 : (Y < 0 ? 0 : Y));
            outputImg[j * stride + 3 * i + 1] = (unsigned char)(Y > 255 ? 255 : (Y < 0 ? 0 : Y));
            outputImg[j * stride + 3 * i + 2] = (unsigned char)(Y > 255 ? 255 : (Y < 0 ? 0 : Y));
        }
    }

 

5. Other Model

(1) CMY

Cyan(청록색), Magenta(자홍색), Yellow(노랑색)

(2) HSI

Hue(색), Saturation(채도), Intensity(밝기)

'Computer Science > 영상처리' 카테고리의 다른 글

(C/C++) Filter (Denoising)  (0) 2024.04.01
(C/C++) Quality Evaluation(PSNR)  (0) 2024.03.27
(C/C++) Pixel Operation  (0) 2024.03.27
(C/C++) Image Format  (0) 2024.03.27
(C/C++) Resolution(해상도)  (0) 2024.03.27