python - 如何在opencv python中更改图像照明

标签 python opencv

我正在用 python opencv 读取图像,现在我需要将此图像上的照明更改为更暗或更亮,我应该使用什么样的方法来启用它?

最佳答案

我知道我来晚了,但我建议使用 Gamma 校正

什么是 Gamma 校正

我通俗的讲一下:

  • 要在屏幕上显示图像,需要输入电压。
  • 此电压作为光强度输出。
  • 在完美世界中,输入电压与输出强度呈线性关系。
  • 但实际屏幕输出接近于指数曲线, 指数为 Gamma

由于计算机屏幕将 Gamma 值应用于屏幕上的图像,因此应用反 Gamma 来抵消这种影响的过程称为 Gamma 校正

enter image description here

这是使用 OpenCV 3.0.0 和 python 的相同代码:

import cv2
import numpy as np

def adjust_gamma(image, gamma=1.0):

   invGamma = 1.0 / gamma
   table = np.array([((i / 255.0) ** invGamma) * 255
      for i in np.arange(0, 256)]).astype("uint8")

   return cv2.LUT(image, table)

x = 'C:/Users/524316/Desktop/stack/test.jpg'  #location of the image
original = cv2.imread(x, 1)
cv2.imshow('original',original)

gamma = 0.5                                   # change the value here to get different result
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("gammam image 1", adjusted)

cv2.waitKey(0)
cv2.destroyAllWindows()

这是原图:

enter image description here

应用值为 0.5 的 gamma 将产生:

enter image description here

应用值为 1.5 的 gamma 将产生:

enter image description here

应用值为 2.5 的 gamma 将产生:

enter image description here

应用值为 1.0 的 Gamma 将产生相同的图像。

Code was borrowed from this link

关于python - 如何在opencv python中更改图像照明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33322488/

相关文章:

Python Pandas 查找另一个数据框返回多个匹配项

python - Seaborn 图为 x 轴时间戳标签添加了额外的零

python - 带有 Django 的 Dropbox API - 身份验证

opencv - undistortPoints() 无法处理镜头畸变

python - Pygame 的图像捕获实际上是如何工作的?

python - Evaluator 组件上的 TFX IndexError

OpenCV 2.4.7 Mat::convertTo() 32 位到 16 位截断

c++ - 读取 svm 数据并用更多数据重新训练?

c++ - 如何在相机 - 投影仪系统中重新投影点(校准后)

java - 使用 OpenCV 使用 find Contours() 减轻光反射?