gpt4 book ai didi

r - 将 RGB 图像转换为 R EBImage 中的单 channel 灰度图像

转载 作者:行者123 更新时间:2023-12-02 01:55:05 25 4
gpt4 key购买 nike

R 初学者问题:

我想将 RGB 图像转换为灰度并使用 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

由 colorMode(gray) = Grayscale 转换的图像似乎与图像功能不兼容。 R EBImage 中的 Crayscale 图像是否有多个 channel ?

然后我手动转换它并能够在其上调用 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

但是,灰色图像的强度略有不同。有没有办法在 R EBImage 中将 RGB 转换为单 channel 灰度?

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

该软件包提供了一些图像处理功能。例如。我可以使用进一步的 EBImage 命令轻松显示扫描测试 strip (img1) 的强度图 (img2):

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

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

img1 img2

最佳答案

这可能更简单,尽管有更快、更干净的方法来压缩颜色层。

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)

请注意,这在“保存”全彩对象 foo 的同时创建了一个灰度图像对象 bar

关于r - 将 RGB 图像转换为 R EBImage 中的单 channel 灰度图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20347025/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com