我想删除形成一个大簇的像素,只保留小簇进行分析(意味着获得像素数量和位置)。首先,我应用滤镜将所有值小于0.66的像素涂成白色。然后我在R中使用函数clump()。模型可以工作,但我不能只删除大的集群。我不明白clump函数是如何工作的。
初始图像:
结果图像: plot_r是值小于0.66的像素变为0的图像。plot_rc是clump()函数之后的结果。正如所观察到的,我不能只删除大的像素集群(在图像plot_r的顶部)。我改变了值(代码中的700),但没有更好,怎么办?
代码如下:
library(magick)
library(pixmap)
library(raster)
library(igraph)
f <- https://ask.qcloudimg.com/http-save/yehe-900000/555d1c7cbc97a5a9a8a5df4f083e9d3f.jpeg
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)
发布于 2020-09-14 07:44:42
也许是这样的
library(raster)
library(igraph)
将你的方法简化一点
f <- "https://i.stack.imgur.com/2CjCh.jpg"
b <- brick(f)
x <- sum(b)
r <- x > 450
rc <- clump(r)
f <- freq(rc, useNA="no")
将聚块替换为其组成的像元数,然后将较大的像元(此处超过100个像元)设置为NA,并使用结果来掩膜原始栅格
rs <- subs(rc, data.frame(f))
rsc <- reclassify(rs, cbind(100,Inf,NA))
m <- mask(b, rsc)
plotRGB(m)
https://stackoverflow.com/questions/63878069
复制