If the files are sequentially named, then you can read them using the following code.
The brightness of the image can be calculated by your definition of brightness. For example it can be calculated by the V color of the HSV color space
This goes below “%%Image Operation as per your work” in the code given above.
hsvImage = rgb2hsv(filnename);
brightness = hsvImage(:, 3);
Other way would be to find the mean of the image
brightness = mean2(filename);
Using the brightness values the adjustments can be made for each image.