我有包含图片的文件夹
C:/Users/admin/Downloads/mypicture
这里有几张图片,我需要阅读。
我使用library("EBImage")
x <- readImage("C:/Users/admin/Downloads/mypicture")
然后得到错误
Error in readImage("C:/Users/admin/Downloads/mypicture") :
Unable to determine type of C:/Users/admin/Downloads/mypicture: Filename extension missing.
如何一次读取所有图片, 然后为每张图片提取像素阵列
# width and height of the original image
dim(x)[1:2]
# scale to a specific width and height
y <- resize(x, w = 200, h = 100)
# scale by 50%; the height is determined automatically so that
# the aspect ratio is preserved
y <- resize(x, dim(x)[1]/2)
# show the scaled image
显示(y)
# extract the pixel array
z <- imageData(y)
如何做到?
我需要创建像素阵列
z <- array(y)
像这样, 标有图片名称kBFrf.jpg的地方
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1,] 0.4676471 0.5000000 0.5382353 0.5803922 0.6107843 0.6441176 0.6715686 0.6843137 0.6901961 0.7117647 0.7303922 0.7362745
[2,] 0.4735294 0.5078431 0.5441176 0.5745098 0.6186275 0.6392157 0.6774510 0.6882353 0.7127451 0.7245098 0.7500000 0.7764706
[3,] 0.4735294 0.5039216 0.5470588 0.5892157 0.6186275 0.6539216 0.6764706 0.7049020 0.7225490 0.7343137 0.7637255 0.7862745
[,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24]
[1,] 0.7686275 0.7843137 0.8000000 0.8156863 0.8450980 0.8460784 0.8519608 0.8539216 0.8529412 0.8607843 0.8647059 0.8568627
[2,] 0.8049020 0.8274510 0.8392157 0.8745098 0.8843137 0.8892157 0.8960784 0.8882353 0.8911765 0.9009804 0.8950980 0.8872549
[3,] 0.8058824 0.8303922 0.8558824 0.8735294 0.8784314 0.8901961 0.8911765 0.8882353 0.8794118 0.8823529 0.8803922 0.8852941
-
69onL.jpg
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1,] 0.4676471 0.5000000 0.5382353 0.5803922 0.6107843 0.6441176 0.6715686 0.6843137 0.6901961 0.7117647 0.7303922 0.7362745
[2,] 0.4735294 0.5078431 0.5441176 0.5745098 0.6186275 0.6392157 0.6774510 0.6882353 0.7127451 0.7245098 0.7500000 0.7764706
[3,] 0.4735294 0.5039216 0.5470588 0.5892157 0.6186275 0.6539216 0.6764706 0.7049020 0.7225490 0.7343137 0.7637255 0.7862745
[,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24]
[1,] 0.7686275 0.7843137 0.8000000 0.8156863 0.8450980 0.8460784 0.8519608 0.8539216 0.8529412 0.8607843 0.8647059 0.8568627
[2,] 0.8049020 0.8274510 0.8392157 0.8745098 0.8843137 0.8892157 0.8960784 0.8882353 0.8911765 0.9009804 0.8950980 0.8872549
[3,] 0.8058824 0.8303922 0.8558824 0.8735294 0.8784314 0.8901961 0.8911765 0.8882353 0.8794118 0.8823529 0.8803922 0.8852941
如何创建此类数据框
带数字的图片的结果
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18] [,19]
[1,] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1.0000000 1.0000000
[2,] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1.0000000 1.0000000
[,20] [,21] [,22] [,23] [,24] [,25] [,26] [,27] [,28] [,29]
[1,] 1.00000000 1.00000000 1.000000000 1.000000000 1.000000000 1.000000000 1.000000000 1.000000000 1.000000000 1.000000000
答案 0 :(得分:1)
readImage
函数将文件名的向量作为输入,因此,要从目录中读取所有图像,您需要列出其内容
library(EBImage)
# create a vector of file names
fls <- dir("./img", full.names = TRUE)
# read images
imgs <- readImage(fls, 'png')
# display images
display(imgs, method = 'raster', all = TRUE)
图像数据存储在Image
类中,该类内部保存着一个array
# internal representation of images data
y <- class(imageData(imgs))
# dimensions of image stack
dim(imgs)
## [1] 825 825 4 10
前两个尺寸对应于XY尺寸,第三个尺寸保持颜色通道(在这种情况下为RGBA),最后一个尺寸是单个图像。为了提取单个图像,例如第二个图像,请使用
img <- getFrame(imgs, 2, "render")
如果您想使用图像的原始名称来引用图像,则可以使用类似于以下内容的方法。
# mapping of file names to frame indices
idx <- setNames(1:length(fls), basename(fls))
getFrame(imgs, idx['sample.png'], "render")