5

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()
nickponline
  • 25,354
  • 32
  • 99
  • 167
  • Can't you just use imgur (the default image hosting service used by SO) and embed the images, so they're visible without having to click and go to some other site? This is kinda annoying. | Just scale intensities in each column appropriately. You know the mean intensity per column (might as well fit a parabola to it) and you know the target intensity you want it at (constant across all columns). Do a linear fit per column to get offset+scale. Apply those per column. – Dan Mašek Feb 17 '17 at 20:47

1 Answers1

1

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.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432