【问题标题】:Python OpenCV cv2 - Easy way to increase the brightness and contrast of an image by 100%Python OpenCV cv2 - 将图像的亮度和对比度提高 100% 的简单方法
【发布时间】:2019-09-28 08:35:24
【问题描述】:

我想将现有图像的亮度和对比度都更改 100%。

decoded = Image.open(BytesIO(base64.b64decode(base64Data)))
image = cv2.cvtColor(np.array(decoded), cv2.COLOR_BGR2RGB)

有没有允许我这样做的功能?

【问题讨论】:

标签: python opencv cv2


【解决方案1】:

我找不到 OpenCV 函数,但我找到了该指南: https://docs.opencv.org/3.4/d3/dc1/tutorial_basic_linear_transform.html 它说您可以通过这种方式调整对比度和亮度:

new_image = old_image * contrast_coeff + brightness_coeff

但是,我没有使用它,因为您可以注意到,如果您只想更改对比度,它不会同时使暗像素变暗和亮像素变亮

之前我使用了 tensorflow 的对比度调整,所以我使用了它的公式:

float_image = image / 255.0       # image must be float!!
mean = np.mean(image, axis=-1)    # compute mean values over each channel
new_image = (image - mean) * contrast_coeff + mean     # change contrast

您可能还想将结果转换为 int,因此最好将值削减为 0 以下和 1 以上。您可以对其进行规范化,或者您可以只剪辑:

new_image = np.clip(new_image, a_min=0., a_max=1.)     # cut values under 0 and over 1 

如您所见,这一切都没有使用 OpenCV,而是使用了 NumPy,所以我希望它适合您的需求。

您可能还想了解不同的对比度公式:https://en.wikipedia.org/wiki/Contrast_(vision)

【讨论】:

    【解决方案2】:

    这是使用 skimage rescale_intensity 的一种简单方法。您提供要成为 min=0 和 max=255 输出值的最小和最大输入值,它会对所有图像值进行线性调整。这里有两个例子:

    输入:

    import cv2
    import numpy as np
    import skimage.exposure
    
    # load image with alpha channel
    img = cv2.imread('lena.png')
    
    # adjust just the input max value   
    out1 = skimage.exposure.rescale_intensity(img, in_range=(0,128), out_range=(0,255))
    cv2.imwrite('lena_stretch_0_128.png', out1)
    
     # adjust both the input min and max values   
    out2 = skimage.exposure.rescale_intensity(img, in_range=(64,192), out_range=(0,255))
    cv2.imwrite('lena_stretch_64_192.png', out2)
    
    cv2.imshow('Out1', out1)
    cv2.imshow('Out2', out2)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    


    Out1(仅调整输入最小值):

    Out2(调整输入最小值和最大值):

    【讨论】:

      【解决方案3】:

      这是一种非常数学且直接的方法,可以将亮度和对比度作为参数进行调整。对比度控制输出与输入值图中直线方程的斜率。截距取决于亮度和对比度。亮度控制直线斜率的枢轴点,使得所需结果越亮,枢轴点越高。这是提供 bri 和 con 参数的代码,它们可以在 -100 到 100 的范围内更改,但受到限制,因此不能反转对比度。 bri=0 和 con=-100 的值将使图像去饱和,使其完全为中灰色。 bri=100 和 con=-100 的值将产生纯白色图像。同样 bri=-100 和 con=-100 将产生纯黑色图像。所以 bri 和 con 值就像百分比变化。因此 bri=0 和 con=0 与输入没有任何变化。

      输入:

      import cv2
      import numpy as np
      import math
      
      
      # load image with alpha channel
      img = cv2.imread('lena.png')
      
      # define desired brightness and contrast change values    
      bri = 20
      con = 20
      
      # compute slope and intercept   
      diffcon = (100 - con)
      if diffcon <= 0.1: con=99.9
      
      arg = math.pi * (((con * con) / 20000) + (3 * con / 200)) / 4
      slope = 1 + (math.sin(arg) / math.cos(arg))
      if slope < 0: slope=0
      
      pivot = (100 - bri) / 200
      intcpbri = bri / 100
      intcpcon = pivot * (1 - slope)
      intercept = (intcpbri + intcpcon)
      
      # print slope and intercept
      print(slope, intercept)
      
      # apply slope and intercept
      img = img/255.0
      out = slope * img + intercept
      out[out>1] = 1
      out[out<0] = 0
      
      # display IN and OUT images
      cv2.imshow('IN', img)
      cv2.imshow('OUT', out)
      cv2.waitKey(0)
      cv2.destroyAllWindows()
      
      # save output image
      out = 255.0 * out
      out = out.astype(int)
      cv2.imwrite('lena_bc_20_20.png', out)
      


      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-09-01
        • 2012-05-20
        • 1970-01-01
        • 2012-12-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多