简体   繁体   English

如何获得 CImg 像素的位深度?

[英]How to get the bit depth of a CImg pixel?

I'm trying to work out how many colour possibilities are used per pixel for any given image.我正在尝试计算任何给定图像的每个像素使用多少种颜色可能性。

For example, if an image uses 8 bits per pixel, then it can represent one of 256 shades.例如,如果图像每像素使用 8 位,则它可以表示 256 种阴影中的一种。

I'm looking for something like the following:我正在寻找类似以下的内容:

CImg<unsigned char> inputImage(inputImageFilename.c_str());
CImgDisplay disp_input(inputImage,"input");

std::cout << sizeof(inputImage[0]);

I know that this particular image has 8 bit pixel depth.我知道这个特定的图像有 8 位像素深度。 I was hoping this would output 8, which I could then use as the exponent of 2 to get 256 (2^8 = 256).我希望这将是 output 8,然后我可以将其用作 2 的指数以获得 256 (2^8 = 256)。 But it outputs 1, so this is not an option.但它输出 1,所以这不是一个选项。

I've also tried .depth() but quickly realised this does not refer to the pixel depth.我也试过.depth()但很快意识到这不是指像素深度。

Can someone help me out?有人可以帮我吗?

Two things here:这里有两件事:

  1. The documentation states:文档指出:

    Class representing an image (up to 4 dimensions wide), each pixel being of type T. Class 表示图像(最多 4 维宽),每个像素的类型为 T。

    which means the pixel depth is defined by the template type T. In your case this is unsigned char which results in a pixel depth of 8. If you want to have a pixel depth of 16 you could use CImg<uint16_t> .这意味着像素深度由模板类型 T 定义。在您的情况下,这是unsigned char导致像素深度为 8。如果您希望像素深度为 16,您可以使用CImg<uint16_t>

  2. Depending on the file type you are reading you can determine the bit depth.根据您正在阅读的文件类型,您可以确定位深度。 Jpeg for example has a bit depth of 8, while png can have a bit depth of 8 or 16 (at least that's whats supported by CImg).例如,Jpeg 的位深度为 8,而 png 的位深度为 8 或 16(至少 CImg 支持这一点)。 If you have a png file and want to know the bit depth you can use the function load_png() as follows:如果你有一个 png 文件并且想知道位深度,你可以使用 function load_png()如下:

     CImg<unsigned char> inputImage(); unsigned int bit_depth; inputImage.load_png(inputImageFilename.c_str(), &bit_depth); std::cout << bit_depth;

    Since I used unsigned char as type TI will only have access to the first 8 bits even if the bit depth of the file is 16. Internally the image data is saved as unsigned short (aka 16 bit) if bit_depth == 16 .由于我使用unsigned char作为类型,即使文件的位深度为 16,TI 也只能访问前 8 位。如果bit_depth == 16 ,则图像数据在内部保存为 unsigned short(又名 16 位)。 So the following should be possible:所以以下应该是可能的:

     if (bit_depth == 16) CImg<unsigned short> newImage(inputImage);

    The bit depth can of course also read from the exif data of the file.位深当然也可以从文件的exif数据中读取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM