Attempt to understand disguise

I tried to understand masking and how it works with image filters. I am using the following code to try to develop my understanding.

import scipy.ndimage as ndi
import matplotlib.pyplot as plt
import numpy as np

#   Generate a random binary mask
np.random.seed(seed=182)
mask = np.random.randint(2, size=(901, 877))

img = np.random.rand(901, 877)

img_masked = np.ma.masked_array(img, mask = mask)
img_masked_filtered = ndi.median_filter(img_masked, size=10)
img_unmasked_filtered = ndi.median_filter(img, size=10)

median_masked = np.ma.median(img_masked)
median_unmasked = np.ma.median(img)

In the results, median_unmasked! = Median_masked, as expected, but img_masked_filtered == img_unmasked_filtered, which I don't want. scipy.ndimage.median_filter does exactly the job that I need, but it does not work with masked images. What can I use, it will do the same as the median filter, but which will work on a masked image?

The weird size I use for the array is that the size of the image that I end up wanting to filter is

+4
1

ndimage . "" NumPy nan, ndimage.generic_filter np.nanmedian:

import scipy.ndimage as ndi
import matplotlib.pyplot as plt
import numpy as np

np.random.seed(seed=182)
# h, w = 901, 877
h, w = 10, 10
mask = np.random.randint(2, size=(h, w))
img = np.random.rand(h, w)
img_masked = np.where(mask, img, np.nan)

size = 3
img_masked_median = ndi.generic_filter(img_masked, np.nanmedian, size=size)
img_unmasked_median = ndi.median_filter(img, size=size)

fig, ax = plt.subplots(nrows=2, ncols=2)
ax[0,0].imshow(img)
ax[0,0].set_title('img')
ax[0,1].imshow(img_masked)
ax[0,1].set_title('img_masked')
ax[1,0].imshow(img_unmasked_median)
ax[1,0].set_title('img_unmasked_median')
ax[1,1].imshow(img_masked_median)
ax[1,1].set_title('img_masked_median')
plt.show()

enter image description here

+1

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


All Articles