gpt4 book ai didi

python - 如何通过灰度图像代码使用电平 Gamma /中间调 slider

转载 作者:太空宇宙 更新时间:2023-11-04 04:21:22 25 4
gpt4 key购买 nike

我有一张灰度图像,我想对其应用 Gamma 校正。手动调整 photoshop 级别中的中间 slider ,而不触及黑点或白点,直到直方图的中值达到 127 或接近它的某个位置。我想通过 python 做同样的事情。

我在寻找一个将 Gamma 值作为输入并生成输出图像的公式。然而,我只找到了一个调整低值和高值而不是 Gamma (或中间调)的方法。

这里,R 是我图像中像素的 RGB 红色值,作为一个 numpy 数组,H 是高输入,L 是低输入。我从 L=0 和 H=255 开始,不断向函数传递不同的值,直到返回的图像在直方图上的中位数为 127。

def get_levelcorrected_image(R,H,L):

temp=((R-L)*255.0)/(H-L)
temp[temp<0]=0
temp[temp>255]=255
temp[temp==255]=0 #added this line because for some reason there were a lot of white pixels where it should have been dark
final = temp.astype('uint8')
return final

我尝试在 gimp 文档中搜索 gamma slider 背后的公式,并找到了一个看起来像这样的代码。

  if (gray)
{
gdouble input;
gdouble range;
gdouble inten;
gdouble out_light;
gdouble lightness;

/* Calculate lightness value */
lightness = GIMP_RGB_LUMINANCE (gray->r, gray->g, gray->b);

input = gimp_levels_config_input_from_color (channel, gray);

range = config->high_input[channel] - config->low_input[channel];
if (range <= 0)
goto out;

input -= config->low_input[channel];
if (input < 0)
goto out;

/* Normalize input and lightness */
inten = input / range;
out_light = lightness / range;

/* See bug 622054: picking pure black or white as gamma doesn't
* work. But we cannot compare to 0.0 or 1.0 because cpus and
* compilers are shit. If you try to check out_light using
* printf() it will give exact 0.0 or 1.0 anyway, probably
* because the generated code is different and out_light doesn't
* live in a register. That must be why the cpu/compiler mafia
* invented epsilon and defined this shit to be the programmer's
* responsibility.
*/
if (out_light <= 0.0001 || out_light >= 0.9999)
goto out;

/* Map selected color to corresponding lightness */
config->gamma[channel] = log (inten) / log (out_light);
config->gamma[channel] = CLAMP (config->gamma[channel], 0.1, 10.0);
g_object_notify (G_OBJECT (config), "gamma");

我知道 gamma 输入值应该在 0.1 到 10 之间,而且它可能是对数函数或二次函数。我不确定这是否是正确的代码片段,因为它似乎采用高、低和像素值并产生 Gamma 值(我可能弄错了),而我想输入 Gamma 值并获得校正后的图像。

问题是虽然用这种方法生成的图像接近我想要的,但它与在 Photoshop 或 gimp 中移动 Gamma slider 不同。

我是使用代码进行图像处理的业余爱好者,这是我在 StackOverFlow 的第一个问题,所以请原谅我可能提出的任何愚蠢问题。

最佳答案

我认为这是正确的,但请在投入生产之前彻底尝试一下:

#!/usr/bin/env python3

import numpy as np
import math
from PIL import Image

# Load starting image and ensure greyscale. Make into Numpy array for doing maths.
im = Image.open('start.png').convert('L')
imnp = np.array(im)/255

# DEBUG: print(imnp.max())
# DEBUG: print(imnp.min())
# DEBUG: print(imnp.mean()

# Calculate new gamma
gamma=math.log(imnp.mean())/math.log(0.5)

# Apply new gamma to image
new = ((imnp**(1/gamma))*255).astype(np.uint8)

# Convert back to PIL from Numpy and save
Image.fromarray(new).save('result.png')

它变成了这个:

enter image description here

进入这个:

enter image description here

如果您的图像很大,可能值得制作一个包含 256 个可能的灰度值的 LUT(查找表)并应用它,而不是做幂等操作。


或者,如果您不想编写任何 Python,您可以只使用 ImageMagick,它安装在大多数 Linux 发行版上并且适用于 macOS 和 Windows。因此,只需在终端(或 Windows 上的命令提示符)中:

magick input.png -auto-gamma result.png

关于python - 如何通过灰度图像代码使用电平 Gamma /中间调 slider ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54459323/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com