在裸 C 中将 RGB 转换为灰度

Convert RGB to Grayscale in bare C

我正在应用 C 中的图像处理基础教程,我在使用此程序将 RGB 转换为灰度时遇到问题,但输出图片不知何故已损坏,尽管代码运行没有错误,但我无法将我的解决问题。代码如下。

FILE *fIn = fopen("tiger.bmp","rb");                    //Input File name
FILE *fOut = fopen("tiger_gray.bmp","wb");              //Output File name

int i,j,y;
unsigned char byte[54];

if(fIn==NULL)   
{
    printf("File does not exist.\n");
}

for(i=0;i<54;i++)               //read the 54 byte header from fIn
{
    byte[i] = getc(fIn);
}

fwrite(byte,sizeof(unsigned char),54,fOut);         //write the header back
// extract image height, width and bit Depth from image Header
int height = *(int*)&byte[18];
int width = *(int*)&byte[22];
int bitDepth = *(int*)&byte[28];

printf("width: %d\n",width);
printf("height: %d\n",height );

int size = height*width;                        

unsigned char buffer[size][3];              //to store the image data


for(i=0;i<size;i++)                                         //RGB to gray
{
    y=0;
    buffer[i][2]=getc(fIn);                                 //blue
    buffer[i][1]=getc(fIn);                                 //green
    buffer[i][0]=getc(fIn);                                 //red

    y=(buffer[i][0]*0.3) + (buffer[i][1]*0.59)  + (buffer[i][2]*0.11);          //conversion formula of rgb to gray

    putc(y,fOut);
    putc(y,fOut);
    putc(y,fOut);
}

fclose(fOut);
fclose(fIn);

您的代码中存在两个主要问题。

  1. 您将宽度和高度颠倒了。
  2. 你没有考虑 每行末尾所需的填充以使其成为倍数 4 个字节。

根据其余代码的编写方式,您还分配了一个不需要的大缓冲区。一般来说,我更喜欢 read/process 一次一整行,甚至一次完整图像,但要做到这一点,您需要使用 malloc 或 calloc,因为数据可能比可用堆栈大。在这种情况下,为了简单起见,我一次只处理一个像素。

我也摆脱了 getc/putc 因为我更喜欢 fread/fwrite 而且你从来没有真正一次处理 1 个字节。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    FILE *fIn = fopen("tiger.bmp", "rb");
    FILE *fOut = fopen("tiger_gray.bmp", "wb");
    if (!fIn || !fOut)
    {
        printf("File error.\n");
        return 0;
    }

    unsigned char header[54];
    fread(header, sizeof(unsigned char), 54, fIn);
    fwrite(header, sizeof(unsigned char), 54, fOut);

    int width = *(int*)&header[18];
    int height = abs(*(int*)&header[22]);
    int stride = (width * 3 + 3) & ~3;
    int padding = stride - width * 3;

    printf("width: %d (%d)\n", width, width * 3);
    printf("height: %d\n", height);
    printf("stride: %d\n", stride);
    printf("padding: %d\n", padding);

    unsigned char pixel[3];
    for (int y = 0; y < height; ++y)
    {
        for (int x = 0; x < width; ++x)
        {
            fread(pixel, 3, 1, fIn);
            unsigned char gray = pixel[0] * 0.3 + pixel[1] * 0.58 + pixel[2] * 0.11;
            memset(pixel, gray, sizeof(pixel));
            fwrite(&pixel, 3, 1, fOut);
        }
        fread(pixel, padding, 1, fIn);
        fwrite(pixel, padding, 1, fOut);
    }
    fclose(fOut);
    fclose(fIn);
    return 0;
}