2

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?

Gaffi
  • 4,307
  • 8
  • 43
  • 73
Ming Lu
  • 21
  • 1
  • 5

1 Answers1

1

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.
}
  • This is not always true. Sometimes the values are different. The ratios are supposed to be the same. i.e. One could compute the saturation of each pixel and use that as an indicator. – AturSams Apr 12 '14 at 18:18