2017-08-13 100 views
1

我有一個單幀灰度圖像。我想修改圖像,以便某個值的所有像素變成彩色的,例如紅色。很顯然,我需要將圖像轉換爲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

你明白了。 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