Search code examples
rr-rasterspatstat

Calculate area from binary image


Ahead, I'm not able to provide a minimum working example since I don't know how. I have an binary mask in an owin. Basically, my data contains five clumped dots of different size. I try to get their area.

As far as I know, the great spatstat package has no function for this.

First, I convert the owin to an raster layer with raster_mask <- raster(as.im(dd)). After that, I detect the clumps as follows:

library(igraph)
clusters <- clump(raster_mask)

Now I can plot clusters with each of the five clusters in its own colour. And as far as I understand the function area(clusters) should provide me with their area, but instead I receive an warning

This function is only useful for Raster* objects with a longitude/latitude coordinates

For control, I use crs(clusters) and see an NA. So I set crs with

crs(clusters) <- "+proj=utm +zone=32 +ellps=GRS80 +units=m +no_defs +type=crs"

Now I have an CRS, but receive the same warning. My data at this points looks as follows:

> print(areas)
class      : RasterLayer 
dimensions : 135, 129, 17415  (nrow, ncol, ncell)
resolution : 0.9790567, 1.224028  (x, y)
extent     : 490715.5, 490841.8, 5429337, 5429502  (xmin, xmax, ymin, ymax)
crs        : +proj=utm +zone=32 +ellps=GRS80 +units=m +no_defs +type=crs 
source     : memory
names      : layer 
values     : 1.198393, 1.198393  (min, max)

So I try to change the CSR to longlat. Before I search for the clumps, using this code and receiving an error.

> crs(raster_mask) <- "+proj=utm +zone=32 +ellps=GRS80 +units=m +no_defs +type=crs"
> raster_mask <- projectExtent(raster_mask, "+proj=longlat +datum=WGS84")
> plot(raster_mask)
Error in .plotraster2(x, col = col, maxpixels = maxpixels, add = add,  : 
  no values associated with this RasterLayer

I might be completely wrong. If someone has an idea how to provide a minimal working example I would be glad to provide it. I hope someone could help me to receive areas from an binary image mask. Any help for orientation is welcome!

Thank you ahead!


Solution

  • As far as I know, the great spatstat package has no function for this.

    In spatstat you can use the function connected to identify the connected components.

    If dd is your original binary mask (object of class "owin"), then P <- connected(dd) returns a pixel image P with categorical values. Each categorical value corresponds to a connected component. A plot of this image, plot(P), would show a coloured version of the original data, with each connected component filled with a different colour.

    You can calculate the areas of each connected component from this. A neat way is to treat the connected components as the tiles in a tessellation. Example:

    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)