[SciPy-User] Focal Majority
Wed Feb 22 10:55:55 CST 2012
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 : z
> array([[1, 1, 0, 8],
> [8, 1, 3, 1],
> [3, 1, 1, 2],
> [3, 1, 4, 5]])
> In : zmask = np.zeros(z.shape, bool)
> In : for n in range(4):
> zmask = np.logical_or(zmask, rank_filter(z,n,3)==rank_filter(z,(n-5),3))
> In : zmask
> array([[ True, True, False, False],
> [ True, True, True, False],
> [False, False, True, False],
> [ True, False, False, False]], dtype=bool)
> SciPy-User mailing list
More information about the SciPy-User