The ‘RGB_image’参数必须是一个三维数组,其中第三维等于3?

huangapple go评论92阅读模式
英文:

The 'RGB_image' parameter must be a 3-dimensional array where the third dimension is equal to 3?

问题

library(abind)
library(OpenImageR)

此代码加载数据

path = list.files("../df", ".jpg", full.names = TRUE)
# 读取数据
imgs = lapply(path, readImage)

这将数据绑定为一个数组

comb <- do.call(abind, c(imgs, list(along = 3)))

上述代码的输出为6张图像的大小为480X640X18。
这段代码用于将图像转换为灰度,但它出现了错误

错误信息:

'RGB_image' 参数必须是三维的

df <- rgb_2gray(comb)

df <- rgb_2gray(comb)
英文:
library(abind)
library(OpenImageR)

This code load the data

path = list.files(&quot;../df&quot;,&quot;.jpg&quot;, full.names = T)
#read the data
imgs=lapply(path, readImage)

This binds the data as an array

comb &lt;- do.call(abind,c(imgs,list(along =3)))

The output of the above is 480X640X18 for six images.
This code is to convert images to grayscale but it does not work it gives an error

the error:

> The 'RGB_image' parameter must be a 3-dimensional

df&lt;-rgb_2gray(comb)

df&lt;-rgb_2gray(comb)

答案1

得分: 0

以下是翻译好的部分:

少数要点:

rgb_2gray() 函数接受一个“第三维等于3的三维数组”,可以从文档中找到相关信息。因此,在使用 abind() 合并图像之前,您需要先将图像转换为灰度图像(例如使用 lapply)。

在使用 abind() 之前,图像必须具有相同的大小,您可以使用 resizeImage() 强制使它们具有相同的大小。

最后,您可以绘制图像,以查看是否获得了期望的结果。

library(abind)
library(OpenImageR)

# 设置正确的目录路径,对我来说路径包含了3个jpg图像
path <- list.files(".", ".jpg", full.names = TRUE)

# 读取数据
imgs <- lapply(path, readImage)

gray_imgs <- lapply(imgs, rgb_2gray)
gray_imgs <- lapply(gray_imgs, function(x) resizeImage(x, 256, 256))
comb <- do.call(abind, c(gray_imgs, list(along = 3)))
dim(comb)
# [1] 256 256   3
n <- dim(comb)[3]

# 显示图像
par(mfrow = c(1, n)) # 对我来说路径包含3个jpg图像,n = 3
for (i in 1:n) {
  image(rotateImage(comb[, , i], 270), col = gray.colors(256))  
}

The ‘RGB_image’参数必须是一个三维数组,其中第三维等于3?

英文:

Few points:

The function rgb_2gray() accepts a "3-dimensional array where the third dimension is equal to 3" from the documentation. Hence, you need to first convert the images to grayscale (e.g., with lapply) before you can use abind() to combine them.

Before you use abind(), the images must be of same size, to force it you can use resizeImage().

Finally, you can plot the images to see if you are obtaining the desired result.

library(abind)
library(OpenImageR)

# set the proper directory with setwd(), for me the path contained 3 jpg images 
path &lt;- list.files(&quot;.&quot;,&quot;.jpg&quot;, full.names = T)
#read the data
imgs &lt;- lapply(path, readImage)

gray_imgs &lt;- lapply(imgs, rgb_2gray)
gray_imgs &lt;- lapply(gray_imgs, function(x) resizeImage(x, 256, 256))
comb &lt;- do.call(abind,c(gray_imgs,list(along =3)))
dim(comb)
# [1] 256 256   3
n &lt;- dim(comb)[3]

# display the images
par(mfrow = c(1, n)) # for me the path contained 3 jpg images, n = 3
for (i in 1:n) {
  image(rotateImage(comb[, , i], 270), col=gray.colors(256))  
}

The ‘RGB_image’参数必须是一个三维数组,其中第三维等于3?

huangapple
  • 本文由 发表于 2023年7月23日 21:25:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76748478.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定