如何提取像素数据使用R的pixmap包?
所以我使用:
读取图像文件picture< - read.pnm(“picture.pgm”) 图片 Pixmap图片 输入:pixmapGrey 尺寸:749x745 分辨率:1x1 包围框:0 0 745 749
如何将像素数据提取到某个矩阵中?
答案 0 :(得分:4)
您可以通过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
答案 1 :(得分:1)
试试这个:
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)