简体   繁体   English

如何使用CImg检索已加载CImg的图像的色彩空间?

[英]How do you retrieve the colour space with CImg of an image loaded with CImg?

I am loading an image with CIMG, like so: 我正在用CIMG加载图像,如下所示:

CImg<unsigned char> image("lena.png");

How do I know the eg four channels of that image represent RGBA colours and not CMYK? 我怎么知道该图像的例如四个通道代表RGBA颜色而不是CMYK? I know CImg<>::spectrum() gives me the amount of channels, but even the documentation says "The meaning of the pixel values is not linked up to the number of channels (eg a 4-channel image may indifferently stands for a RGBA or CMYK color image)" . 我知道CImg<>::spectrum()为我提供了通道数量,但即使文档中也说: “像素值的含义与通道数量没有关系(例如4通道图像可能无意中代表了RGBA或CMYK彩色图像)”

I have searched the documentation and tutorials, but there seems to be no function to tell me the meaning of the channel data. 我已经搜索了文档和教程,但是似乎没有任何功能可以告诉我频道数据的含义。

So, how do I know kind of data I'm working with? 那么,我如何知道正在使用的数据类型? Can I just assume all newly loaded image are in either RGB or RGBA? 我是否可以仅假设所有新加载的图像均为RGB或RGBA?

If your image is in PNG format, it is necessarily RGB or RGBA because PNG doesn't support CMYK. 如果您的图片为PNG格式,则必须为RGB或RGBA,因为PNG不支持CMYK。

If it is JPEG, I would assume it is in sRGB colourspace, but you could check with ImageMagick first, using: 如果是JPEG,我会假设它是sRGB色彩空间,但是您可以先使用ImageMagick进行检查,方法是:

identify -format "%[colorspace]" YourImage.xxx

I generated a CMYK image using Imagemagick and loaded it using CImg . 我使用Imagemagick生成了CMYK图像,并使用CImg加载了它。 It appears to perform no transformation whatsoever as a result of the colorspace and merely returns the first byte of each pixel as red (though it is cyan in reality), the second as green (though it is magenta) and so on... 由于色彩空间的缘故,它似乎不执行任何转换,而仅将每个像素的第一个字节返回为红色(实际上是青色),第二个返回为绿色(虽然是洋红色),依此类推...

I also compiled CImg with debugging info, with cimg_use_jpeg defined as 1 (so it uses libjpeg ) and stepped through it. 我还使用调试信息编译了CImg ,并将cimg_use_jpeg定义为1 (因此它使用libjpeg )并逐步执行。 It could see in the jpeg_info_struct in function _load_jpeg that the image is CMYK, but it ignores this info - I guess you could patch CImg and modify a global variable quite readily to store the colourspace - if you like hacking about! 可以在函数_load_jpeg中的jpeg_info_struct中看到该图像是CMYK,但是它忽略了此信息-我想您可以修补CImg并很容易地修改全局变量以存储色彩空间-如果您喜欢黑客! Or you could request David Tschumperlé to do so via the CImg GitHub page which he does respond to... 或者,您可以通过CImg GitHub页面请求DavidTschumperlé这样做,他确实对此进行了回复...

在此处输入图片说明


Here is a possible work-around. 这是一个可能的解决方法。 If you don't build with cimg_use_jpeg , or cimg_use_png , or cimg_use_tiff , CImg will delegate reading files to ImageMagick 's command-line tool called convert . 如果您不使用cimg_use_jpegcimg_use_pngcimg_use_tiff ,则CImg会将读取文件委托给ImageMagick的名为convert的命令行工具。 It will then convert your image to PNM format (in a pipe, not on disk) and CImg will read the data from the pipe. 然后它将图像转换为PNM格式(在管道中,不在磁盘上), CImg将从管道中读取数据。 Crucially, ImageMagick always converts images being written to PNM format into sRGB colourspace, so if you have ImageMagick convert your images (by not using CImg 's built-in libraries), your images will always be read in sRGB colourspace. 至关重要的是, ImageMagick始终将以 PNM格式写入的图像转换为sRGB色彩空间,因此,如果您有ImageMagick转换图像(通过不使用CImg的内置库),则您的图像将始终以sRGB色彩空间读取。 Essentially, CImg does this to read files when delegating to ImageMagick : 本质上, CImg这样做是在委派给ImageMagick时读取文件:

convert input.jpg pnm:-

and reads the output. 并读取输出。

You will note that your 4-channel (previously CMYK) image suddenly becomes a 3-channel (now sRGB) image when CImg has delegated to ImageMagick because the output from ImageMagick is a, necessarily 3-channel, PNM file. 你会注意到,你的4通道(以前CMYK)图像突然变得时CIMG已授权ImageMagick的 3通道(现在的sRGB)图像,因为从ImageMagick的输出是一个必然3通道,PNM文件。


If you want to go even further... 如果您想走得更远...

If you set an environment variable, you can control where CImg looks for convert , so you could point it to your own "wrapper" around the real ImageMagick convert . 如果设置环境变量,则可以控制CImg哪里寻找convert ,因此可以将其指向真正的ImageMagick convert周围的自己的“包装器”

So, in concrete terms, if you do: 因此,具体来说,如果您这样做:

export cimg_convert_path="/Users/YourUser/bin/convertWrapper"

before running anything with CImg in it, or at the start of your program, with: 在其中运行任何带有CImg的程序之前或在程序开始时,执行以下操作:

setenv("cimg_convert_path","/Users/YourUser/bin/convertWrapper",1);

CImg will run the following command to load your files: CImg将运行以下命令来加载文件:

/Users/YourUser/bin/convertWrapper input.jpg pnm:-

So, I am suggesting that in that wrapper file, you could modify the parameters passed to convert so it actually does something else: 因此,我建议在该包装文件中,您可以修改传递来进行convert的参数,以便它实际执行其他操作:

convert input.jpg -auto-level pnm:-

So, convertWrapper would contain: 因此, convertWrapper将包含:

#!/bin/bash

# Pick up arguments and put in array
args=( "$@" )

# Remove last argument ("pnm:-")
unset 'args[${#args[@]}-1]'

# Add our "-auto-level" parameter in before last item
args+=("-auto-level")

# Re-append "pnm:-"
args+=("pnm:-")

# Debug output to /tmp/a
echo "BEFORE: $@"         >> /tmp/a
echo "AFTER:  ${args[@]}" >> /tmp/a

# Call real "convert" with modified parameters
/usr/local/bin/magick "${args[@]}"

This approach means you don't need to modify CImg . 这种方法意味着您不需要修改CImg

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

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