Convert RGB image to single-channel gray image in R EBImage

R-beginner question:

I wanted to convert an RGB image to grayscale and display / plot it using an image ()

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

Image converted colorMode (gray) = Grayscale is not compatible with the image function. Does Crayscale image in R EBImage have more than one channel?

Then I converted it manually and was able to call the image () on it

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

However, grayscale images were slightly different from the intensity. Is there a way to convert RGB to single gray channel in R EBImage?

EDIT To answer the question why EBImage:

. . (img2) (img1) EBImage:

blotgraph = resize(gblur(gray1,3),200,1)
plot(blotgraph, type="l")

, EBImage

img1img2

+4
2

, , , , . EBImage grayimage<-channel(yourimage,"gray") ( )

Image
  colormode: Grayscale 
  storage.mode: double 
  dim: X Y 
  nb.total.frames: 1 
  nb.render.frames: 1 

colormode nb.total.frames:3

+6

, .

library(png)
foo<-readPNG("c:/test/b/s2.png")
#that 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)

, bar, "" foo.

+7

All Articles