How to get pixel data from an image using R

后端 未结 2 1634
眼角桃花
眼角桃花 2020-12-05 16:41

Can anyone help me on how to get the RGB pixel data from an image in R?

I need this information to compare differences in bird plumage to aid in the understanding of

2条回答
  •  春和景丽
    2020-12-05 17:00

    Take this tiny sample 3x3 pixel png image:

    sample image

    Then:

    library('png')
    download.file('http://i.stack.imgur.com/hakvE.png', 'sample.png')
    img <- readPNG('sample.png')
    pix.top.left <- img[1,1,]     # row 1, column 1
    pix.bottom.left <- img[3,1,]  # row 3, column 1
    pix.top.right <- img[1,3,]    # row 1, column 3
    

    If you're reading a PNG image with alpha channel (as the sample image above), then each of the pix. variables is a vector with four entries, each entry corresponding to Red ,Green, Blue and Alpha values of the pixel.

    > pix.top.left
    [1] 1 0 0 1
    

提交回复
热议问题