[英]CImg: How to save a grayscale?
When I use CImg
to load a .BMP
, how can I know whether it is a gray-scale or color image? 使用CImg
加载.BMP
,如何知道它是灰度图像还是彩色图像? 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. 出乎我的意料,无论我加载的是哪种.BMP
,光谱始终为3。因此,当我加载灰度并保存时,结果大小将比原来大3倍。
I just want to save a same image as it is loaded. 我只想保存与加载相同的图像。 How do I save as gray-scale? 如何保存为灰度?
I guess the BMP format always store images as RGB-coded data, so reading a BMP will always result in a color image. 我猜想BMP格式总是将图像存储为RGB编码数据,因此读取BMP总是会生成彩色图像。 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). 如果您知道图像是标量的,则所有通道都将相同,因此您可以丢弃其中两个通道(此处保留第一个通道)。
img.channel(0);
If you want to check that it is a scalar image, you can test the equality between channels, as 如果要检查它是否为标量图像,可以测试通道之间的相等性,如下
const CImg<unsigned char> R = img.get_shared_channel(0),
G = img.get_shared_channel(1),
B = img.get_shared_channel(2);
if (R==G && R==B) {
.. Your image is scalar !
} else {
.. Your image is in color.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.