如何获得R中像素簇的大小 [英] How to obtain size of cluster of pixels in R

查看:47
本文介绍了如何获得R中像素簇的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2种颜色的图片.红色像素呈簇状.我想知道每个群集的最大尺寸,以与可接受的公差进行比较.怎么做?有执行它的功能吗?

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?

推荐答案

对于这种图像分析,您可以签出 EBImage :

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")

现在,我们可以使用 computeFeatures 函数获取每个簇的质心和最大直径:

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

现在,我们要找出与哪个斑点匹配的尺寸,因此让我们在ggplot中绘制栅格,并在每个斑点上方写入最大像素尺寸.

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()

如果您希望的是像素总数而不是最大直径(以像素为单位),也可以从 computeFeatures

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

这篇关于如何获得R中像素簇的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆