How to obtain size of cluster of pixels in R

时间秒杀一切 提交于 2021-01-27 21:05:49

问题


I have a picture of 2 colors. Red color pixels are in form of cluster. I would like to know the max dimension of each cluster to compare with the acceptable tolerance. How to do? Is there any function to perform it?


回答1:


For this kind of image analysis, you can check out EBImage:

install.packages("BiocManager")
BiocManager::install("EBImage")

Your workflow might look something like this. First, load the packages and read in your image. We'll also display it to show we're on the right track:

library(EBImage)
library(ggplot2)

dots <- readImage("https://i.stack.imgur.com/3RU7u.png")
display(dots, method = "raster")

Now we can use the computeFeatures functions to get the centroids and maximum diameter of each cluster:

dots_bw <- getFrame(dots, 1)
labelled_dots <- bwlabel(dots_bw)
df <- as.data.frame(cbind(computeFeatures.moment(labelled_dots)[, 1:2],
                          computeFeatures.shape(labelled_dots)[, 5:6]))
df
#>        m.cx      m.cy s.radius.min s.radius.max
#> 1  65.73316  25.69588    11.095535     40.69698
#> 2 156.24181 129.77241    19.377341     33.83485
#> 3 483.60853 155.23006     9.419478     16.28808
#> 4 277.21467 409.62152    20.411710     28.77508
#> 5 397.36817 607.47749     8.424518     18.53617
#> 6 224.93790 623.28266     8.530353     15.26678

Now we want to find out which dimension matches which blob, so let's plot the raster in ggplot, and write the maximum pixel dimension above each blob.

img_df <- reshape2::melt(as.matrix(as.raster(as.array(dots))))

ggplot(img_df, aes(Var1, Var2, fill = value)) + 
  geom_raster() +
  scale_fill_identity() +
  scale_y_reverse() +
  geom_text(inherit.aes = FALSE, data = df, color = "white",
            aes(x = m.cx, y = m.cy, label = round(s.radius.max, 1))) +
  coord_equal()

If you would rather have the total number of pixels than the maximum diameter in pixels, you can also get this from computeFeatures



来源:https://stackoverflow.com/questions/63336483/how-to-obtain-size-of-cluster-of-pixels-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!