2

I have a jpeg image that I am attempting to extract the RGB values from in R.

Here is the image:

enter image description here

I am able to access the pixel values quite easily with the following code:

library(jpeg)

y <- readJPEG("MOLD_1.jpg")
head(y)

This returns:

[1] 0.9450980 0.9450980 0.9450980 0.9490196 0.9490196 0.9529412

I'm interested in knowing the color values associated with those pixels. I've tried to use alot of packages to figure this out including raster, pixmap, etc.

I'm struggling pretty bad - any help would be appreciated.

pol_guy
  • 457
  • 3
  • 8
  • 20
  • Related/Duplicate posts: [here](http://stackoverflow.com/questions/16163611) and [here](http://stackoverflow.com/questions/14769628) – zx8754 Aug 24 '16 at 20:13

1 Answers1

2

readJPEG returns a 3-D array that is height x width x channels. You can access individual color values using standard indexing. For example, y[,,1] will give you a height x width matrix of red intensities. You can convert these to color values using the rgb() function:

val <- rgb( y[,,1], y[,,2], y[,,3] )
myImg <- matrix( val, dim(y)[1], dim(y)[2] )
Artem Sokolov
  • 13,196
  • 4
  • 43
  • 74