I have a raster image (20231031_110019.png, although it could be any format, jpg, png, bmp) and would like to individually
calculate the areas of discrete regions (I will call them 'islands' although they are actually areas of fungal growth on a petri dish) in this raster.
I am trying to use the spatstat package in R to do this. I found a post stating the following code can do this:
library(spatstat)
## create example data
dd <- dilation(redwood, 0.5, polygonal=FALSE)
## find connected components
P <- connected(dd)
## convert to a tessellation
B <- tess(image=P)
## compute area of each tile
answer <- tile.areas(B)
An owin object (redwood in the above instance) is needed to execute the dilation command and I cannot find a way to convert a raster into an owin object without the maptools package, which is no longer available on CRAN. I also have been unable to install an archived version maptools using devtools.
What would a workaround be for this?
Is there another way to convert a raster into a useable object?
Is there an equally straightforward method for calculating the areas of the fungal 'islands' in my raster image?
Many thanks!
Emily




With "terra" you can first identify the patches, and then count the number of cells in each patch. You could then multiply that number with the actual size of each pixel to get areas. "patches" are defined as islands of values in a sea of missing values; so in this case 255 needs to be set as the missing values flag.
Some illustration
To go from pixel counts to area you need to know the size of your image in the real world. Let's say it has a height of 20 and a width of 15 cm. In that case you could do
With area expressed in cm2.
At that point you could also go the polygons-route suggested by Eduardo like this:
PS: Note that if this were geographic data you would want to set the coordinate reference system with
crs<-and usecellSizeto get the actual size of the cells in the real world and then usezonalto compute the area of each patch.