Png image crop, and plot - r

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.

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)

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.

Related

Rasterize SpatVect (points) with buffer around SpatRaster

I have a SpatVect consisting of points and I want to rasterize them into a SpatRaster with a given resolution. Is there a way of specifying a function taking in the points that are within a buffer of each raster cell?
Many thanks
Joao
-- Update --
Maybe a figure would help understand what I'm after with my question. The red square will have to be run over the center of each pixel to calculate some statistics using the ovelaying points. Apologies for the clumsy question, but I hope the figure is clear enough...
terra version 1.6-28 supports rasterization of points with a rectangular moving window.
Example data
library(terra)
#terra 1.6.33
r <- rast(ncol=100, nrow=100, crs="local", xmin=0, xmax=50, ymin=0, ymax=50)
set.seed(100)
x <- runif(50, 5, 45)
y <- runif(50, 5, 45)
z <- sample(50)
v <- vect(data.frame(x,y,z), geom=c("x", "y"))
Solution
r1 <- rasterizeWin(v, r, field="z", fun="count", pars=10, win="rectangle")
plot(r1)
points(x, y)
You can change fun to another function that works for you, and you can change the size of the moving window with pars.
Instead of a rectangle, you can also use a circle or an ellipse. The border of a circular window is equidistant from the center of the cells. In contrast, the border of rectangles are at a constant distance from the border of the grid cells in most directions (not at the corners). Here is an example.
r2 <- rasterizeWin(v, r, field="z", fun="count", pars=5.25, win="circle")
plot(r2)
You can also use buffers around each cell to get a window that is truly equidistant from each cell border.
r3 <- rasterizeWin(v, r, field="z", fun=length, pars=5, win="buf")
plot(r3)
In this case, because the buffer size is large relative to the cell size, the result is very similar to what you get when using a circular window. Using "circle" should be the fastest, and using "buffer" should be the slowest in most cases. The function should now in all cases be memory-safe, except, perhaps when using very large buffers (more could be done if need be).
Version 1.6-28 is currently the development version. You can install it with
install.packages('terra', repos='https://rspatial.r-universe.dev')
The approach you take seems to depend on what result you're looking for from the above and the relationship they have with each other.
library(terra)
`terra::buffer(` # both SpatVectx/SpatRastery, to distance in 'm'
`terra::buffer(` # that is meaningful
#take Rasty to SpatVecty
`terra::as.polygons(`, #then
`z<-terra::intersection(SpatVectx, SpatVecty)`
then back to SpatRastz? terra::mask or crop, might also be useful, again depending on where things are going next.

R: creating DEM of an imaginary map from png

I have drawn an imaginary map for fun, and I thought it would be even more fun to make a DEM for the imaginary country using R, just to add to the visualization. :)
Basically, I now have a png with the outline of the country. Just only with a black border, the rest is left white.
My plan was to create a raster from it, so I could eventually add values to every pixel in the map. Since it's a imaginary map, it doesn't have a coordinate system. I just set the extent as it is as png:
drawing <- raster('drawing.png')
extent(drawing) <- c(0, 388, 0, 348)
Then I create a dataframe with the points of which eventually the DEM is calculated from. Basically, I just randomly select pixel locations within the country (name them x and y) and assign a random value as height. For this I use a simple excel sheet:
samples <- read_excel('samples.xlsx')
coordinates(samples) <- ~x+y
spatlocations <- SpatialPoints(samples)
vectlocations <- vect(samples)
Then, I thought, I could create a map using Inverse Distance Weighting (IDW), just to get a nice smooth gradient looking map. So it would possibly something like this:
result <- idw(unsampled, samples)
However, I fail to do this. I for example don't really know how to exactly work with the raster I created from the png, and the same for the sample points.
As you maybe can see, I am what you could consider a beginner :).
Is this possible at all? Is this methodology correct? How can I achieve such a map? Thanks in advance!

Calculating the distance between points and the closest raster cell of a certain value in R

I am currently trying to calculate the distance a set of points and their closes raster cell of a certain value. So far I have tried to convert the raster file into Points, but I keep getting an error message:
Cannot allot Vector of this size. Is there any other way how I can go around this. My datasets are very large (20.000 Points and a raster layer of an entire country).
so far I have tried:
library(raster)
water_points <- rasterToPoints(land_cover , fun = function(x) {x == 405}) #405 are cells that contain water
then I would continue like this:
df$water_dist <- gDistance(df, water_points)
I have also tried to use rastertoPolygons but it seems to show the same problem
Thank you very much

How to extract small dataset images from a large image using box in R

I would like to extract from an image a dataset of smaller images as illustrated as follow. I would like to create a box of 64x64 pixels, translate it in x and y and save each image in JPEG.
Could you suggest a function in R to do it ? I do not find the way the create a box.
You can use the magick-package for this. It has many functions for image-manipulation.
Basically what I did in the following code is reading in the image and then based on image-size create a list of coordinates which spans most of the image (a part of the edges might be missing), but this is only an example and you could modify it to the coordinates you need. Each point refers to the top-left point of one box you want to crop.
Afterwards in the loop I specify the size of the box 64x64 and use to coordinates from earlier to offset this box in each iteration and to give each cropped image a unique name.
install.packages("magick")
library(magick)
# read in image
im <- image_read("example.jpg")
# get image size
im_dim <- dim(image_data(im))
# create offsets for cropping image
coords <- expand.grid(x = seq(0, im_dim[2]-64, by = 64),
y = seq(0, im_dim[3]-64, by = 64))
coords$offset <- paste0("+", coords$x, "+", coords$y)
# crop and save
for(i in coords$offset) {
cropped <- image_crop(im, paste0("64x64", i))
image_write(cropped, paste0("example", i,".jpg"))
}

Converting raster (tiff) image to a pixel image in R - problems when converting spatial polygon into owin object class

I am not an R expert, but i use it for all kinds of image processing. Now I am trying to apply Gaussian blur smoothing (spatstat package) on my satellite S-2 image. Original type of my image is Raster (Raster layer) tiff, actually a subtract image from two Sentinel-2 bands (green and blue). To apply blur on this kind of image I have to first convert it to a pixel image. I've tried doing this following few other questions (like this one Converting a raster object to an im object in R), but i did not succed. I have tried few possibilities, like converting raster image into matrix and than to pixel image, but this does not work, because the image is than too large (although I use small, croped area of the whole Sentinel-2 image).
So, my function in brief looks something like that:
blue <- raster("S2A_OPER_MSI_T33TWH_B02.tif")
green <- raster("S2A_OPER_MSI_T33TWH_B03.tif")
subt <- function(r1, r2) {
return(r2-r1)
}
out_sub1 <- (blue, green, fun = subt)
I tried to apply blur directly on a Raster image, but i soon realized its not working on raster data:
gauss_sub1 <- blur(out_sub1, sigma = 5)
#Error: is.im(x) is not TRUE
So, I try to convert my image into a pixel one
out_sub11 <- as.im(X = "out_sub1")
Error in as.im.function(X, W, ..., dimyx = dimyx, na.replace = na.replace): A window W is required
Therefore I try to define a window following my raster extent
e <- out_sub1#extent
sp_w <- as(e, "SpatialPolygons")
W <- as(sp_w, "owin")
Error in as(SP.win, "owin") : no method or default for coercing “SpatialPolygons” to “owin”
Can anyone tell me what am I doing wrong or how can I convert spatial polygon into owin object class, so I can further process blur command?
And can please someone explain me what difference there is between raster image and a pixel image in R?
You can apply a filter using raster library:
library(raster)
r <- blue - green
# 3 by 3 mean filter
r_mf <- focal(r, w=matrix(1/9,nrow=3,ncol=3))
# gaussian filter
gf <- focalWeight(r, 2, "Gauss")
r_gf <- focal(r, w=gf)

Resources