【问题标题】:How to extract the pixel data Use R's pixmap package?如何提取像素数据 使用 R 的 pixmap 包?
【发布时间】:2011-08-30 09:59:27
【问题描述】:

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

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

图片

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

【问题讨论】:

    标签: r pixmap


    【解决方案1】:

    您可以通过getChannels将数据作为灰度图像的二维矩阵或彩色图像的3维数组。

    >  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
    

    【讨论】:

      【解决方案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)
      

      【讨论】:

        猜你喜欢
        • 2013-01-24
        • 1970-01-01
        • 2020-01-24
        • 2017-05-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-07
        • 1970-01-01
        相关资源
        最近更新 更多