我想用这样的方式分割图像,每一个符号都被垂直分割,就像这个输入图像:
对此:
问题是每个符号可能有不同的宽度,所以我不能像我们在数组分裂中那样真正地修复分裂点。如果所有的对象都有相同的宽度,那么我可以根据宽度分割图像。在这个场景中,我应该使用什么逻辑来提取这些连接的对象?
发布于 2022-01-25 17:38:56
首先从url加载img。
import numpy as np
import urllib.request
from PIL import Image
from matplotlib import pyplot as plt
urllib.request.urlretrieve(
'https://i.stack.imgur.com/GRHzg.png',
"img.png")
img = Image.open("img.png")
img.show()

然后将黑色部分视为“填充”,并在numpy数组中进行转换。
arr = (np.array(img)[:,:,:-1].sum(axis=-1)==0)如果我们对每一列的行值进行求和,我们就可以得到每个列填充多少像素的简单和:
plt.subplot(211)
plt.imshow(arr, aspect="auto")
plt.subplot(212)
plt.plot(arr.sum(axis=0))
plt.xlim(0,arr.shape[1])

最后,如果我们计算这个和对列的微分,我们可以得到以下结果:
plt.subplot(211)
plt.imshow(arr, aspect="auto")
plt.subplot(212)
plt.plot(np.diff(arr.sum(axis=0)))
plt.xlim(0,arr.shape[1])

此时,您只需选择一个阈值并剪切图像:
threshold = 25
cut = np.abs(np.diff(arr.sum(axis=0)))>threshold
x_lines = np.arange(len(cut))[cut]
plt.imshow(arr, aspect="auto")
plt.vlines(x_lines, 0, arr.shape[0], color="r")

这是我的解决方案,它工作得很好,但它对所选的阈值和列梯度很敏感。我希望这是有用的。
https://stackoverflow.com/questions/70852674
复制相似问题