CImg:如何保存灰度?
当我使用CImg
加载.BMP
时,我如何知道它是灰度图像还是彩色图像? 我尝试了如下,但失败了:
cimg_library::CImg<unsigned char> img("lena_gray.bmp");
const int spectrum = img.spectrum();
img.save("lenaNew.bmp");
按照我的预期,无论我加载哪种.BMP
,频谱总是3。因此,当我加载灰度并保存时它,结果大小将比实际大 3 倍。
我只想保存加载时相同的图像。如何保存为灰度?
When I use CImg
to load a .BMP
, how can I know whether it is a gray-scale or color image?
I have tried as follows, but failed:
cimg_library::CImg<unsigned char> img("lena_gray.bmp");
const int spectrum = img.spectrum();
img.save("lenaNew.bmp");
To my expectation, no matter what kind of .BMP
I have loaded, spectrum will always be 3. As a result, when I load a gray-scale and save it, the result size will be 3 times bigger than it is.
I just want to save a same image as it is loaded. How do I save as gray-scale?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜想 BMP 格式总是将图像存储为 RGB 编码数据,因此读取 BMP 总是会生成彩色图像。
如果您知道您的图像是标量,则所有通道都将相同,因此您可以丢弃其中两个(此处保留第一个)。
如果您想检查它是否是标量图像,您可以测试通道之间的相等性,如下所示
I guess the BMP format always store images as RGB-coded data, so reading a BMP will always result in a color image.
If you know your image is scalar, all channels will be the same, so you can discard two of them (here keeping the first one).
If you want to check that it is a scalar image, you can test the equality between channels, as