我怎么做相当于Gimp的颜色,自动,白平衡在PythonFu?

2024-10-05 18:58:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我能找到的唯一函数是:gimp color balance,它接受适用的参数:preserve lum(osity)、青色红色、品红绿色和黄蓝色。在

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


Tags: 函数标题参数菜单color蓝色balance绿色
3条回答

根据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的“Colors->Auto->White Balance”之后的结果非常相同

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

^{pr2}$

为了完成@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

我用它效果很好,试试看!在

根据我在快速查看源代码(以及或多或少通过测试图像确认)后的理解,这些代码是不相关的,并且是隐藏的,Colors>Auto>White Balance

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

用合成图像证明:

在此之前:

enter image description here

之后:

enter image description here

所有这些在Python中并不难实现。在

相关问题 更多 >