[SciPy-User] Focal Majority

Bjorn Nyberg bjorn.burr.nyberg@gmail....
Wed Feb 22 10:55:55 CST 2012

Thanks Aronne,

I only had half an hour or so to play around with it but it certainty looks promising.  Im going to spend some more time on that over the weekend when im free... especially to understand how the ranking is being calculated.

I have to ask though, as I am using this for GIS purposes is there an easy way to convert the Bool format into a 0,1 integer - i.e. needed to convert the array to raster format (arcpy.ArrayToRaster....).


On Feb 21, 2012, at 19:58 PM, Aronne Merrelli wrote:

> I don't think a convolution would work. A convolution is really just a weighted sum, so I can't see a way to mimic a sort or conditional that way.
> But, I think you can do this with scipy.ndimage.rank_filter. If you want 5 cells with the same value, it should be equivalent to checking if the first and fifth ranked elements are the same (or second and sixth, etc...). So a loop through the window size, combining rank_filter calls, should do this. Definitely double check me on this - I'm not 100% sure it is doing the the correct thing, and it probably isn't doing what you want at the edges. If this is not fast enough, then I would consider writing a "brute force" loop in Cython to make it fast.
> In [90]: z
> Out[90]: 
> array([[1, 1, 0, 8],
>        [8, 1, 3, 1],
>        [3, 1, 1, 2],
>        [3, 1, 4, 5]])
> In [91]: zmask = np.zeros(z.shape, bool)
> In [92]: for n in range(4):
>     zmask = np.logical_or(zmask, rank_filter(z,n,3)==rank_filter(z,(n-5),3))
> In [93]: zmask
> Out[93]: 
> array([[ True,  True, False, False],
>        [ True,  True,  True, False],
>        [False, False,  True, False],
>        [ True, False, False, False]], dtype=bool)
> HTH,
> Aronne
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user

More information about the SciPy-User mailing list