是否有可用于R栅格对象的自动阈值算法,例如“ Otsu”。我尝试使用“ authothresholder”包,但是它效率低下,因为它适用于矩阵并且不适用于32位tif文件。我正在尝试将NDWI图像转换为二进制层。
答案 0 :(得分:3)
这在Bioconductor的EBImage
软件包中实现。这是一个示例用法:
library(EBImage)
img <- readImage(system.file("images", "sample.png", package = "EBImage"))
thr <- img > otsu(img)
display(img)
display(thr)
本质上是以下实现(从EBImage::otsu
的函数定义中提取,即不是我的工作),因此您应该能够将以下内容用于所使用的任何图像分析工具集:
img # assuming img is a numeric matrix or vector
range = c(0, 1) # assuming values in the matrix range from 0 to 1
levels = 256L
breaks = seq(range[1], range[2], length.out = levels + 1)
h = hist.default(img, breaks = breaks, plot = FALSE)
counts = as.double(h$counts)
mids = as.double(h$mids)
len = length(counts)
w1 = cumsum(counts)
w2 = w1[len] + counts - w1
cm = counts * mids
m1 = cumsum(cm)
m2 = m1[len] + cm - m1
var = w1 * w2 * (m2/w2 - m1/w1)^2
maxi = which(var == max(var, na.rm = TRUE))
(mids[maxi[1]] + mids[maxi[length(maxi)]])/2