如何在opencv python中改变图像照明

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33322488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:12:07  来源:igfitidea点击:

how to change image illumination in opencv python

pythonopencv

提问by user824624

I am reading a image in python opencv, now I need to change the illumination on this image to be darker or lighter, what kind of method I should use to enable this?

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

采纳答案by Rahul K P

I think you can done this with opencv. Here is my suggestion

我认为你可以用 opencv 做到这一点。这是我的建议

import cv2
import numpy as np

img1 = cv2.imread('abc.jpg')
a = np.double(img1)
b = a + 15
img2 = np.uint8(b)
cv2.imshow("frame",img1)
cv2.imshow("frame2",img2)
cv2.waitKey(0)
cv2.destroyAllWindows()

Here i increased the brightness of image. If you use subtraction that will makes darker.

在这里,我增加了图像的亮度。如果您使用减法,则会变暗。

回答by Jeru Luke

I know I am late, but I would suggest using gamma correction.

我知道我迟到了,但我建议使用伽玛校正

Now what is gamma correction?

现在什么是伽马校正

I will make it clear in layman's terms:

我会用外行的话说清楚:

  • To display image on a screen, input voltage is needed.
  • This voltage is output as light intensity.
  • In perfect world, input voltage would be linear to output intensity.
  • But the real screen output is close to an exponential curve, the exponent being gamma.
  • 要在屏幕上显示图像,需要输入电压。
  • 该电压作为光强度输出。
  • 在完美世界中,输入电压与输出强度成线性关系。
  • 但实际屏幕输出接近指数曲线,指数为gamma

Since the computer screen applies a gamma value to the image on screen, the process of applying inverse gamma to counter this effect is called gamma correction.

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

enter image description here

在此处输入图片说明

Here is the code for the same using OpenCV 3.0.0 and python:

这是使用 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()

Here is the original image:

这是原始图像:

enter image description here

在此处输入图片说明

Applying gamma of value 0.5 will yield:

应用值 0.5 的 gamma 将产生:

enter image description here

在此处输入图片说明

Applying gamma of value 1.5 will yield:

应用值 1.5 的 gamma 将产生:

enter image description here

在此处输入图片说明

Applying gamma of value 2.5 will yield:

应用值 2.5 的 gamma 将产生:

enter image description here

在此处输入图片说明

Applying gamma of value 1.0 will yield the same image.

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

Code was borrowed from this link

代码是从这个链接借来的

回答by devforfu

A small remark to complement Jeru Luke's answer. Be sure that both arrays are of type np.uint8. The cv.LUTfunction name stands for "look-up-table". It means that each pixel from the imageis replaced with a value from the table.

补充Jeru Luke的回答的一句话。确保两个数组都是 类型np.uint8。该cv.LUT函数名代表“查找表”。这意味着 中的每个像素image都替换为 中的一个值table

You could convert both arrays:

您可以转换两个数组:

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)])
   return cv2.LUT(image.astype(np.uint8), table.astype(np.uint8))

Or make sure that an image array is casted to the valid type before passing into adjust_gamma()function. It is easy to convert the image into floatwhile applying various transformations and forget to restore valid type before adjusting gamma.

或者确保在传递到adjust_gamma()函数之前将图像数组转换为有效类型。float在应用各种变换时很容易将图像转换成,而在调整伽马之前忘记恢复有效类型。