Reputation: 3195
I have folder with images
C:/Users/admin/Downloads/mypicture
Here several images, I need read it.
I use library("EBImage")
x <- readImage("C:/Users/admin/Downloads/mypicture")
then get the error
Error in readImage("C:/Users/admin/Downloads/mypicture") : Unable to determine type of C:/Users/admin/Downloads/mypicture: Filename extension missing.
How to read all pictures at once, and then extract pixel array for each picture
# 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
display(y)
# extract the pixel array
z <- imageData(y)
How to do it?
here two images from my folder
I need to create pixel array
z <- array(y)
like this, where marked name of picture 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
How to create such dataframe
result for picture with digits
[,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
Upvotes: 1
Views: 857
Reputation: 4520
readImage
function takes as input a vector of file names, so in order to read all images from a directory you need to list its contents
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 data is stored in an Image
class which internally holds an array
# internal representation of images data
y <- class(imageData(imgs))
# dimensions of image stack
dim(imgs)
## [1] 825 825 4 10
The first two dimensions correspond to the XY dimensions, the third one holds color channels (RGBA in this case), and the last one are the individual images. In order to extract a single image, for example the second one, use
img <- getFrame(imgs, 2, "render")
If you would like to refer to images by their original names you could use an approach similar to the following.
# mapping of file names to frame indices
idx <- setNames(1:length(fls), basename(fls))
getFrame(imgs, idx['sample.png'], "render")
Upvotes: 1