我有一个TIFF文件,我需要把它变成二进制图像。这可能在R?我应该如何编写设置阈值的参数?
您可以使用imager
库的threshold
函数。从文档:
library(imager)
im <- load.image("cameraman.tif") # provide the correct image path
im.g <- grayscale(im)
im.g %>% plot
threshold(im.g,"15%") %>% plot # the threshold will be set at 15th percentile
threshold(im.g,"auto") %>% plot # a threshold will be computed automatically using kmeans
#If auto-threshold is too high, adjust downwards or upwards using "adjust"
threshold(im,adjust=1.3) %>% plot
另一个选择是使用基于ImageMagick库的magick
R包:
library(magick)
#> Linking to ImageMagick 6.9.7.4
#> Enabled features: fontconfig, freetype, fftw, lcms, pango, x11
#> Disabled features: cairo, ghostscript, rsvg, webp
logo <- image_read("logo:")
plot(logo)
logo_gray <- image_convert(logo, colorspace = "Gray")
plot(logo_gray)
logo_bw <- logo_gray %>%
image_threshold(type = "white", threshold = "50%") %>%
image_threshold(type = "black", threshold = "50%")
plot(logo_bw)
由reprex package创建于2019-03-20(v0.2.1)
或者,如果您想要局部自适应阈值处理,您应该查看image_lat()
函数。