The function focal() does these types of operations. 
Raster algebra and boolean operations do the rest.

library(raster)

va <- c(0,0,0,0,0,0,0,0,0,0,0,1,0,0,
0,1,0,0,0,0,
        0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,1,1,1,1,1,1,1,1,1,0,1,0,1,1,1,1,1,0,
        0,0,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,
        0,1,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0)
r <- raster(ncols=10, nrows=10)
r <- setValues(r, va)
plot(r, col=c("blue","red"), main="before filter")
r2 <- focal(r, fun=sum, ngb=c(3,3))
r3 <- -1 * (r2<2 & r == 1) #isolated 1 cells
r4 <- r2>7 & r == 0 #isolated 0 cells

rf <- r + r3 + r4

x11()
plot(rf, col=c("blue","red"), main="after filter")

Cheers, Jacob.

--- On Tue, 4/13/10, gianni lavaredo <gianni.lavar...@gmail.com> wrote:

From: gianni lavaredo <gianni.lavar...@gmail.com>
Subject: [R-sig-Geo] Suggestion to build a Special Filter for a Raster 
classification
To: r-sig-geo@stat.math.ethz.ch
Date: Tuesday, April 13, 2010, 4:57 PM

Dear Researchers,

Sorry if I use your time with this email, but I wish to ask a suggestion to
build-up a special filter in a forest/no forest classification. In the end
of my methodology I have a raster image with Forest (pixel with 0 value) and
No forest area (pixel with 1 value). But when plot the raster I can see some
noise of my methodology (single isolate pixels with "0" value and single
isolate pixels with "1". PS: the resolution of my map is 1x1 m and It's not
possible a forest gap with area <= of 1 m square and not possible a forest
with the same area in a Gap). I wish to create a filter to eliminate these
pixel:

example
"1" value pixel around 8 "0" value pixels = Became "0"
"0" value pixel around 8 "1" value pixels = Became "1"

you could see with this example:

library(raster)

va <- c(0,0,0,0,0,0,0,0,0,0,0,1,0,0,
0,1,0,0,0,0,
        0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,1,1,1,1,1,1,1,1,1,0,1,0,1,1,1,1,1,0,
        0,0,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,
        0,1,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0)

r <- raster(ncols=10, nrows=10)
r <- setValues(r, va)
plot(r,  maxpixels = 1000, col=c("blue","red"), main="before filter")

va2 <- c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,
        0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,0,
        0,0,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,
        0,1,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0)

rfinal <- raster(ncols=10, nrows=10)
rfinal  <- setValues(r, va2)
plot(rfinal,  maxpixels = 1000, col=c("blue","red"), main="after filter")


Thanks for attention
Gianni

    [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
R-sig-Geo@stat.math.ethz.ch
https://stat.ethz.ch/mailman/listinfo/r-sig-geo



      
        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
R-sig-Geo@stat.math.ethz.ch
https://stat.ethz.ch/mailman/listinfo/r-sig-geo

Reply via email to