gpt4 book ai didi

python - Numpy PIL Python : crop image on whitespace or crop text with histogram Thresholds

转载 作者:行者123 更新时间:2023-11-28 21:54:07 28 4
gpt4 key购买 nike

我将如何找到下图中数字周围空白区域的边界框或窗口?:

原图:

enter image description here

高度:762 像素宽度:1014 像素

目标:

类似于:{x-bound:[x-upper,x-lower], y-bound:[y-upper,y-lower]} 这样我就可以裁剪到文本和输入到 tesseract 或某些 OCR。

尝试:

我曾想过将图像切成硬编码的 block 大小并随机分析,但我认为这太慢了。

使用改编自 ( Using python and PIL how can I grab a block of text in an image? ) 的 pyplot 的示例代码:

from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
im = Image.open('/home/jmunsch/Pictures/Aet62.png')
p = np.array(im)
p = p[:,:,0:3]
p = 255 - p
lx,ly,lz = p.shape

plt.plot(p.sum(axis=1))
plt.plot(p.sum(axis=0))

#I was thinking something like this
#The image is a 3-dimensional ndarray [[x],[y],[color?]]
#Set each value below an axes mean to 0
[item = 0 for item in p[axis=0] if item < p.mean(axis=0)]

# and then some type of enumerated groupby for each axes
#finding the mean index for each groupby(0) on axes

plt.plot(p[mean_index1:mean_index2,mean_index3:mean_index4])

根据图表,每个山谷都表示一个要绑定(bind)的地方。

  • 第一个图表显示了文本行的位置
  • 第二张图显示了字符所在的位置

绘图示例plt.plot(p.sum(axis=1)):

enter image description here

绘图示例输出 plt.plot(p.sum(axis=0)):

enter image description here

相关文章/文档:

更新:HYRY 的解决方案

enter image description here

最佳答案

我认为你可以在 scipy.ndimage 中使用形态学函数,这里是一个例子:

import pylab as pl
import numpy as np
from scipy import ndimage
img = pl.imread("Aet62.png")[:, :, 0].astype(np.uint8)
img2 = ndimage.binary_erosion(img, iterations=40)
img3 = ndimage.binary_dilation(img2, iterations=40)
labels, n = ndimage.label(img3)
counts = np.bincount(labels.ravel())
counts[0] = 0
img4 = labels==np.argmax(counts)
img5 = ndimage.binary_fill_holes(img4)
result = ~img & img5
result = ndimage.binary_erosion(result, iterations=3)
result = ndimage.binary_dilation(result, iterations=3)
pl.imshow(result, cmap="gray")

输出是:

enter image description here

关于python - Numpy PIL Python : crop image on whitespace or crop text with histogram Thresholds,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24687760/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com