bash 生成一个值来反映图像的平均亮度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4240775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 22:57:20  来源:igfitidea点击:

Generate a value to reflect the average brightness of an image

linuximagebashbrightness

提问by dibs

I need to determine if an image is above a certain brightness. Using a scale of 0 - 255 I want to generate a value within this range to reflect the image brightness.

我需要确定图像是否高于特定亮度。使用 0 - 255 的比例,我想在这个范围内生成一个值来反映图像亮度。

i.e. a white image is 255 and a black image is 0.

即白色图像为 255,黑色图像为 0。

All this needs to take place via a bash script I am building up at the moment. I have no idea what image lib could do this for me though.

所有这些都需要通过我目前正在构建的 bash 脚本进行。我不知道什么图像库可以为我做这件事。

回答by GreyCat

Generally, it's one of the classic problems of signal processing and there are several approaches, based on how do you define "brightness". It's generally the same for "brightness" of an image, "loudness" of a sound signal, etc.

通常,这是信号处理的经典问题之一,根据您如何定义“亮度”,有几种方法。图像的“亮度”、声音信号的“响度”等通常是相同的。

Some ideas of what you can use as a generic "brightness" is:

您可以用作通用“亮度”的一些想法是:

  • Average value of all the pixels (i.e. sum up all the brightnesses of all the pixels, then divide by total amount of pixels, i.e. width * height).
  • Build up a histogram of brightness distribution, then choose such point xin that histogram that 98% (95%, 90%, 70% - this number can vary) of all the pixels in your image would be less bright than this x. It's called percentile.
  • Calculate "root mean square" (RMS)for all the pixels (sum up squares of all the pixels, divide by total amount of pixels, extract square root from this one).
  • 所有像素的平均值(即所有像素的所有亮度相加,然后除以像素总数,即宽*高)。
  • 建立亮度分布的直方图,然后在该直方图中选择这样的点x,使图像中所有像素的 98%(95%、90%、70% - 这个数字可以变化)比这个x更不亮。这叫做百分位。
  • 计算所有像素的“均方根”(RMS)(将所有像素的平方相加,除以像素总数,从中提取平方根)。

There are multiple image libraries that can yield good results. The easiest one to use from a shell script is probably ImageMagick/GraphicsMagick - you can get both simple averages and do some more complex histogramming to check out percentiles if you want to.

有多个图像库可以产生良好的结果。从 shell 脚本中最容易使用的可能是 ImageMagick/GraphicsMagick - 如果您愿意,您可以获得简单的平均值并进行一些更复杂的直方图以检查百分位数。

回答by Jonas Elfstr?m

Try ImageMagick gray statsor histogram

尝试 ImageMagick灰色统计数据直方图

convert rose: -colorspace gray -format "%[fx:100*mean]%%" info: