On Wednesday, September 4, 2013 10:44:21 PM UTC-7, Juan Nunez-Iglesias wrote:
It makes a lot of sense that this would fail, but skimage.filter.rank.meanhttp://scikit-image.org/docs/dev/api/skimage.filter.rank.html#meanhas a `mask` keyword argument that can do what you want. However, it only works for uint8 or uint16 images, *and* if I remember correctly, it will be much slower if values exceed 12 bits, that is, your image intensity values should be in [0, 4096).
Thanks for your reply! Could you explain why it makes sense that the uniform_filter fails in my example? Using a window of 3, I expect the following in this simple 1d example:
input: [nan, 1, 2, 3, 4, 5, nan]
output: [nan, nan, 2, 3, 4, nan, nan]
Why should this not work?
from skimage.filter import rank
arr = array([[np.nan, 1, 2, 3, 4, 5, np.nan]]) # make sure this is 2D selem = np.ones((3, 3)) arr_mean1 = rank.mean(arr.astype(np.uint8), selem) arr_mean1
array([[0, 1, 2, 3, 4, 3, 2]], dtype=uint8)
arr.astype(np.uint8) # nan's are actually converted to 0... Not good!
array([[0, 1, 2, 3, 4, 5, 0]], dtype=uint8)
nan_mask = True - np.isnan(arr) arr_mean2 = rank.mean(arr.astype(np.uint8), selem, mask=nan_mask) arr_mean2 # using the nan mask produces the correct output
array([[1, 1, 2, 3, 4, 4, 5]], dtype=uint8)
Hope this helps!
I am trying to use ndimage's uniform_filter (for a simple local mean filtering) on a map-projected image that has NAN's at the border (basically the corners where the rotated map projected image does not fit into the rectangular grid). Is there a way, maybe in skimage, to use a uniform_filter on an array that contains NANs? ndimage' version does not cope with it correctly:
arr = array([np.nan, 1,2,3,4,5,np.nan]) arr
array([ nan, 1., 2., 3., 4., 5., nan])
array([ nan, nan, nan, nan, nan, nan, nan])