% Load and display the image rice.png
clear, close all
I = imread('rice.png');
imshow(I)
% Use the blkproc function to find a coarse estimate of the 
% background illumination by determining the minimum pixel value 
% of each 32-by-32 block of the image. 
% Display the result as a surface plot. 
bg32 = blkproc(I,[32 32],'min(x(:))');
figure, surf(double(bg32)), set(gca,'ydir','reverse')
rotate3d on 
% Our estimate of the background illumination is only 8-by-8. 
% Expand it to be the same size as the original image using the imresize function. 
bg256 = imresize(bg32, [256 256], 'bilinear');
figure, imshow(bg256) 
% Now subtract the background image from the original image. 
I2 = im2uint8(im2double(I) - im2double(bg256));
imshow(I2) 
% The corrected image is too dark to see easily. 
% Use imadjust to brighten the image: 
max_normalized = double(max(I2(:)))/255;
I3 = imadjust(I2, [0 max_normalized]);
imshow(I3);