Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 向单帧灰度图像添加颜色元素_R_Image_Rgb - Fatal编程技术网

R 向单帧灰度图像添加颜色元素

R 向单帧灰度图像添加颜色元素,r,image,rgb,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帧图像。 为了方便起见,我使用了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()

当我继续保存图像时,它会显示
图像堆栈缺少帧索引,假设“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)
通过将灰度矩阵用作
rgbImage
中的
red
通道,可以将所有像素涂成红色

mat_red <- rgbImage(red = mat)

display(mat_red)

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

col2rgb("orange")/255

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

谢谢你,奥莱斯,看起来很棒。但我不确定这正是我想要做的。你的建议是将所有像素都涂成红色,同时保持明暗对比。我想要的是将大多数像素保持在灰度范围内,并将灰度值为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) )
col2rgb("orange")/255

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