首页 > 解决方案 > R :通过将 RGB 图像的红色和绿色值作为矩阵处理来将其设置为零

问题描述

我正在使用 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 新手,我找不到任何可能导致它的原因的线索。所以我想帮助我应该做什么,并提前非常感谢你!

标签: rmatrixgrayscale

解决方案


这实际上不是“SuperpixelImageSegmentation”或“OpenImageR”问题。'OpenImageR' 包的 'imageShow' 函数在底层使用了grid::grid.raster函数。您收到的错误是由于 R、G、B 的修改值不在 0 和 1 之间的范围内,正如另一个stackoverflow 问题中提到的那样。要克服这个问题,您必须首先将像素值标准化为 [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)

推荐阅读