<p>探测像这样的线是<a href="https://doi.org/10.1016/j.imavis.2006.07.021" rel="noreferrer">path opening</a>发明的目的。<a href="https://github.com/DIPlib/diplib" rel="noreferrer">PyDIP</a>有一个实现(披露:我在那里实现了它;还请注意,您必须从源代码安装PyDIP,因为我们还没有创建一个二进制发行版)。作为一种选择,你可以尝试使用我上面链接的论文的<a href="https://github.com/path-openings/morphological_path_operators" rel="noreferrer">the implementation by the authors</a>。该实现没有我下面使用的<a href="https://doi.org/10.1109/TIP.2010.2044959" rel="noreferrer">the "constrained" mode</a>。在</p>
<p>下面是一个如何使用它的快速演示:</p>
<pre><code>import PyDIP as dip
import matplotlib.pyplot as pp
img = 1 - pp.imread('/home/cris/tmp/DWRTF.png')
lines = dip.PathOpening(img, length=300, mode={'constrained'})
</code></pre>
<p>在这里,我们首先将图像反转,因为这样可以使以后的其他事情变得更简单。如果不反转,请使用闭合路径。<code>lines</code>图像:</p>
<p><a href="https://i.stack.imgur.com/s50VL.png" rel="noreferrer"><img src="https://i.stack.imgur.com/s50VL.png" alt="lines"/></a></p>
<p>接下来我们减去这些线。一个小区域开口将删除由路径开口过滤掉的行中的几个孤立像素:</p>
^{pr2}$
<p><a href="https://i.stack.imgur.com/ASWsl.png" rel="noreferrer"><img src="https://i.stack.imgur.com/ASWsl.png" alt="text"/></a></p>
<p>然而,我们现在在文本中做了一些空白。把这些填满可不是小事。下面是一个快速而肮脏的尝试,您可以将其作为起点:</p>
<pre><code>lines = lines > 0.5
text = text > 0.5
lines -= dip.BinaryPropagation(text, lines, connectivity=-1, iterations=3)
img[lines] = 0
</code></pre>
<p><a href="https://i.stack.imgur.com/kIQoH.png" rel="noreferrer"><img src="https://i.stack.imgur.com/kIQoH.png" alt="final result"/></a></p>