因此,您要提取白色字符中的黑色区域。例如,您可以在直方图中选择值小于某个阈值的列(或行)。
from matplotlib import pyplot as plt
import pylab
import numpy as np
img = plt.imread('binary_image/iXWgw.png')
(rows,cols)=img.shape
h_projection = np.array([ x/rows for x in img.sum(axis=0)])
threshold = (np.max(h_projection) - np.min(h_projection)) / 4
print("we will use threshold {} for horizontal".format(threshold))
# select the black areas
black_areas = np.where(h_projection < threshold)
fig = plt.figure(figsize=(16,8))
fig.add_subplot(121)
for j in black_areas:
img[:, j] = 0
plt.plot((j, j), (0, 1), 'g-')
plt.plot(range(cols), h_projection.T)
v_projection = np.array([ x/cols for x in img.sum(axis=1)])
threshold = (np.max(v_projection) - np.min(v_projection)) / 4
print("we will use threshold {} for vertical".format(threshold))
black_areas = np.where(v_projection < threshold)
fig.add_subplot(122)
for j in black_areas:
img[j, :] = 0
plt.plot((0,1), (j,j), 'g-')
plt.plot(v_projection, range(rows))
plt.show()
# obscurate areas on the image
plt.figure(figsize=(16,12))
plt.subplot(211)
plt.title("Image with the projection mask")
plt.imshow(img)
# erode the features
import scipy
plt.subplot(212)
plt.title("Image after erosion (suggestion)")
eroded_img = scipy.ndimage.morphology.binary_erosion(img, structure=np.ones((5,5))).astype(img.dtype)
plt.imshow(eroded_img)
plt.show()
所以现在你有了水平和垂直投影,看起来像这样

之后你可以应用掩码:有几种方法可以做到这一点,代码已经应用在 for 循环中,我们在其中img[:,j] = 0为列和img[j,:] = 0行设置。这很简单,我认为很直观,但你可以寻找其他方法。作为一个建议,我想说你可以研究一下腐蚀的形态算子,它可以帮助分离白色部分。所以输出看起来像这样。

不幸的是,上部和下部仍然显示白色区域。您可以手动将行设置为 white img[:10,:] = 0, img[100:,:] = 0,但这可能不适用于您拥有的所有图像(如果您正在尝试训练神经网络,我假设您有很多,因此您需要有一个适用于所有图像的代码其中。
所以,既然你现在也要求分割,这就打开了另一个话题。分割是一项复杂的任务,它不像二进制掩码那么简单。我强烈建议你在不理解的情况下应用一些东西之前阅读一些关于这方面的材料。例如,这里有一个使用 scipy 进行图像处理的指南,但您可能会寻找更多。作为一个建议和一个使它工作的小片段,您可以使用来自scipy.ndimage. 这里有一小部分代码(来自指南)
label_im, nb_labels = scipy.ndimage.label(eroded_img)
plt.figure(figsize=(16,12))
plt.subplot(211)
plt.title("Segmentation")
plt.imshow(label_im)
plt.subplot(212)
plt.title("One Object as an example")
plt.imshow(label_im == 6) # change number for the others!
这将输出:

作为一个例子,我展示了这S封信。如果您更改label_im == 6,您将收到下一封信。如您所见,它并不总是正确的,图像的其他小块也被视为对象。所以你将不得不在这方面做更多的工作。