2011-05-21 38 views

Trả lời

4

Bạn có thể lấy dữ liệu dưới dạng ma trận 2-D cho hình ảnh thang độ xám hoặc mảng 3-D cho hình ảnh màu bằng 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 

nếu bạn muốn truy cập dữ liệu trực tiếp hơn, sử dụng S4 accessor (@), ví dụ .:

> 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

Hãy thử điều này:

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) 
Các vấn đề liên quan