2011-05-21 9 views

risposta

4

È possibile ottenere i dati come matrice 2-D per immagini in scala di grigi o array 3D per immagini a colori entro il getChannels.

> x <- read.pnm(system.file("pictures/logo.ppm", package="pixmap")[1]) 
> y <- getChannels(x) 
> class(y) 
[1] "array" 
> dim(y) 
[1] 77 101 3 
> 
> x <- read.pnm(system.file("pictures/logo.pgm", package="pixmap")[1]) 
> y <- getChannels(x) 
> class(y) 
[1] "matrix" 
> dim(y) 
[1] 77 101 

se si desidera accedere ai dati in modo più diretto, utilizzare S4 di accesso (@), ad es .:

> x <- read.pnm(system.file("pictures/logo.ppm", package="pixmap")[1]) 
> str(x) 
Formal class 'pixmapRGB' [package "pixmap"] with 8 slots 
    [email protected] red  : num [1:77, 1:101] 1 1 1 1 1 1 1 1 1 1 ... 
    [email protected] green : num [1:77, 1:101] 1 1 1 1 1 1 1 1 1 1 ... 
    [email protected] blue : num [1:77, 1:101] 1 1 0.992 0.992 1 ... 
    [email protected] channels: chr [1:3] "red" "green" "blue" 
    [email protected] size : int [1:2] 77 101 
    [email protected] cellres : num [1:2] 1 1 
    [email protected] bbox : num [1:4] 0 0 101 77 
    [email protected] bbcent : logi FALSE 
> [email protected] 
[1] 77 101 
1

Prova questo:

library(pixmap) 

picture <- read.pnm("picture.pgm") 

#Take a look at what you can get (notice the "@" symbols) 
str(picture) 

#If you want to build a matrix using the dimensions of "picture"....  
[email protected]  
mat1 <- matrix(NA, [email protected][1], [email protected][2]) 

#If you want to build a matrix directly from "grey"..... 
mat <- [email protected]  

#Take a look at mat 
head(mat)