We can try to correct the lighting effect by setting a linear plane according to the image intensities, which will bring the average image level closer. Subtracting this shading plane from the original image, we can try to normalize the lighting conditions in the image.
For RGB color images, simply repeat the process for each channel separately, or even apply it to a different color space (HSV, Lab *, etc.)
Here is an example implementation:
function img = correctLighting(img, method) if nargin<2, method='rgb'; end switch lower(method) case 'rgb' %# process R,G,B channels separately for i=1:size(img,3) img(:,:,i) = LinearShading( img(:,:,i) ); end case 'hsv' %# process intensity component of HSV, then convert back to RGB HSV = rgb2hsv(img); HSV(:,:,3) = LinearShading( HSV(:,:,3) ); img = hsv2rgb(HSV); case 'lab' %# process luminosity layer of L*a*b*, then convert back to RGB LAB = applycform(img, makecform('srgb2lab')); LAB(:,:,1) = LinearShading( LAB(:,:,1) ./ 100 ) * 100; img = applycform(LAB, makecform('lab2srgb')); end end function I = LinearShading(I) %# create X-/Y-coordinate values for each pixel [h,w] = size(I); [XY] = meshgrid(1:w,1:h); %# fit a linear plane over 3D points [XYZ], Z is the pixel intensities coeff = [X(:) Y(:) ones(w*h,1)] \ I(:); %# compute shading plane shading = coeff(1).*X + coeff(2).*Y + coeff(3); %# subtract shading from image I = I - shading; %# normalize to the entire [0,1] range I = ( I - min(I(:)) ) ./ range(I(:)); end
Now let's test it on this image:
img = im2double( imread('http://i.stack.imgur.com/JmHKJ.jpg') ); subplot(411), imshow(img) subplot(412), imshow( correctLighting(img,'rgb') ) subplot(413), imshow( correctLighting(img,'hsv') ) subplot(414), imshow( correctLighting(img,'lab') )
The difference is subtle, but it can improve the results of further image processing and OCR tasks.
EDIT: Here are some results that I obtained using other methods to increase the contrast IMADJUST , HISTEQ , ADAPTHISTEQ on different color spaces in the same way as above:
Remember that you must fine tune any parameter that matches your image ...