I have a single-frame greyscale image. I would like to modify the image so that all pixels of a certain value becomes colored, say red. Obviously I need to convert the image into a 3-frame image. I use the package EBImage for convenience, but a simple code using only the base package would be great.
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()
When I go on to save the image, it says Missing frame index for an image stack, assuming 'i = 1'
, and the image indeed does not show any red... Thank you for any help!