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?
I have tried as follows, but failed:
cimg_library::CImg<unsigned char> img("lena_gray.bmp");
const int spectrum = img.spect开发者_JAVA百科rum();
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?
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).
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.
}
精彩评论