Let’s say you have an image in the form of an 2D numpy array as the one below. Now you want to erode the image. However, you don’t want to erode it equally. It should be eroded only if the object in the image has an intensity < 50, otherwise it should not be affected at all.
In the image below, only the blue-marked area should be eroded but but the lighter gray area should be unchanged.
Furthermore, any dark-gray area adjacent to light-gray area should not be affected (red-marked). This latter requirement makes it harder to erode the whole object and then put back the original light gray area because such an operation could affect also the dark gray are marked in red if the erosion eats all the way to the red parts. In such case, there would be a hole in the object.
I have looked into OpenCV's erode
function, but it does not have any options for this. I have also googled without finding any solution.