如何使用R从图像获取像素数据

如何使用R从图像获取像素数据

问题描述:

任何人都可以帮助我如何从R中的图像获取RGB像素数据

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 a speciation event.

我的照片由数码相机拍摄,现在作为NEF文件。无论需要哪种类型的文件,我都可以将文件转换为我想要的文件。然而,我更喜欢在文件中保持尽可能多的信息(即PNG文件是好的)。

My photos are taken by a digital camera and are now as a NEF-file. It doesn't matter which type of file that's needed, I have the possibility to convert the file to whatever I want. However, I would prefer to maintain as much information as possible in the file (i. e. PNG-files are good).

我在R:Pixmap,Raster,ImageMetrics中尝试过许多软件包,并浏览了互联网,测试方法,问同事等几个星期试图解决这个问题。
这里在Stackoverflow我试过这个:如何提取像素数据使用R的像素映射包?,没有运气。我的文件对于R窗口也太大了(整个数组不显示),我很难理解产生的数组。对我来说最好的事情是把数据作为一个矩阵或者另一种方式,使得更容易理解什么是什么。
我发现了类似的问题,但在其他程序(如Java,C ++,IOS,Matlab,Python等),我不幸不知道如何使用。

I have tried many packages in R: Pixmap, Raster, ImageMetrics and browsed the internet, tested methods, asked co-students etc. for several weeks trying to solve this problem. Here at Stackoverflow I've tried this: How to extract the pixel data Use R's pixmap package?, with no luck. My files are also too big for the R window (the entire array doesn't show), and I have difficulties understanding the array produced. The best thing for me would be to get the data as a matrix or in another way that makes it easier to understand what is what. I have found loads of similar questions, but in other programs (such as Java, C++, IOS, Matlab, Python etc) which I unfortunately don't know how to use.

我的问题可能是由于我这种类型的工作的低技能,但我正在努力,我可以与我的背景。
如果任何人可以帮助我或给我一些提示,我将非常感激。

My problems might be due to my low skills with this type of work, but I am trying as hard as I can with the background that I have. If anyone can help me or give me som tips, I will be very grateful.

示例 png image:

png("/tmp/test.png")
plot(rnorm(100))
dev.off()

然后我将其转换为 pixmap 可读的格式:这里我将png转换为ppm文件,因为我想保留颜色信息。我从命令行使用ImageMagick,但你可以使用任何你想要的程序:

Then I convert it to a format readable by pixmap : here I convert the png to a ppm file as I want to keep colors information. I use ImageMagick from the command line, but you can use whatever program you want :

$ convert /tmp/test.png /tmp/test.ppm

接下来,您可以使用 pixmap 软件包中的.pnm 函数:

Next, you can read the image with the read.pnm function from the pixmap package :

x <- read.pnm("/tmp/test.ppm")

,您可以使用 x @ red x @ blue x @ green 插槽(或对于灰度图像,或 x @ gray ),以获取每个通道的像素值作为矩阵。您可以检查矩阵的尺寸是否与图片大小相同:

And then, you can use the x@red, x@blue, x@green slots (or x@grey for a greyscale image) to get the pixels value for each channel as a matrix. You can check that the dimensions of the matrices are the same as the size of your picture :

dim(x@red)
[1] 480 480