擅长:python、mysql、java
<p>我带着建议从未来回来。在</p>
<p>上述方法对于一个简单的全局阈值非常有效。
我发布这个答案是为了警告非自适应阈值,像这样,可能太天真了,这取决于您的应用程序。在</p>
<p>如果不适应图像的平均亮度或其他质量,如果要分析在不同条件下拍摄的多张照片,输出将不一致。在</p>
<p>有更精确的方法来解决这个问题,但它们稍微复杂一点。Scikit图像使这一切变得简单。在</p>
<p>最流行的方法之一是大津的(我不能说哪种方法对每种情况都是最准确的,我还没有进行足够的研究)。
<a href="https://en.wikipedia.org/wiki/Otsu%27s_method" rel="nofollow">https://en.wikipedia.org/wiki/Otsu%27s_method</a></p>
<p>Scikit Image在它们的模块中内置了这个和其他一些算法。在</p>
<p>使用这种方法,对上述问题的答案很简单:</p>
<pre><code>import matplotlib.pyplot as plt
from skimage.filters import threshold_otsu
thresh = threshold_otsu(Image)
binary = Image > thresh
plt.imshow(Image, cmap=plt.cm.gray)
plt.show()
</code></pre>
<p>请阅读以下示例:
<a href="http://scikit-image.org/docs/dev/auto_examples/plot_otsu.html" rel="nofollow">http://scikit-image.org/docs/dev/auto_examples/plot_otsu.html</a></p>
<p>关于这里的用法:
<a href="http://scikit-image.org/docs/dev/api/skimage.filters.html?highlight=local%20otsu" rel="nofollow">http://scikit-image.org/docs/dev/api/skimage.filters.html?highlight=local%20otsu</a></p>