cropping (subsetting) raster image (matrix) in r -


i'm looking able split raster image composed matrix separate matrix/images can compare them.

for example, have raster image-

library(grid)       m = matrix(c( .7, .7, .7, .7, .7, .7, .7, .3, .7, .7, .3, .7, .7, .7, .7, .7, .7, .7, .7, .7, .7, .7, .7, .7, .7, .3, .7, .7, .3, .7, .7, .7, .7, .7, .7, .7, .7, .7, .7, .7, .7, .7, .7, .3, .7, .7, .3, .7, .7, .7, .7, .7, .7, .7), nrow = 6, ncol=9) grid.raster(m) 

i "crop" 6 individual cells, saving each it's own matrix. i've been thinking may possible based on dimensions.

d <- dim(m) 

and knowing cells each 1/3 x dimension 1/2 y dimension looping find , save each cell.

the data i'm trying work has 48 cells , has faint line around each cell, creating rectangles of equal size. if there way in r have find border , punch out individual cells ideal, using dimensions seemed workable solution.

thank you!

if knew sub-tiles regularly shaped , square there other ways/packages create these. think raster package best solution getting done you'd in general, understandable manner:

m <- matrix(c( .7, .7, .7, .7, .7, .7, .7, .3, .7, .7, .3, .7, .7, .7, .7, .7, .7, .7, .7, .7, .7, .7, .7, .7, .7, .3, .7, .7, .3, .7, .7, .7, .7, .7, .7, .7, .7, .7, .7, .7, .7, .7, .7, .3, .7, .7, .3, .7, .7, .7, .7, .7, .7, .7), nrow = 6, ncol=9)   library(raster)  ##  construct raster object matrix: r <- raster(m)  ##  build index locations upper left corners of grid: <- seq(1, ncol(r), ncol(r)/3) j <- seq(1, nrow(r), nrow(r)/2) indices <- expand.grid(i, j)  ##  crop these grid locations, storing individual cropped ##    areas in list object: r_out <- lapply(1:nrow(indices), function(x){    crop(r,      extent(r,       indices[x,2], indices[x,2]+nrow(r)/2 - 1,       indices[x,1], indices[x,1]+ncol(r)/3 - 1     )   ) })   ##  plot individual rasters: par(mfrow=c(2,3)) lapply(r_out, plot) 

enter image description here

##  re-mosaic raster objects: args <- r_out args['fun'] <- 'mean' r_out_mos <- do.call(mosaic, args) par(mfrow=c(1,1)) plot( r_out_mos ) 

reconstituted image


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -