4

I'm looking for a way to import Raw image into R.

Indeed, there are many packages which permit to import .bmp, .png, .jpeg or .tiff images into R (ImageMagick, EBImage, imager, bmp, tiff, TIFF, pixmap, Momocs and many others..) but these files formats have a conversion (white balance, contrast, saturation..) and the pictures can be different (in term of saturation, lightness..) even if the taking pictures is standardised but the objects have different size or colour. The Raw picture haven't this problem.

I know the extension differ according to the brand of the camera, which complicates the thing.

But i would like to analyse the difference between saturation, lightness in a more precise and relevant way than .jpeg or .tiff.

Despite my research, I block on the first step : The Raw image importation..

Has someone already achieved this pass ? Does it exist bash command to import this kind of file ?
I'm not looking to see the picture, but to have access of the raw data.

  • Not sure what you want to do with the data, but ImageMagick can import raw data. I presume Rmagick can do it also. For example: `convert -size 512x512 -depth 8 image.rgb image.png`. ImageMagick uses either dcraw or ufraw as a delegate to do the work. – fmw42 May 16 '18 at 05:11

2 Answers2

4

You can use the readRaw() function from the hexView package :

library(hexView)
raw_image <- readRaw("image_path")

The data is then in raw_image$fileRaw. It is stored as a vector of class "raw".

You can simply turn it into a matrix :

image_matrix <- matrix(raw_image$fileRaw, nrow = image_height, ncol = image_width, byrow = TRUE)

It should recreate what you had in your raw file.

NB : image_height and image_width would be in pixels.

Now you can easily plot your image via as.raster:

plot(as.raster(image_matrix))
3

Note that if you do not want to add a package dependency and only need to read in the raw data, readBin could suffice.

Example:

raw_data <- readBin("test.png", "raw")
raw_data[1:10]
# [1] 89 00 00 00 00 00 00 00 00 00

You can explore other options for readBin with ?readBin

cole
  • 1,737
  • 2
  • 15
  • 21