简体   繁体   English

如何在OpenCV中调整图像部分的亮度

[英]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. 保留ab通道以便以后重建。

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. 现在采用LightnessL )通道并以大半径模糊它 - 这将消除所有高频变化并仅保留您想要消除的低频变化。 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之间的值,以消除低频变化,然后将修改后的亮度与原始ab通道重新组合。

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.

相关问题 如何使用 python-opencv 调整图像的颜色? - How could I adjust color of an image with python-opencv? 如何使用 opencv python 调整亮度、对比度和振动度? - How do I adjust brightness, contrast and vibrance with opencv python? 如何获得图像的亮度梯度 - How to get luminance gradient of an image 如何使用jython调整整个图像 - How do I adjust the Entire image using jython 如何从PIL图像创建OpenCV图像? - How do I create an OpenCV image from a PIL image? 如何使用 opencv python 自动调整扫描图像的对比度和亮度 - How to auto adjust contrast and brightness of a scanned Image with opencv python 如何使用matplotlib将灰度图像转换为亮度图像? - How to convert gray image to luminance image using matplotlib? 如何更改图像上像素部分的Alpha通道? - How do I change the alpha channel of a section of pixels on my image? 如何将最相似的Unicode字符返回到图像的某个部分? - How do I return the most similar Unicode character to a section of an image? 将具有透明背景的图像与普通图像(非透明背景)混合后如何调整 alpha 值? - How do I adjust the alpha value after blending an image with a transparent background to a normal image (NOT transparent background)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM