[英]how to change image illumination in opencv python
我在python opencv中读取图像,现在我需要将此图像上的照明更改为更暗或更亮,我应该使用哪种方法来启用此功能?
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: 我将以外行的方式说清楚:
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 . 由于计算机屏幕将伽玛值应用于屏幕上的图像,因此应用反伽马来对抗该效果的过程称为伽马校正 。
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: 这是原始图像:
Applying gamma of value 0.5 will yield: 应用值为0.5的gamma将产生:
Applying gamma of value 1.5 will yield: 应用值为1.5的gamma将产生:
Applying gamma of value 2.5 will yield: 应用值为2.5的gamma将产生:
Applying gamma of value 1.0 will yield the same image. 应用值为1.0的伽玛将产生相同的图像。
I think you can done this with opencv. 我想你可以用opencv做到这一点。 Here is my suggestion
这是我的建议
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.
如果你使用减法会变暗。
A small remark to complement Jeru Luke's answer. 一个小小的评论补充Jeru Luke的答案。 Be sure that both arrays are of type
np.uint8
. 确保两个数组都是
np.uint8
类型。 The cv.LUT
function name stands for "look-up-table". cv.LUT
函数名称代表“查找表”。 It means that each pixel from the image
is replaced with a value from the table
. 这意味着
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. 或者在传入
adjust_gamma()
函数之前确保将图像数组转换为有效类型。 It is easy to convert the image into float
while applying various transformations and forget to restore valid type before adjusting gamma. 在应用各种变换时很容易将图像转换为
float
,并且在调整伽玛之前忘记恢复有效类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.