gpt4 book ai didi

python - 如何设置 Gamma 校正的最佳值

转载 作者:行者123 更新时间:2023-12-02 16:07:00 25 4
gpt4 key购买 nike

我正在尝试对图像使用 Gamma 校正。但我只手动更改 Gamma 校正值。有什么办法可以自动计算 Gamma 校正的最佳值吗?例如。带亮度直方图。

代码:

# import the necessary packages
from __future__ import print_function
import numpy as np
import argparse
import cv2
def adjust_gamma(image, gamma=1.0):
# build a lookup table mapping the pixel values [0, 255] to
# their adjusted gamma values
invGamma = 1.0 / gamma
table = np.array([((i / 255.0) ** invGamma) * 255
for i in np.arange(0, 256)]).astype("uint8")
# apply gamma correction using the lookup table
return cv2.LUT(image, table)


# load the original image
original = cv2.imread('image.jpg')

# loop over various values of gamma
for gamma in np.arange(0.0, 3.5, 0.5):
# ignore when gamma is 1 (there will be no change to the image)
if gamma == 1:
continue
# apply gamma correction and show the images
gamma = gamma if gamma > 0 else 0.1
adjusted = adjust_gamma(original, gamma=gamma)
cv2.putText(adjusted, "g={}".format(gamma), (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 3)
cv2.imshow("Images", np.hstack([original, adjusted]))
cv2.waitKey(0)

最佳答案

以下是在 Python/OpenCV 中执行此操作的两种方法。两者都基于对数(中灰色)/对数(平均值)的比率。结果通常是合理的,尤其是对于暗图像,但并非在所有情况下都有效。对于明亮的图像,反转灰度或值(value)图像,处理暗图像,然后再次反转并重新组合,如果使用值(value)图像。

  • 阅读输入
  • 转换为灰度或 HSV 值
  • 计算灰度或值 channel 上的比率 log(mid-gray)/log(mean)
  • 将输入或值提高到比率
  • 的幂
  • 如果使用值 channel ,请将新的值 channel 与色调和饱和度 channel 组合并转换回 RGB

  • 输入:

    enter image description here
    import cv2
    import numpy as np
    import math

    # read image
    img = cv2.imread('lioncuddle1.jpg')

    # METHOD 1: RGB

    # convert img to gray
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # compute gamma = log(mid*255)/log(mean)
    mid = 0.5
    mean = np.mean(gray)
    gamma = math.log(mid*255)/math.log(mean)
    print(gamma)

    # do gamma correction
    img_gamma1 = np.power(img, gamma).clip(0,255).astype(np.uint8)



    # METHOD 2: HSV (or other color spaces)

    # convert img to HSV
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    hue, sat, val = cv2.split(hsv)

    # compute gamma = log(mid*255)/log(mean)
    mid = 0.5
    mean = np.mean(val)
    gamma = math.log(mid*255)/math.log(mean)
    print(gamma)

    # do gamma correction on value channel
    val_gamma = np.power(val, gamma).clip(0,255).astype(np.uint8)

    # combine new value channel with original hue and sat channels
    hsv_gamma = cv2.merge([hue, sat, val_gamma])
    img_gamma2 = cv2.cvtColor(hsv_gamma, cv2.COLOR_HSV2BGR)

    # show results
    cv2.imshow('input', img)
    cv2.imshow('result1', img_gamma1)
    cv2.imshow('result2', img_gamma2)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

    # save results
    cv2.imwrite('lioncuddle1_gamma1.jpg', img_gamma1)
    cv2.imwrite('lioncuddle1_gamma2.jpg', img_gamma2)

    方法1的结果:

    enter image description here

    方法 2 的结果:

    enter image description here

    关于python - 如何设置 Gamma 校正的最佳值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61695773/

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