[R-sig-Geo] Filter raster by threshold using focal and euclidean distance

anne.reichmuth at esa.int anne.reichmuth at esa.int
Thu Nov 20 11:25:04 CET 2014


Dear R users,

I have the issue of defining the right function for my task. For filtering 
an image I have set up a filter matrix eucdis with

refm=matrix(1,nrow=11,ncol=11)
M = dim(refm)[1]
N = dim(refm)[2]
eucdis = matrix(NaN, nrow=11, ncol=11)
for (i in -5:5){
      for (j in -5:5){
            eucdis[i+6,j+6] = 2*(sqrt(sum(abs(0-i)^2+abs(0-j)^2))) 
#euclidean distance of the moving matrix
            eucdis[6,6]=1
            eucdis[eucdis>10]=0
            eucdis[eucdis>0]=1
      }
}


Using the example raster 

f <- system.file("external/test.grd", package="raster")
f
r <- raster(f)

 
I want to filter all values of that raster that have a certain the value, 
say 200 within 10%  (=8) of the eucdis filter matrix

s=focal(x=r,w=eucdis,fun=function(w) {if (length(w[w==2])>=8) {s=1} else 
{s=0}})

But this only gives me all values where the eucdis filter matrix has at 
least 8 pixel with any values of r. If I add the constraint about 
r[r>=200] it is not working as I thought it would. It is not taking the 
second constraint into account.

s=focal(x=r,w=eucdis,fun=function(w,x) {if (length(w[w==1])>=8 | 
x[x>=200]){s=1} else {s=0}}) # I also tried & and &&
 
If anyone can help me please. I have spend days already and can't figure 
it out my self.

Thank you,
Anne
This message and any attachments are intended for the use of the addressee or addressees only.
The unauthorised disclosure, use, dissemination or copying (either in whole or in part) of its
content is not permitted.
If you received this message in error, please notify the sender and delete it from your system.
Emails can be altered and their integrity cannot be guaranteed by the sender.

Please consider the environment before printing this email.


	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list