与ChunkyPNG的标准偏差

时间:2013-11-27 17:53:08

标签: ruby chunkypng

我试图使用ChunkyPNG找到图像的对比度。有没有办法使用 ChunkyPNG 来获得图像的标准偏差?

1 个答案:

答案 0 :(得分:2)

查看ChunkyPNG代码,我找不到任何统计模块。

但您可以使用以下方法:

image = ChunkyPNG::Image.from_file('any PNG image file')

# @return [Hash] some statistics based on image pixels
def compute_image_stats(image, &pixel_transformation)
  # compute pixels values
  data  = image.pixels.map {|pixel| yield(pixel)} # apply the pixel convertion

  # compute stats
  n         = data.size # sum of pixels
  mean      = data.inject(:+).to_f / n
  variance  = data.inject(0) {|sum, item| sum += (item - mean)**2} / n
  sd        = Math.sqrt(variance) # standard deviation

  {mean: mean, variance: variance, sd: sd}
end

# compute stats for grayscale image version
compute_image_stats(image) {|pixel| ChunkyPNG::Color.grayscale_teint(pixel)}
# compute stats for blue channel
compute_image_stats(image) {|pixel| ChunkyPNG::Color.b(pixel)}

我在回报中包含了所有统计数据,因为它们是针对标准差(sd)计算计算的。