0

Can't interpret X as a point pattern, ask for help!

library(imager) library(spatstat)

Read image

img <- load.image("C:/Users/yyan2/Documents/Dec 21/F10F.jpg") img_gray <- grayscale(img) threshold <- 0.5 # adjust this value as needed img_thresh <- img_gray > threshold class(img_thresh) # convert the binary image to a matrix [1] "pixset" "imager_array" "logical"
binary_image <- as.im(matrix(as.numeric(img_thresh), nrow = nrow(img_thresh),

  •                          ncol = ncol(img_thresh))) # convert to binary image
    

image_window <- owin(c(1, ncol(img_thresh)), c(1, nrow(img_thresh))) point_pattern <- as.ppp(binary_image, window = image_window) Error in as.ppp.default(binary_image, window = image_window) : Can't interpret X as a point pattern

I have tried all kinds of function that I know, it won't work. so frustrating

1 Answers1

1

The function as.ppp (and in general any conversion function as.xxx) is designed to convert data from one format to another format which represents the same information.

It seems that you want to convert a binary pixel image into a point pattern. But these are different kinds of information, so exactly what do you want to do?

  1. Do you want every pixel that is TRUE in the binary image to become a separate point in the point pattern?
  2. Or does the binary image contain blobs/islands/clusters of TRUE pixels and you want the centre of each island to be a separate point in the point pattern?

It's unclear what is intended, since you did not provide a working example. Neither I nor the computer can guess what you want.

If your intention is (1) above, then use pixelcentres, e.g.

P <- pixelcentres(binaryimage)

If your intention is (2) above, use connected.im to identify the islands, then extract them and compute a suitable midpoint, e.g.

Labelled <- connected(binaryimage)
Islands <- tiles(tess(image=Labelled))
Centres <- lapply(Islands, centroid.owin)
xy <- t(sapply(Centres, as.numeric))
P <- as.ppp(xy, W=Window(binaryimage))
Adrian Baddeley
  • 2,534
  • 1
  • 5
  • 8