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

问题描述:

我有一个单帧灰度图像。我想修改图像,以便某个值的所有像素变成彩色的,例如红色。很显然,我需要将图像转换为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',并且图像确实没有表现出任何红色...感谢您的帮助!

你明白了。 channel(mat, 'rgb')或其别名功能toRGB(mat)通过在红色,绿色和蓝色通道上复制像素强度来产生3D阵列,从而将单个灰度帧提升为RGB图像。默认情况下,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 

可以着色通过使用灰度矩阵作为rgbImagered所述红色通道的所有像素。

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 
+0

谢谢你,这看起来不错。但我不确定这是我想要做的。您建议您在保持明暗对比的同时将所有像素以红色着色。我想要的是保持大多数像素的灰度,并将灰度值为0的所有像素都改为红色。结果将是灰度图像,只有少量红色像素在其中漂浮 – Gabriel123

+0

感谢您的澄清,我已经相应地更新了我的答案。 – aoles

+0

谢谢你oales,那很好用:-) – Gabriel123