0

I want to calculate GLCM with 488 raster files. Because of the enormous calculation time i want to use all the power of my multicore processor (AMD Phenom II 6-core).

library("glcm")
library(raster)
library(devtools)
install_github('azvoleff/glcm')

setwd(working dir.)
rasters <- list.files()[grep("()\\w*.tif", list.files())]
statistics <- c("mean", "variance", "homogeneity", "contrast", "dissimilarity", "entropy","second_moment", "correlation")
shift1 <- c(0,0,1,1)
shift2 <- c(0,1,0,1)

for (j in 1:length(rasters)){ 
  raster1 <- raster(rasters[j])
  for (i in 1:length(statistics)){
    for (k in 1:length(shift1)){
      GLCM <- glcm(raster1, window=c(11,11), statistics=statistics[i], shift = c(shift1[k],shift2[k]), na_opt="ignore")

      file <- paste("./GLCM/", substr(tiles[j],0,nchar(tiles[j])-4),"_", statistics[i], "_shift_",shift1[k], shift2[k] , ".tif", sep="")
      writeRaster(GLCM, filename = file, type = "GTIFF")    
    }

  }
  gc()
}

I searched the internet for multicore solutions in R, but could not find out which one is up to date. So I hope someone can help me.

4

1 回答 1

3

glcm未编码为并行运行,但鉴于您正在处理 488 个栅格,我不会担心并行运行算法本身 - 并行处理栅格(比如在普通笔记本电脑上一次两个,如果你拥有更多的处理能力和 RAM)是这里最简单的方法。glcm> 1.4 的版本将自动逐块运行大图像(并将考虑边缘效应),因此内存不应该成为问题。

像下面这样的东西应该让你开始(基于你的代码):

library(glcm)
library(raster)
library(foreach)
library(doparallel)

cl <- makeCluster()
registerDoParallel(cl)

setwd(working dir.)
rasters <- list.files()[grep("()\\w*.tif", list.files())]
statistics <- c("mean", "variance", "homogeneity", "contrast",
                "dissimilarity", "entropy","second_moment",
                "correlation")
shift1 <- c(0, 0, 1, 1)
shift2 <- c(0, 1, 0, 1)

foreach (j in 1:length(rasters), .packages=c('raster', 'glcm')) %dopar% {
  raster1 <- raster(rasters[j])
  for (i in 1:length(statistics)) {
    for (k in 1:length(shift1)) {
      GLCM <- glcm(raster1, window=c(11,11), statistics=statistics[i],
                   shift = c(shift1[k],shift2[k]), na_opt="ignore")
      file <- paste("./GLCM/", substr(tiles[j], 0, nchar(tiles[j])-4),
                    "_", statistics[i], "_shift_",shift1[k], shift2[k],
                    ".tif", sep="")
      writeRaster(GLCM, filename = file, type = "GTIFF")
    }
  }
}

stopCluster(cl)
于 2014-05-06T17:49:44.780 回答