Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I adjust the luminance in a section of an image in OpenCV

I have the following image. enter image description here

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.

enter image description here

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()
like image 650
nickponline Avatar asked Nov 09 '22 01:11

nickponline


1 Answers

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.

like image 178
Mark Setchell Avatar answered Nov 14 '22 21:11

Mark Setchell