0

I would like to read (multiple) images (e.g. with read.jpeg, EBImage) than select a part of the image (ROI) and calculate the brightness for this area (e.g. brightness = sqrt( .241*R^2 + .691*G^2 + .068*B^2 )). How can I access this values (RGB)? The example code just opens and crops a image:

require('EBImage')
Image <- readImage('path/image.JPG')
display(Image)
Image <- Image[200:400, 200:300,]
display(Image)
.
.
.
Y  <-  0.2126*R^2 + 0.7152*G^2 + 0.0722*B^2

Finally I will run this through a loop over thousands of pictures and write the value for each picture in a vector, so it should be very efficient.

Hans
  • 141
  • 3
  • 10
  • First, read the help file for `readImage`. I would do it for you but you haven't stated where that package can be found. In the meantime, consider the CRAN package `jpg` which produces an `X by y by r,g,b` 3D array. From that it's trivial to do what you want. – Carl Witthoft Jan 10 '15 at 13:18
  • perhaps `browseVignettes("EBImage")` will help? perhaps `imgk = channel(img, 'rgb')` – ckluss Jan 10 '15 at 22:38

1 Answers1

1

EBImage is a Bioc package (but since it will not compile properly on a Mac running Yosemite) I failed in my initial efforts to explore this problem. On this machine running Lion I am able to demonstrate how to look at items of formal class "Image" and how to pick them apart. (I'm using a commercial file that was attached to one of my emails as my test case, and it is only 142 x 69 pixels so I used a smaller "window"

> Image <- Image[75:100, 25:50,]
> display(Image)
> str(Image)
Formal class 'Image' [package "EBImage"] with 2 slots
  ..@ .Data    : num [1:21, 1:31, 1:3] 1 1 1 1 1 ...
  ..@ colormode: int 2

So the .Data component of this S4 object is a 21 x 31 x 3 R array. There are presumably a red, green and blue layer although I'm not an image expert and there might be other color schems. This will let you "see" the upper left corner of the first layer:

> Image@.Data[ 1:5,1:5 , 1]
     [,1]      [,2]      [,3]      [,4]      [,5]
[1,]    1 0.9882353 1.0000000 1.0000000 1.0000000
[2,]    1 1.0000000 1.0000000 1.0000000 1.0000000
[3,]    1 1.0000000 1.0000000 0.9803922 0.8470588
[4,]    1 1.0000000 0.9960784 0.8588235 0.6627451
[5,]    1 1.0000000 0.9607843 0.8470588 0.6901961

Notice the at-sign rather than the dollar sign as the infix extraction operator. This will provide a mechanism for calculating the sum of layer values with the coefficients and squaring you requested:

> Y  <-  0.2126*Image@.Data[ , , 1]^2 + 0.7152*Image@.Data[ , , 2]^2 + 0.0722*Image@.Data[ , , 3]^2
> str(Y)
 num [1:21, 1:31] 0.974 0.956 0.957 0.963 0.97 ...

The result is an R matrix of "intensities" of the same dimensions as the projected RGB layers (if that are what .jpg files are using.)

IRTFM
  • 258,963
  • 21
  • 364
  • 487