将颜色元素添加到单帧灰度图像

时间:2017-08-13 13:36:19

标签: r image rgb

我有一个单帧灰度图像。我想修改图像,以便某个值的所有像素变成彩色,比如红色。显然我需要将图像转换为3帧图像。 我使用包EBImage是为了方便,但只使用基本包的简单代码会很棒。

mat = round(matrix(runif(100), ncol = 5), digits = 1)
mat
      [,1] [,2] [,3] [,4] [,5]
 [1,]  0.1  0.2  0.6  0.1  0.8
 [2,]  0.1  0.3  0.4  0.5  0.6
 [3,]  0.6  0.3  0.8  0.8  0.5
 [4,]  0.5  0.9  0.7  0.3  0.2
 [5,]  0.7  0.7  0.9  0.3  0.2
 [6,]  0.6  1.0  0.4  0.7  0.3
 [7,]  0.7  0.6  0.5  0.8  0.5
 [8,]  0.4  0.8  0.2  0.2  0.1
 [9,]  0.2  0.1  0.4  0.9  0.6
[10,]  0.7  0.3  0.2  0.3  0.8
[11,]  0.8  1.0  0.4  0.8  0.3
[12,]  0.8  1.0  0.5  0.8  0.7
[13,]  0.6  0.5  0.9  0.9  0.1
[14,]  1.0  0.5  0.6  0.0  0.3
[15,]  0.1  0.5  0.6  0.6  0.2
[16,]  0.2  0.8  0.2  0.5  0.2
[17,]  0.9  0.9  0.4  0.7  0.1
[18,]  0.3  0.7  0.9  0.7  0.4
[19,]  1.0  0.0  0.5  0.0  1.0
[20,]  0.2  0.3  0.9  0.3  0.6

matgray = 255*mat
mat_rgb = channel(matgray, 'rgb') ## using EBImage package
dim(mat_rgb)
[1] 20  5  3
mat_red = channel(mat_rgb, 'asred')

# To generate and save gray image from gray matrix
par(mar = c(0,0,0,0))
png(filename = paste("Image.png"), res=1)
image(mat_red, axes = FALSE)
dev.off()

当我继续保存图像时,它显示Missing frame index for an image stack, assuming 'i = 1',图像确实没有显示任何红色...感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

你几乎是对的。 channel(mat, 'rgb')或其别名函数toRGB(mat)通过复制红色,绿色和蓝色通道上的像素强度,将单个灰度帧提升为RGB图像,从而生成3D阵列。默认情况下,EBImage对[0:1]范围内的图像数据进行操作,因此通常您不会将矩阵乘以255。您可以使用display来显示数据。

library(EBImage)
set.seed(0) # set random number generator state for reproducibility

mat <- round(matrix(runif(100), ncol = 5), digits = 1)
mat_rgb <- channel(mat, 'rgb') #or: toRGB(mat)
mat_rgb

## Image 
##   colorMode    : Color 
##   storage.mode : double 
##   dim          : 20 5 3 
##   frames.total : 3 
##   frames.render: 1 
## 
## imageData(object)[1:5,1:5,1]
##      [,1] [,2] [,3] [,4] [,5]
## [1,]  0.9  0.8  0.4  0.4  1.0
## [2,]  0.3  0.9  0.8  0.9  0.4
## [3,]  0.4  0.2  0.6  0.3  0.7
## [4,]  0.6  0.7  0.8  0.5  0.4
## [5,]  0.9  0.1  0.6  0.3  0.3

display(mat_rgb)

enter image description here

但即使图像的colorModeColor,结果仍会以灰色阴影显示,因为每个像素的颜色强度在3个通道中是相同的。 channel(mat_rgb, 'asred')仅通过复制其值来提取从灰度原始生成的红色成分。因此,您最终将拥有与开始时相同的框架。

mat_red <- channel(mat_rgb, 'asred')
mat_red

## Image 
##   colorMode    : Grayscale 
##   storage.mode : double 
##   dim          : 20 5 
##   frames.total : 1 
##   frames.render: 1 
## 
## imageData(object)[1:5,1:5]
##      [,1] [,2] [,3] [,4] [,5]
## [1,]  0.9  0.8  0.4  0.4  1.0
## [2,]  0.3  0.9  0.8  0.9  0.4
## [3,]  0.4  0.2  0.6  0.3  0.7
## [4,]  0.6  0.7  0.8  0.5  0.4
## [5,]  0.9  0.1  0.6  0.3  0.3

identical(imageData(mat_red), mat) #or: identical(as.array(mat_red), mat)

## [1] TRUE

您可以使用灰度矩阵作为red中的rgbImage频道,为所有像素着色。

mat_red <- rgbImage(red = mat)

display(mat_red)

enter image description here

为了使用固定颜色仅突出显示符合特定条件的像素,同时将图像的其余部分保留为灰度,您需要将原始强度矩阵作为红色,绿色和蓝色通道提供给rgbImage,但是感兴趣的像素被改变,使得它们编码所需的颜色。例如,要绘制低于0.2红色阈值的所有像素,您可以将其设置为红色通道中的1(最大颜色值),并将其设置为0,而不是其他两个。

pts <- which(mat<0.2)

mat_r <- mat_g <- mat_b <- mat
mat_r[pts] <- 1
mat_g[pts] <- 0
mat_b[pts] <- 0

display( rgbImage(mat_r, mat_g, mat_b) )

enter image description here

可以从col2rgb轻松获取与其他颜色对应的值。

col2rgb("orange")/255

##           [,1]
## red   1.0000000
## green 0.6470588
## blue  0.0000000