如何在Python-Fu中完成相当于Gimp的Color,Auto,White Balance的操作?

问题描述 投票:2回答:4

我能找到的唯一功能是:gimp-color-balance,它采用适用的参数:preserve-lum(osity),cyan-red,magenta-green和yellow-blue。

我不确定要为这些参数传递什么值以复制标题中的菜单选项。

python scripting gimp
4个回答
0
投票

为了完成@ banderlog013的答案,我认为Gimp Doc指定首先丢弃每个通道的最终像素,然后拉伸剩余的范围。我相信正确的代码是:

img = cv2.imread('test.jpg')
balanced_img = np.zeros_like(img) #Initialize final image

for i in range(3): #i stands for the channel index 
    hist, bins = np.histogram(img[..., i].ravel(), 256, (0, 256))
    bmin = np.min(np.where(hist>(hist.sum()*0.0005)))
    bmax = np.max(np.where(hist>(hist.sum()*0.0005)))
    balanced_img[...,i] = np.clip(img[...,i], bmin, bmax)
    balanced_img[...,i] = (balanced_img[...,i]-bmin) / (bmax - bmin) * 255

我用它取得了很好的效果,试一试!


0
投票

根据GIMP doc的说法,我们需要在红色,绿色和蓝色直方图的每一端丢弃像素颜色,这些直方图仅占图像中0.05%的像素,并尽可能地拉伸剩余范围(Python代码):

img = cv2.imread('test.jpg')
x = []
# get histogram for each channel
for i in cv2.split(img):
    hist, bins = np.histogram(i, 256, (0, 256))
    # discard colors at each end of the histogram which are used by only 0.05% 
    tmp = np.where(hist > hist.sum() * 0.0005)[0]
    i_min = tmp.min()
    i_max = tmp.max()
    # stretch hist
    tmp = (i.astype(np.int32) - i_min) / (i_max - i_min) * 255
    tmp = np.clip(tmp, 0, 255)
    x.append(tmp.astype(np.uint8))

# combine image back and show it
s = np.dstack(x)
plt.imshow(s[::,::,::-1])

结果与GIMP的“颜色 - >自动 - >白平衡”之后的结果非常相似

UPD:我们需要np.clip(),因为OpenCVnumpy不同地将int32转换为uint8:

# Numpy
np.array([-10, 260]).astype(np.uint8)
>>> array([246,   4], dtype=uint8)
# but we need just [0, 255]

-1
投票

根据我的理解,快速查看源代码(或多或少用测试图像确认),这些是无关的,并且在引擎盖下,Colors>Auto>White Balance

  • 获得每个通道的直方图
  • 得到确定底部和顶部0.6%的值
  • 使用这两个值作为黑色和白色点,使用与“级别”非常相似的内部调用,拉伸该通道的值范围。

使用合成图像证明:

之前:

enter image description here

后:

enter image description here

所有这些在Python中并不难做到。


-1
投票

K,很酷。弄清楚如何编写脚本。 如果你愿意,可以使用它。好吧,我。

https://github.com/doyousketch2/eAWB

pic

© www.soinside.com 2019 - 2024. All rights reserved.