I'm working with some fairly large image files (aerial survey mosaics, generally > 1 billion pixels), such that loading an entire image into memory would be a problem on my system. I would like to bring them into R piece-by-piece, such that I can process them in "grid-wise" sections.
NOTE: I'm not tied to a particular image format, so
image.file <- "~/Desktop/penguins.jpg"
grid.size <- 100
v <- 3
h <- 1
library( jpeg )
image <- readJPEG( image.file )[ seq.int( (v-1)*grid.size+1, (v)*grid.size, 1 ),
seq.int( (h-1)*grid.size+1, h*grid.size, 1 ), ]
If you have ImageMagick installed, you can crop the image from the command line before reading it into R. An example using this image: http://www.worldatlas.com/worldmaps/worldpoliticallarge.jpg
To create the cropped image:
x <- 800 ## x and y are offsets y <- 400 w <- 200 ## width and height of cropped image h <- 100 filename <- "worldpoliticallarge.jpg" outname <- "crop.jpg" cmd <- sprintf("jpegtran -crop %dx%d+%d+%d -copy none %s > %s", w, h, x, y, filename, outname) system(cmd)
Check to see if the new image contains the region we want:
library(jpeg) original <- readJPEG(filename) cropped <- readJPEG(outname) all.equal(original[(y+1):(y+h), (x+1):(x+w), ], cropped) #  TRUE