如何从 try_all_threshold 中自动获得 select 最佳结果?

How to automatically select best result from try_all_threshold?

我正在对基于文本数字的图像应用阈值处理。使用 skimage.filters.try_all_threshold 会导致应用 7 种阈值算法。我能够得到结果,但我在考虑如何只选择 1 个结果将结果传递给下一个 process/dynamically 选择 1 个最佳结果。

您需要定义原始图像和二值化图像之间的相似性度量,然后select最大化该度量的阈值方法。

演示

以下代码旨在让您走上正确的轨道。请注意,函数 similarity returns 是一个随机数,而不是一个合理的相似性度量。你应该自己实现它或者用适当的函数替换它。

import numpy as np
from skimage.data import text
import skimage.filters
import matplotlib.pyplot as plt

threshold_methods = [skimage.filters.threshold_otsu,
                     skimage.filters.threshold_yen,
                     skimage.filters.threshold_isodata,
                     skimage.filters.threshold_li,
                     skimage.filters.threshold_mean,
                     skimage.filters.threshold_minimum,
                     skimage.filters.threshold_mean,
                     skimage.filters.threshold_triangle,
                     ]

def similarity(img, threshold_method):
    """Similarity measure between the original image img and and the
    result of applying threshold_method to it.
    """
    return np.random.random()

results = np.asarray([similarity(text(), f) for f in threshold_methods])    
best_index = np.nonzero(results == results.min())[0][0]    
best_method = thresholding_methods[best_index]
threshold = best_method(text())
binary = text() >= threshold

fig, ax = plt.subplots(1, 1)
ax.imshow(binary, cmap=plt.cm.gray)
ax.axis('off')
ax.set_title(best_method.__name__)
plt.show(fig)

编辑

显然,随机选择阈值方法是无稽之谈(就像我在上面的玩具示例中所做的那样)。相反,您应该实施一种相似性度量,它允许您自动 select 最有效的算法。一种可能的方法是计算 错误分类错误 ,即背景像素错误分配给前景的百分比,相反,前景像素错误分配给背景的百分比。由于错误分类错误是一种相异性度量而不是相似性度量,因此您必须 select 最小化该度量的方法,如下所示:

best_index = np.nonzero(results == results.min())[0][0]

查看 this paper 以了解有关此方法和其他阈值性能评估方法的详细信息。