MadmanLee
MadmanLee

Reputation: 558

Png image crop, and plot

Is there a simple way to crop a specified pixel location of a png image, and raster that image onto a plot.

Currently i am working with dissociated cells (up to 1000 cells in a field of view). Each cell has a center x (in pixels) and a center y (in pixels). We gather data in trace format, so i would like a way of displaying the picture of the cells next to the trace.

I have tried rasterImage, and grid.raster. Unfortunately i have no idea how to call upon the image array to specify the location.

Also, i do not want to use the zoom package since the functions work extremely slow and are not able to raster onto a current plot.

Upvotes: 0

Views: 1934

Answers (2)

MadmanLee
MadmanLee

Reputation: 558

It was alot easier than expected. My PNG image was an array with 3 layers.

img[xpos, ypos, layer]

Therefore i could simply specify the location of my cells increased by a factor,

rasterImage(img[(x-x*.2):(x+x*.2), (y-y*.2):(y+y*.2),],-2, 4, 3, 1)

Now i need to code around for cells at the limits of my plots.

Upvotes: 1

Jeffrey Evans
Jeffrey Evans

Reputation: 2397

Take a look at the raster package. The raster::raster function, for importing or coercing your png and raster::crop for subsetting to a specific extent.

Here is an example from the crop functions help.

Create an example raster

r <- raster(nrow=45, ncol=90)
r[] <- 1:ncell(r)

Subset the raster based on a define extent

e <- extent(-160, 10, 30, 60)
rc <- crop(r, e)     

Plot results

par(mfrow=c(1,2))
  plot(r)
  plot(rc)

Upvotes: 1

Related Questions