Run through all your images calling mean2
Then run through them all again dividing by the mean and getting your gradient with imgradient() or imgradientxy(), which ever you want.
See for FAQ for code https://matlab.fandom.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F.
for k = 1 : numFiles
% Get gray scale image..... using FAQ... then
means(k) = mean2(grayImage);
end
% Get the the mean of all the image means.
meanMean = mean(means)
% Now go through again
for k = 1 : numFiles
% Get gray scale image..... using FAQ... then
grayImage = meanMean * double(grayImage) / means(k); % Cast to uint 8 if you want in in uint8 instead of double.
% Now call imgradient()
end


