我需要您的帮助和建议
我必须在R中开发一个脚本,以保存由数据帧中的多个图像转换产生的向量,该数据帧将具有与图像一样多的行(1000)和许多列 作为变量(4096)。数据框将导出到一个csv文件。
我尝试了一个循环,以便它读取图像并将其一一转换,然后将它们加入矩阵或数据帧中。我是新来的...我迷路了... 我分享你的尝试。 有任何想法或改进吗?
set.seed(1234)
mypath = "C:/dataset/dataset/effusion/" #file where the images are
files <- list.files(path=mypath, pattern=".png$")
length(files)
for (i in files){
im <- readImage(mypath[i], header = FALSE)
grises <- rgb_2gray(im[i])
# Resize the image to a size of 64 x64 pixels:
resiz = resizeImage(grises[i], width = 64, height = 64, method = 'nearest')
# Convert to vector:
im.convert <- as.vector(resiz[i])
out <- as.data.frame(do.call(rbind, im.convert)) # create a data frame
}
return(out)
非常感谢您!
答案 0 :(得分:0)
您的代码有几个问题,建议您回顾一下如何编写循环(例如,如果您不在函数中,则无需使用return
-您还将i索引放在并不需要)。
这应该可以为您提供一个数值矩阵:
library(OpenImageR)
mypath = "C:/dataset/dataset/effusion/" #file where the images are
files = list.files(path=mypath, pattern=".png$", full.names = TRUE) # add full.names option to get full path
out = numeric()
for (f in files){
im = readImage(f)
grises = rgb_2gray(im)
# Resize the image to a size of 64 x64 pixels:
resiz = resizeImage(grises, width = 64, height = 64, method = 'nearest')
# Convert to vector:
out = rbind(out, as.vector(resiz))
}
out