如何将TIFF文件转换为R中的二进制图像?

问题描述 投票:1回答:2

我有一个TIFF文件,我需要把它变成二进制图像。这可能在R?我应该如何编写设置阈值的参数?

r image-processing binary tiff
2个回答
1
投票

您可以使用imager库的threshold函数。从文档:

library(imager)
im <- load.image("cameraman.tif") # provide the correct image path 
im.g <- grayscale(im)
im.g %>% plot

enter image description here

threshold(im.g,"15%") %>% plot # the threshold will be set at 15th percentile 

enter image description here

threshold(im.g,"auto") %>% plot # a threshold will be computed automatically using kmeans

enter image description here

#If auto-threshold is too high, adjust downwards or upwards using "adjust"
threshold(im,adjust=1.3) %>% plot

enter image description here


0
投票

另一个选择是使用基于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()函数。

© www.soinside.com 2019 - 2024. All rights reserved.