I have a raster:
r <- raster(ncol=10, nrow=10)
set.seed(0)
values(r) <- runif(ncell(r))
From the raster I select the top 10% and change to binary:
r_10<-r[[1]]>=quantile(r,.90)
From this subset raster r_10
all green pixels have the same value of 1. I would like to change these values, by identifying pixels or groups of pixels as objects and labeling every new object with a new ID. The new raster should have values like this example image:
Some objects can have multiple pixels, and they all should have the same object ID (like number 8).
How can I code this up in R? I thought to use some sort of edge detection, or Sobel filter, but cant figure it out.
Here is a similar post, not the same, but its in python, and I need to implement this in R.
Any alternative solutions are welcome.