Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
468 views
in Technique[技术] by (71.8m points)

colors - How to get pixel data from an image using R

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.

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).

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First I generate an example png image :

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

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

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

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...