[R-sig-Geo] extract matrix values using circular window

Rolf Turner r.turner at auckland.ac.nz
Fri Mar 16 09:16:36 CET 2012


On 16/03/12 19:48, Lorenzo Cattarino wrote:
> I wanna know how many cells with values of one (1) are within a circular neighbourhood with radius r and centre in x0 y0.

This might get you going:

require(spatstat)
# Build an example image of 0's and 1's:
M <- im(matrix(sample(0:1,500*500,TRUE),nrow=500),xcol=seq(0,10,length=500),
               yrow=seq(0,10,length=500))

# Take a disc which lies inside this image.
d <- disc(centre=c(3,5),radius=1)

# Plot the image and the disc.
plot(M)
plot(d,add=TRUE)

# Find the part of the image inside the disc:
Md <- M[d,drop=FALSE]
plot(Md)

# Find the number of 1's in Md:
summary(eval.im(Md==1))

# Or:
table(Md$v)

# Another approach:
i <- inside.owin(rasterx.im(M),rastery.im(M),d)
table(M[i])

HTH

     cheers,

         Rolf Turner



More information about the R-sig-Geo mailing list