5

如何提取像素数据 使用 R 的 pixmap 包?

所以我使用以下方法读取图像文件:

图片 <- read.pnm("picture.pgm") 图片像素图图像类型:pixmapGrey 大小:749x745 分辨率:1x1 边界框:0 0 745 749

如何将像素数据提取到某个矩阵中?

4

2 回答 2

4

您可以将数据作为灰度图像的 2-D 矩阵或彩色图像的 3-D 数组通过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

如果您想更直接地访问数据,请使用 S4 访问器 ( @),例如:

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

尝试这个:

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"....    
picture@size    
mat1 <- matrix(NA, picture@size[1], picture@size[2]) 

#If you want to build a matrix directly from "grey".....  
mat <- picture@grey    

#Take a look at mat
head(mat)
于 2011-05-22T01:35:47.847 回答