【问题标题】:get the max of R,G,B of one image by using programming [closed]通过使用编程获得一张图像的 R、G、B 的最大值 [关闭]
【发布时间】:2014-09-10 08:20:12
【问题描述】:

在photoshop中,通常需要手动调整设置R、G或B的最大值。

是否有任何图像库(python、java、C#。)来获取、计算或设置 R、G 和 B 的最大值?

我查看了 PIL(python 图像库),但没有找到重点。

谢谢

【问题讨论】:

    标签: java php python c


    【解决方案1】:

    获得最大红色、绿色、蓝色值,您可以使用 Python 库 SciPy:

    from scipy import misc
    
    img = misc.imread("lena.bmp")
    
    print "R:", img[:, :, 0].max()
    print "G:", img[:, :, 1].max()
    print "B:", img[:, :, 2].max()
    

    输出:

    R: 255
    G: 238
    B: 255
    

    设置某个颜色通道的最大值,您可以将所有像素乘以一个比例因子,例如

    factor = 240.0 / img[:, :, 0].max()
    img[:, :, 0] *= factor
    print "new R:", img[:, :, 0].max()
    

    输出:

    new R: 240
    

    以防万一您需要同时设置最小值和最大值

    newMinR = 50.0
    newMaxR = 240.0
    
    factor = (newMaxR - newMinR) / (img[:, :, 0].max() - img[:, :, 0].min())
    shift = newMinR - factor * img[:, :, 0].min()
    
    img[:, :, 0] = factor * img[:, :, 0].astype('float') + shift
    print "new R:", img[:, :, 0].min(), img[:, :, 0].max()
    

    输出:

    new R: 50 240
    

    (计算factorshift 的公式可以从newR = factor * oldR + shift 之类的线性函数导出。确保使用浮点数而不是uint8!)

    【讨论】:

    • 这就是我想要的。非常感谢法尔科
    • 所有图片都有 .jpg 是真的吗?我总是得到 R: 255 G: 255 B: 255
    • @kn3l:我不明白你的问题。
    • 回溯(最近一次调用最后):文件“checkrgb.​​py”,第 5 行,在
    • @kn3l:看起来您的图像只有一个灰色通道。所以索引0 是错误的。您仍然可以获得最大灰度值img[:, :].max() 或简单地img.max()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-19
    • 2013-05-11
    • 1970-01-01
    • 2020-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多