I scanned slides and I have now 48 bit color plus IR (infrared) 16 bit gray files. Now I am processing them to remove dust and scratches and I am looking for a way to batch-detect the correct threshold to apply to IR images to define the regions with dust and scratches. I have over 4k slides and I cannot check all of them manually.
My question is about the choice of an algorithm to define the IR threshold. Other similar questions (Healing dust and scratch marks in scanned photos in post-processing) do not mention the infrared layer, therefore they need to "guess" the dust and the scratches. With the IR channel I have, I know where the issues are, only I have to isolate them better.
I read other questions concerning removal of dust and scratches from scanned film, for example https://stackoverflow.com/questions/27217916/dust-and-scratch-removal-with-open-source-graphic-libraries but those are about the choice of algorithms for the healing, given an already-defined IR B/W mask.
I also prepared some examples to illustrate my issue, I hope they won't clutter the question too much.
By checking the histograms of the IR images of each roll, I notice that I can define a threshold that correctly marks only dust and scratches, without wrongly marking parts of the picture as dust, but this threshold is low and not every scratch gets marked. See first image. I found this is about 135/255, 53% gray.
However, in some images the IR data is distributed around higher brightness values and I would like to increase the threshold to detect more dust. See example 2 where I used either the same threshold as in the first example (first mask) or I used a threshold of 194/255=76% gray (second mask). However, I don't know how to define this new threshold, image by image, automatically.
An option would be to check the slope of the IR histogram? I'm not sure how, there is a lot of variability.
Another option would be to analyse sub-regions of each image and to define a threshold on each one of them to adapt the baseline to brighter and darker regions. However, I am not sure how to define such an algorithm.
I am using G'Mic for the processing due to the very good inpaint filter and it provides a built-in language interpreter. As long as I know which algorithm I should implement, I should have no further problems. This question is specifically about the algorithm and not about its implementation.
The suggestion of other software able to adaptively mark dust and scratches is also welcome, I could pass the result to G'Mic for the final inpaint/healing.