[英]How do I adjust the luminance in a section of an image in OpenCV
I have the following image. 我有以下图片。
If I plot the average luminance as a function of x-pixel location I can see that the image is bright along the center than at the edges. 如果我将平均亮度绘制为x像素位置的函数,我可以看到图像沿中心而不是边缘是明亮的。
I would like to correct this using OpenCV so that the luminance is the same across the image. 我想使用OpenCV来纠正这个问题,以便整个图像的亮度相同。 Is this possible?
这可能吗?
EDIT: My code so far is 编辑:到目前为止我的代码是
import cv2
import pylab
img = cv2.imread('3.jpeg', 1)
cv2.imshow("img",img)
lab= cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
cv2.imshow("lab",lab)
l, a, b = cv2.split(lab)
values = []
for c in xrange(l.shape[1]):
count = 0
for r in xrange(l.shape[0]):
count += l[r][c]
values.append(1.0 * count / l.shape[0])
pylab.figure()
pylab.ylabel('Average Luminance')
pylab.xlabel('X axis')
pylab.plot(values, 'k-')
pylab.show()
I have a method, but I don't feel like writing any code for it today and, worse still, I don't speak Python. 我有一个方法,但我不想今天为它编写任何代码,更糟糕的是,我不会说Python。 But looking at how you determined there is an unevenness to the brightness, you can clearly code it yourself.
但是看看你如何确定亮度不均匀,你可以自己清楚地编码。
First, I would go to Lab mode (as you already did), and split the channels. 首先,我将进入实验室模式(正如您已经做过的那样),然后拆分频道。 Retain the
a
and b
channels for later reconstruction. 保留
a
和b
通道以便以后重建。
Now take the Lightness
( L
) channel and blur it with a large radius - that will remove all high frequency variations and retain only the low frequency variations you seek to eliminate. 现在采用
Lightness
( L
)通道并以大半径模糊它 - 这将消除所有高频变化并仅保留您想要消除的低频变化。 Let's say that new, blurred channel varies between say a minimum of 110 and a maximum of 125. Subtract 110 from all the values and you will now have values between 0 and 15 for each location in the image. 假设新的模糊通道在最小值110和最大值125之间变化。从所有值中减去110,现在图像中每个位置的值都在0到15之间。
Now subtract that value between 0..15 from the original, unblurred Lightness
channel to remove the low-frequency variations and then recombine that modified Lightness with the original a
and b
channels. 现在从原始的,不模糊的
Lightness
通道中减去0..15之间的值,以消除低频变化,然后将修改后的亮度与原始a
和b
通道重新组合。
I hope that's clear enough - if not, please just ask! 我希望这很清楚 - 如果没有,请问!
The advantage of this method over constructing a parabola to match the light fall-off, is that it will work whether the lightness varies with x, with y, or diagonally or in some other fashion. 这种方法优于构造抛物线以匹配光线衰减的优点在于,无论亮度随x,y,还是对角线或以其他方式变化,它都将起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.