How to get data from an image using R

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

I need this information to compare differences in plumage of birds, to help in understanding the speciation event.

My photos were taken with a digital camera and are now NEF files. No matter what type of file you need, I have the ability to convert the file to any convenient for me. However, I would prefer to keep as much information as possible in the file (i.e. PNG files are good).

I 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 tried this: How to extract pixel data Use the pixmap R package? , no luck. My files are too large for the R window (the entire array is not displayed) and it is difficult for me to understand the resulting array. The best thing for me would be to get the data in the form of a matrix or in another way, which will simplify the understanding of what's what. I found many similar questions, but in other programs (such as Java, C ++, IOS, Matlab, Python, etc.), which, unfortunately, I do not know how to use.

My problems may be related to my low skills with this type of work, but I try my best as much as I can against the background that I have. If anyone can help me or give me advice, I will be very grateful.

+5
source share
2 answers

First, I create an example pngimage:

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

Then I convert it to a readable format pixmap: here I convert png to a ppm file, because I want to save color information. I use ImageMagick from the command line, but you can use any program you need:

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

Then you can read the image using the function read.pnmfrom the package pixmap:

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

x@red, x@blue, x@green ( x@grey ), . , :

dim(x@red)
[1] 480 480
+7

3x3 png:

sample image

:

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

PNG- - ( ), pix. , , , - .

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

All Articles