【问题标题】:Extracting foreground image as mask by thresholding通过阈值提取前景图像作为掩码
【发布时间】:2015-04-02 02:03:35
【问题描述】:

我正在寻找一种可靠的方法来从背景中有一些噪点的图像中提取前景。

所以,我想用它的图像是:

我的尝试是使用Otsu thresholding。我在 Python 中这样做如下:

from skimage.filter import threshold_otsu
import os.path as path
import matplotlib.pyplot as plt

img = io.imread(path.expanduser('~/Desktop/62.jpg'))
r_t = threshold_otsu(img[:, :, 0])
g_t = threshold_otsu(img[:, :, 1])
b_t = threshold_otsu(img[:, :, 2])

m = np.zeros((img.shape[0], img.shape[1]), dtype=np.uint8)
mask = (img[:, :, 0] < r_t) & (img[:, :, 1] < g_t) & (img[:, :, 2] < b_t)
m[~mask] = 255

plt.imshow(m)
plt.show()

这给出了 R、G、B 阈值为 (62 67 64),这有点高。结果是:

这张图片也是Otsu thresholding 效果最好的图片之一。如果我使用像 30 这样的手动阈值,它工作得很好。结果是:

我想知道我是否应该尝试其他一些方法。细分确实不是我的专业领域,而且我能开箱即用的东西似乎有限。

【问题讨论】:

    标签: python image-processing image-segmentation scikit-image


    【解决方案1】:

    您的图像看起来不是很丰富多彩。所以你可以对灰度值进行分割,而不是分别对每种颜色进行分割,然后组合三个蒙版。

    查看包scikit-image.filter还有其他几种阈值方法。我尝试了所有这些,发现threshold_isodata 表现非常好,提供与您想要的图像几乎相同的图像。所以我推荐isodata算法。

    例子:

    import numpy as np
    import skimage.io as io
    import skimage.filter as filter
    import matplotlib.pyplot as plt
    
    img = io.imread('62.jpg')
    gray = np.sum(img, axis=2) # summed up over red, green, blue
    #threshold = filter.threshold_otsu(gray) # delivers very high threshold
    threshold = filter.threshold_isodata(gray) # works extremely well
    #threshold = filter.threshold_yen(gray) # delivers even higher threshold
    print(threshold)
    
    plt.imshow(gray > threshold)
    plt.show()
    

    给予:

    【讨论】:

    • 这很好。我将尝试使用其他一些图像,看看它有多强大。
    • @Luca 如果您有其他图像并且有些图像不起作用,请选择最差的三个左右并再次询问(我希望在一个新问题中)。
    • 谢谢万亿。你真是太好了。我将会。我觉得我现在可以让它工作了
    猜你喜欢
    • 2016-01-07
    • 2019-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多