【问题标题】:R : Setting Red and Green Values of an RGB image to Zero by handling it as a matrixR:通过将 RGB 图像的红色和绿色值作为矩阵处理来将其设置为零
【发布时间】:2020-02-27 16:46:41
【问题描述】:

我正在使用 R 中的 OpenImageR 和 SuperpixelImageSegmentation 包。 我想提取分割图像的绿色维度,结果是二维图像而不是 3(灰度)。
所以为了给它上色,我将彩色图像的红色和蓝色值设置为 0。
但是,每当我尝试显示图像时,都会出现以下错误,并且我无法理解为什么 0 被解释为 NAN,我也尝试将其设置为优于零(0.01)的值,但我仍然得到同样的信息:

Error in rgb(t(x[, , 1L]), t(x[, , 2L]), t(x[, , 3L]), maxColorValue = max) : 
  color intensity nan, not in [0,1]

这是我的代码:

library(SuperpixelImageSegmentation)
library(OpenImageR)

path = system.file("tmp_images", "Phen.jpg", package = "OpenImageR")

image = readImage(path)
init = Image_Segmentation$new()

segmentation = init$spixel_segmentation(input_image = image,
                                    superpixel = 2000, # k
                                    AP_data = TRUE,
                                    use_median = TRUE,
                                    sim_wA = 5,
                                    sim_wB = 5,
                                    sim_color_radius = 3,
                                    kmeans_method = "kmeans",
                                    kmeans_initializer = "kmeans++",
                                    kmeans_num_init = 5,
                                    kmeans_max_iters = 50,
                                    verbose = TRUE)
#getting the green part alone
imG = segmentation$AP_image_data
imG = imG[,,2]

imB = segmentation$AP_image_data
imB = imB[,,3]

imR = segmentation$AP_image_data
imR = imR[,,1]


imR4 = imR / 2
imB4 = imB / 2
imGDone = imG - imR4
imGDone = imGDone - imB4
imageShow(imGDone)  # works fine, the result is a mask that contains only the green concentrated areas

imGColor <- segmentation$AP_image_data
for (i in 1:nrow(imGDone)) {
  for (j in 1:ncol(imGDone)){
    if (imGColor[i,j,1] > 0) {
      imGColor[i,j,1] = 0 * imGDone[i,j] # setting red to zero
      imGColor[i,j,3] = 0 * imGDone[i,j] # setting blue to zero
      imGColor[i,j,2] = 1 * imGDone[i,j] # setting green
    }
  }
}

imageShow(imGColor)

当我执行最后一行以显示 imGColor 时,我得到了错误。 我是 R 新手,我找不到任何可能导致它的原因的线索。 所以我想知道我应该做什么,提前非常感谢你!

【问题讨论】:

    标签: r matrix grayscale


    【解决方案1】:

    这实际上不是“SuperpixelImageSegmentation”或“OpenImageR”问题。 'OpenImageR' 包的'imageShow' 函数在后台使用grid::grid.raster 函数。您收到的错误是由于 R、G、B 的修改值不在 0 和 1 之间的范围内,正如另一个 stackoverflow issue 中提到的那样。要克服这个问题,您必须首先将像素值标准化为 [0,1],然后进行调整。您提到的文件的“路径”在“OpenImageR”包中不存在,因此我使用了该包的另一个可用图像,

        library(SuperpixelImageSegmentation)
        library(OpenImageR)
    
        path = system.file("tmp_images", "2.jpg", package = "OpenImageR")
    
        image = readImage(path)
        init = Image_Segmentation$new()
    
        segmentation = init$spixel_segmentation(input_image = image,
                                                superpixel = 2000, # k
                                                AP_data = TRUE,
                                                use_median = TRUE,
                                                sim_wA = 5,
                                                sim_wB = 5,
                                                sim_color_radius = 3,
                                                kmeans_method = "kmeans",
                                                kmeans_initializer = "kmeans++",
                                                kmeans_num_init = 5,
                                                kmeans_max_iters = 50,
                                                verbose = TRUE)
        #getting the green part alone
        imG = segmentation$AP_image_data
        imG = imG[,,2]
    
        imB = segmentation$AP_image_data
        imB = imB[,,3]
    
        imR = segmentation$AP_image_data
        imR = imR[,,1]
    
    
        imR4 = imR / 2
        imB4 = imB / 2
        imGDone = imG - imR4
        imGDone = imGDone - imB4
        imageShow(imGDone)  # works fine, the result is a mask that contains only the green concentrated areas
    
        # the 'imGDone' array has values in the range [-0.08039, -0.03824] 
        summary(as.vector(imGDone))
    
        # you have to normalize first to [0,1] to avoid the error
        imGDone <- OpenImageR::NormalizeObject(imGDone)
    
        # values now in the range [0.0000, 1.0000]
        summary(as.vector(imGDone))
    
        imGColor <- segmentation$AP_image_data
    
        for (i in 1:nrow(imGDone)) {
          for (j in 1:ncol(imGDone)){
            if (imGColor[i,j,1] > 0) {
              imGColor[i,j,1] = 0 * imGDone[i,j] # setting red to zero
              imGColor[i,j,3] = 0 * imGDone[i,j] # setting blue to zero
              imGColor[i,j,2] = 1 * imGDone[i,j] # setting green
            }
          }
        }
    
        imageShow(imGColor)
    

    【讨论】:

      猜你喜欢
      • 2014-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-06
      • 2019-09-19
      • 1970-01-01
      • 1970-01-01
      • 2017-08-19
      相关资源
      最近更新 更多