[英]Numpy PIL Python : crop image on whitespace or crop text with histogram Thresholds
我如何找到下圖中圍繞數字的空白區域的邊界框或窗口?
高度:762像素寬度:1014像素
像這樣的東西: {x-bound:[x-upper,x-lower], y-bound:[y-upper,y-lower]}
因此我可以裁剪文本並輸入到tesseract或一些OCR中。
我曾考慮過將圖像切成硬編碼的塊大小並隨機分析,但是我認為這太慢了。
使用pyplot
示例代碼改編自( 使用python和PIL,如何在圖像中獲取文本塊? ):
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])
基於這些圖,每個山谷將指示出一個綁定的地方。
plt.plot(p.sum(axis=1))
: plt.plot(p.sum(axis=0))
: 相關文章/文檔:
我認為您可以在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")
輸出為:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.