在R EBImage中将RGB图像转换为单通道灰度图像

Val*_*itz 4 r image-processing

一个R-beginner问题:

我想将RGB图像转换为灰度并使用图像()显示/绘制它

library(EBImage)
orig = readImage("c:/test/b/s2.png")
gray = orig
colorMode(gray) = Grayscale

display(gray) #works
image(gray) #Error 'z' should be a matrix
Run Code Online (Sandbox Code Playgroud)

由colorMode(灰色)=灰度转换的图像似乎与图像功能不兼容.R EBImage中的Crayscale图像是否有多个通道?

然后我手动转换它,并能够调用image()

r = channel(orig,"r")
g = channel(orig,"g")
b = channel(orig,"b")

gray1 = 0.21*r+0.71*g+0.07*b

display(gray1) 
image(gray1) #works
Run Code Online (Sandbox Code Playgroud)

然而,灰度图像的强度略有不同.有没有办法在R EBImage中将RGB转换为一个通道灰度?

编辑 为了回答这个问题,为什么选择EBImage:

该包提供了一些图像处理功能.例如,我可以使用更多EBImage命令轻松显示扫描测试条纹(img1)的强度图(img2):

blotgraph = resize(gblur(gray1,3),200,1)
plot(blotgraph, type="l")
Run Code Online (Sandbox Code Playgroud)

我不知道如何在没有EBImage的情况下解决这类任务

IMG1 IMG2

Car*_*oft 7

这可能更简单,但有更快更清洁的方法来压缩颜色层.

library(png)
foo<-readPNG("c:/test/b/s2.png")
#that's an array n by m by 3 . Now reduce to grey
bar<- foo[,,1]+foo[,,2]+foo[,,3]
# normalize
bar <- bar/max(bar)
# one of many ways to plot
plot(c(0,1),c(0,1),t='n')
rasterImage(bar, 0,0,1,1)
Run Code Online (Sandbox Code Playgroud)

请注意,这会在bar"保存"全色对象时创建灰度图像对象foo.


Ton*_*ony 6

对不起,如果我错了,但据我所知,你没有以适当的方式转换为灰度.通常用EBImage转换为灰度是用命令grayimage<-channel(yourimage,"gray") 和图像(grayimage)给出的

Image
  colormode: Grayscale 
  storage.mode: double 
  dim: X Y 
  nb.total.frames: 1 
  nb.render.frames: 1 
Run Code Online (Sandbox Code Playgroud)

colormode给出的地方 nb.total.frames:3