使用Python分析图像亮度有哪些方法? [英] What are some methods to analyze image brightness using Python?

查看:2301
本文介绍了使用Python分析图像亮度有哪些方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在python中执行简单的图像分析。我需要计算图像亮度的值。我知道 PIL 是用于执行此类操作的goto库。有一个内置的直方图功能。

I'd like some advice on performing a simple image analysis in python. I need to calculate a value for the "brightness" of an image. I know PIL is the goto library for doing something like this. There is a built-in histogram function.

我需要的是一个感知亮度值我可以决定是否需要进一步调整图像。那么在这种情况下哪些基本技术会起作用呢?我应该只使用RGB值,还是直方图会给我一些足够接近的东西?

What I need is a "perceived brightness" values I can decide if further adjustments to the image are necessary. So what are something of the basic techniques that will work in this situation? Should I just work with the RGB values, or will histogram give me something close enough?

一种可能的解决方案可能是将两者合并,并生成平均值R,G,和B值使用直方图,然后应用感知亮度公式。

One possible solution might be to combine the two, and generate average R,G,and B values using the histogram, then apply the "perceived brightness" formula.

推荐答案

使用问题中提到的技术,我提出了几个不同的版本。

Using the techniques mentioned in the question, I came up with a few different versions.

每个方法返回一个值close,但与其他方法不完全相同。此外,除最后一个方法外,所有方法都以相同的速度运行,这取决于图像大小,速度要慢得多。

Each method returns a value close, but not exactly the same as the others. Also, all methods run about the same speed except for the last one, which is much slower depending on the image size.


  1. 将图像转换为灰度,返回平均像素亮度。

  1. Convert image to greyscale, return average pixel brightness.

def brightness( im_file ):
   im = Image.open(im_file).convert('L')
   stat = ImageStat.Stat(im)
   return stat.mean[0]


  • 将图像转换为灰度,返回RMS像素亮度。

  • Convert image to greyscale, return RMS pixel brightness.

    def brightness( im_file ):
       im = Image.open(im_file).convert('L')
       stat = ImageStat.Stat(im)
       return stat.rms[0]
    


  • 平均像素,然后转换为感知亮度。

  • Average pixels, then transform to "perceived brightness".

    def brightness( im_file ):
       im = Image.open(im_file)
       stat = ImageStat.Stat(im)
       r,g,b = stat.mean
       return math.sqrt(0.241*(r**2) + 0.691*(g**2) + 0.068*(b**2))
    


  • 像素的RMS,t hen转换为感知亮度。

  • RMS of pixels, then transform to "perceived brightness".

    def brightness( im_file ):
       im = Image.open(im_file)
       stat = ImageStat.Stat(im)
       r,g,b = stat.rms
       return math.sqrt(0.241*(r**2) + 0.691*(g**2) + 0.068*(b**2))
    


  • 计算像素的感知亮度,然后返回平均值。

  • Calculate "perceived brightness" of pixels, then return average.

    def brightness( im_file ):
       im = Image.open(im_file)
       stat = ImageStat.Stat(im)
       gs = (math.sqrt(0.241*(r**2) + 0.691*(g**2) + 0.068*(b**2)) 
             for r,g,b in im.getdata())
       return sum(gs)/stat.count[0]
    


  • 更新测试结果
    我对200张图片进行了模拟。我发现方法#2,#4给出了几乎相同的结果。方法#3,#5也几乎相同。方法#1紧跟#3,#5(除少数例外)。

    Update Test Results I ran a simulation against 200 images. I found that methods #2, #4 gave almost identical results. Also methods #3, #5 were also nearly identical. Method #1 closely followed #3, #5 (with a few exceptions).

    这篇关于使用Python分析图像亮度有哪些方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆