从RGB BMP创建灰度BMP [英] Create greyscale BMP from RGB BMP

查看:203
本文介绍了从RGB BMP创建灰度BMP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有24位图像,我读了位图,并将其转换为灰度并保存像8位。

I have 24-bit image, I read bitmap and transform it to the grayscale and save like 8-bit.

RGBTRIPLE temp;
unsigned char t;
...
t = (temp.rgbtBlue * 0.114 + temp.rgbtGreen * 0.587 + temp.rgbtRed * 0.299);
fwrite(&t, sizeof(UCHAR), 1, newFile);

在该图像没开,我明白我必须在水木清华头改变。
我试着文件,并在头位的大小,改变大小,但它并没有正常工作。

After that image didn't open, I understand I must smth to change in headers. I try change size of file and size of bitmap in headers, but it didn't working.

BITMAPFILEHEADER bfh;
BITMAPINFOHEADER bih;
...
bfh.bfSize = sizeof(UCHAR) * img.Width * img.Height + bfh.bfOffBits;
bih.biSizeImage = sizeof(UCHAR) * img.Width * img.Height;
bih.biCompression = BI_BITFIELDS;
bih.biBitCount = 8;

我需要像8位BMP更改保存图像?

What I need to change for save image like 8-bit BMP?

推荐答案

其实,最简单的方法是不改变在任何头。你读3个值(RGB),将它们转换使用标准的PAL / NTSC公式为灰色,然后你就可以输出所计算出的灰度值的3倍了。这样一来,你又得到1个像素,但改变的值。

Actually, the easiest way is not to change anything in the headers. You read 3 values (RGB), convert them to gray using the standard PAL/NTSC formula, and then you can output the calculated gray value 3 times back. That way, you get 1 pixel again, but with the altered value.

您只需更改只是头不为8位,因为工作,的颜色索引的形象,您还需要提供一个颜色索引地图 - 中的调色板。此外,根据原始图像的大小,你可能需要更改每行的步幅的(这就是它被称为 - 谷歌是你的朋友太)。

Your simply changing just the header doesn't work because for an 8-bit, color indexed image, you also need to provide a color index map -- the palette. In addition, depending on the original image size, you may need to change the stride of each row (that's what it is called -- Google is your friend too!).

正如马克·瑟特查的言论,BI_BITFIELDS是不是你在这里需要什么(维基百科上BMP )。使用BI_RGB一个真正的颜色或颜色索引图像;其他值的非常的专业 - 我从来没有见过他们在野外

As Mark Setchell remarks, BI_BITFIELDS is not what you need here (Wikipedia on BMP). Use BI_RGB for a true color or color indexed image; the other values are very specialized -- and I've never seen them "in the wild".

这篇关于从RGB BMP创建灰度BMP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆