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.
I would like to correct this using OpenCV so that the luminance is the same across the image. 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. 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.
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. 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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With