使用多核并行功能的EBImage错误

时间:2019-02-06 10:30:48

标签: image-processing parallel-processing

当我尝试使用多核功能(mclapply,mcmapply ...等)时,使用EBImage软件包执行图像分析时遇到一些问题。这是一个分析图像堆栈的示例,每个图像框有两个帧

# Initial parameters
LUT_color <- c('purple', 'green')
median_filter <- c(2, 3)
threshold_size <- c(5, 10)
threshold_offset <- c(.1, .2)
segmentation_tolerance <- c(1, 2)
segmentation_tolerance_merge <- 2

# FUNCTION
image_analysis <- function(multicore){

  require('EBImage')
  require('parallel')

  # Number of Cores
  if (multicore == TRUE){

    if (detectCores() > 1){
      n_cores = detectCores()
    } else {n_cores = 1}

  } else{n_cores = 1}

  # Set stack images list
  img_ls <- list.files(path = file.path(path_to_images), full.names = TRUE)

  # Compute for each Stack
  lapply(seq(length(img_ls)), function(x){

    # Load stack image from list
    img <- readImage(img_ls[x])

    # Number of frames
    no_frames <- numberOfFrames(img, type = 'render')

    # Grayscale stack  
    stack_gray <- mclapply(seq(no_frames), 
                           mc.cores = n_cores, 
                           function(x){
                             img[,,x]
                           })

    # Color stack
    stack_color <- mcmapply(stack_gray, LUT_color,  
                            mc.cores = n_cores, 
                            SIMPLIFY = FALSE, 
                            FUN = function(x, LUT_color){
                              colormap(x, colorRampPalette(c('black', LUT_color))(256))
                            })

    # Median Filter
    stack_filter <- mcmapply(stack_gray, median_filter,  
                             mc.cores = n_cores, 
                             SIMPLIFY = FALSE, 
                             FUN = function(x, median_filter){
                               medianFilter(x, size = median_filter)
                             })

    # Threshold stack
    stack_mask <- mcmapply(stack_filter, channel_name, threshold_size, threshold_offset, open_value,  mc.cores = n_cores, SIMPLIFY = FALSE, 
                           FUN = function(x, channel_name, threshold_size, threshold_offset, open_value){
                             thresh(x, w = threshold_size, h = threshold_size, offset = threshold_offset)
                           })

    # Segmented stack
    stack_seg <- mcmapply(stack_mask, segmentation_tolerance, 
                          mc.cores = n_cores, 
                          SIMPLIFY = FALSE, 
                          FUN = function(x, segmentation_tolerance){
                            watershed(x = distmap(x), tolerance = segmentation_tolerance)
                          })

    # Sum Stacks
    merge_gray <- Reduce('+', stack_gray)
    merge_color <- Reduce('+', stack_color)
    merge_mask <- Reduce('+', stack_mask)
    merge_seg <- watershed(x = distmap(merge_mask), tolerance = segmentation_tolerance_merge)

    # Feature DataFrame from Sum Stack
    fts_merge_moment <- as.data.frame(computeFeatures.moment(merge_seg))
    fts_merge_shape <- as.data.frame(computeFeatures.shape(merge_seg))

    # Feature DataFrame from each GrayScale Frame
    fts_gray <- mapply(stack_gray, stack_mask, SIMPLIFY = FALSE, FUN = function(gray_img, stack_mask){
      new_gray <- gray_img
      new_gray[stack_mask == FALSE] = 0
      fts <- as.data.frame(computeFeatures.basic(merge_seg, new_gray))
      return(fts)
    })

  })

}

'multicore = TRUE'时出现问题。

我的目的是使用分段图像的总和来计算每帧的“基本”功能。因此,我将每个分析步骤累积到一个列表中,该列表包含一个堆栈中的所有帧。使用这种类型的功能,我可以选择是将每个图像保存到磁盘还是仅提取特征数据框。

另一方面,我可以使用 RBioformats 包中的read.Image函数以其他方式编写此函数,以仅加载每个帧并将每个帧保存到磁盘上,并在需要时加载它对帧进行求和以计算特征数据帧,但是当我不需要将图像保存到磁盘以显示图像分析过程的完成方式时,我无法选择最快的方法。

我需要一种更有效的方法,具有多核功能而不会出现内存不足问题,因为每帧约为5000x5000像素。

谢谢

0 个答案:

没有答案