【问题标题】:Convert RGB to Grayscale in bare C在裸 C 中将 RGB 转换为灰度
【发布时间】:2019-06-07 23:34:06
【问题描述】:

我正在应用 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);

【问题讨论】:

  • 请将其转换为minimal reproducible example 并解释/显示“不知何故损坏”的含义。
  • getc() 我怀疑是从图片中读取二进制数据的合适函数。阅读规范。更喜欢将二进制文件读写到缓冲区中。
  • 您确定这是读取标题的正确方法吗?我得到一个带有jpeg 图像的height 的负值?
  • 您没有注意到位图的每一行都必须填充为 4 个字节的倍数。
  • @FrancescoBoi 这是 BMP 特定代码,因此它不适用于 JPG。在 BMP 文件中,负高度表示像素是正面朝上而不是倒置存储的。

标签: c image-processing rgb grayscale


【解决方案1】:

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

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

根据其余代码的编写方式,您还分配了一个不需要的大缓冲区。通常,我更喜欢一次读取/处理一整行,甚至一次读取/处理完整图像,但要做到这一点,您需要使用 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;
}

【讨论】:

  • 现在对我来说效果很好。我什至得到了更好的结果,只是得到了 rgb 值的平均值,而不是使用方程。非常感谢!
  • 在 memset 像素为 'buggy' 后 Fwrite
  • “越野车”怎么样?你能解释一下吗?
猜你喜欢
  • 2020-04-29
  • 1970-01-01
  • 1970-01-01
  • 2010-10-15
  • 2013-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多