如何在R中使用丛集功能移除像素丛集

问题描述

我想删除形成大簇的像素,只保留小簇进行分析(即获取像素数和位置)。首先,我将所有值小于0.66的像素应用滤镜以白色着色。然后,我在R中使用了函数clump()。该模型可以工作,但不能仅删除大型群集。我不明白丛集函数的工作原理。

初始图片

enter image description here

结果图像:plot_r是将值

enter image description here

代码在这里

library(magick)
library(pixmap)
library(raster)
library(igraph)

f <- "https://i.stack.imgur.com/2CjCh.jpg"
x <- image_read(f)
x <- image_convert(x,format = "pgm",depth = 8)

# Save the pgm file
f <- tempfile(fileext = ".pgm")
image_write(x,path = f,format = "pgm")

# Read in the pgm file
picture <- read.pnm(file = f,cellres = 1)
str(picture)
picture@size
mat <- picture@grey
mat[mat<0.66] <- 0; x

##############################################################
##Remove clumps of pixels in R using package Raster and igraph
#Detect clumps (patches) of connected cells
r <-raster(mat)
rc <- clump(r) 

#extract IDs of clumps according to some criteria
clump9 = data.frame(freq(rc))
#remove clump observations with frequency smaller/larger than N
clump9 = clump9[ ! clump9$count > 700,]
# record IDs from clumps which met the criteria in prevIoUs step
clump9 = as.vector(clump9$value) 
#replace cells with IDs which do not belong to the group of interest 
rc[rc != clump9[1] & rc != clump9[2]] = NA 

# converting rasterlayer to matrix
n <- as.matrix(r)
m <- as.matrix(rc)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)