rmatlabimage-processing

Slow image loading R using EBImage


Why is it much slower to load in a large image (~4MB) in R using the EBImage package, compared to matlab? is there anyway around this?

Note: I'm pretty sure EBImage is in some way a wrapper for imagemagick in R

in R:

system.time(im <- readImage("~/Desktop/image.jpg"))
Elapsed time is 10.935 seconds. 

in Matlab:

tic; 
im = imread('~/Desktop/image.jpg');
toc;
Elapsed time is 0.555381 seconds.

using the raster package

system.time(im <- brick('image.jpg'))

takes 0.264 seconds

When trying to get the values

system.time(vals <- getValues(im))

takes 8.617 seconds so I'm back to square one since I need to extract a channel


Edit 2

I ended up using the package jpeg which has function readJPEG and performs better than anything i've tried out there.

 system.time(x<-readJPEG('~/Desktop/image.jpg'))

Takes about 1.431 seconds for a 4mb image and returns a n x m x 3 matrix with each layer of the matrix being a color channel


Solution

  • I don't know why it's slow, but try using raster instead. You can plot the results of reading it in with image

    require( raster )
    im <- brick("~/Desktop/image.jpg")
    image( im , y = 1 )
    
    # Or for a rgb coloured image...
    plotRGB( im , r=1 , g = 2 , b = 3 )
    
    # To extract values of each layer
    vals <- getValues( im )
    
    # A quicker way to get the values would be to use the as.matrix method for rasters
    vals <- as.matrix( im )
    

    Raster might give you a warning about georeferencing, but it should work and it should be pretty darn quick.