Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to understand masking

I've been trying to understand masking and how it works with image filters. I'm 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 I expect, but img_masked_filtered == img_unmasked_filtered which I don't want. scipy.ndimage.median_filter does exactly the job I need, but it doesn't work with masked images. What can I use that will do the same thing as the median filter, but which will work on a masked image?

The weird size I'm using for the array is because that's the size of the image I eventually want to filter.

like image 627
Jim421616 Avatar asked Aug 26 '17 00:08

Jim421616


People also ask

What does masking feelings mean?

In recent developmental studies, masking has evolved and is now defined as “concealing one's emotion by portraying another emotion”. It is mostly used to conceal a negative emotion (usually sadness, frustration, and anger) with a positive emotion.

Why do we need masking?

Layered prevention strategies — like staying up to date on vaccines and wearing masks — can help prevent severe illness and reduce the potential for strain on the healthcare system. Wear a mask with the best fit, protection, and comfort for you.

What is the meaning of mask anxiety?

Some people become anxious when wearing a face mask or just thinking about wearing a face mask. This would be what some are calling face mask anxiety. But it is important to remember that the mask is NOT causing the anxiety. The person's perceptions about the mask are what lead the brain to become anxious.


1 Answers

The ndimage filters do not respect masked arrays' masks. Instead, "mask" an ordinary NumPy array with nan values, and then use ndimage.generic_filter to call 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

like image 58
unutbu Avatar answered Sep 28 '22 10:09

unutbu