How to find and remove white spots from an image using SciPy / NumPy?

I have a series of images that serve as my raw data, which I am trying to prepare for publication. These shots randomly have a series of white spots that I would like to replace with the average values ​​of the surrounding pixels.

I can not send images, but the following code should contain PNG, which approximates the problem I'm trying to fix:

import numpy as np from scipy.misc import imsave random_array = np.random.random_sample((512,512)) random_array[random_array < 0.999] *= 0.25 imsave('white_specs.png', random_array) 

While this should create an image with a similar distribution of spots that are present in my source data, my images do not have spots uniform in intensity, and some of them are larger than one pixel (although none of them are more than 2). In addition, there are spots on my image that I don’t want to change, which were intentionally saturated during data collection for the purpose of clarity in their presentation: these spots have a diameter of approximately 10 pixels.

Basically, I could write something to look for pixels whose value exceeds a certain threshold, and then check them for the average of their nearest neighbors. However, I assume that what I am ultimately trying to achieve is not uncommon in image processing, and I highly suspect that there are some SciPy features that will do this without having to reinvent the wheel. My problem is that I am not familiar with the formal aspects / vocabulary of image processing to really know what I should look for. Can someone point me in the right direction?

+6
source share
1 answer

You can just try the median filter with a small kernel size,

 from scipy.ndimage import median_filter filtered_array = median_filter(random_array, size=3) 

which will remove specks without noticeable change in the original image.

A media filter is well suited for such tasks, since it will better preserve functions in the original image with a high spatial frequency when compared, for example, with a simple moving average filter.

By the way, if your images are experimental (i.e. noisy), applying a non-aggressive median filter (for example, the one above) never hurts, since it also allows you to dampen the noise.

+9
source

Source: https://habr.com/ru/post/988246/


All Articles