MATLAB: How to choose threshold values adaptively according to the illumination

Computer Vision ToolboxImage Processing Toolboxnormalize illuminationthreshold

I am proessing a series of graylevel images of the eye. The goal is to assess the uneven areas of each image and compare them to detect any trend/patterm. The detection is done by (1) applying gaussian filter to remove noises (2) find the backgroun by median filter with an area of [80, 80] (3) find the difference between the image and the background (4) thresholding the difference and find the connected areas using bwconncomp (5) the areas of all the connected areas are the number representing the unevenness.
Since each image was taken possibly with different levels of illumination. It's difficult to find a threshold value used in (4) for all the images and the values found in (5) seemed to be tied to/depend on the illumination level (the darker the bigger). I cannot use imadjust to normalize the illumination since it would smooth out the image and hence the unevenness.
Can anyone suggest a way to find out the threshold value to use for each image based on its illumination to achieve my goal? Thanks a lot in advance for the help!

Best Answer

I don't think this algorithm could work for every image in the world, or even the COCO dataset. Your only hope is that your images are somewhat similar. For example, not every image out there has what you would call an identifiable "background". What if I just take a snapshot of your office or yard outside? What is the background?
Related Question