簡體   English   中英

Numpy PIL Python:在空格上裁剪圖像或使用直方圖閾值裁剪文本

[英]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))

在此處輸入圖片說明

相關文章/文檔:

更新:HYRY解決方案

在此處輸入圖片說明

我認為您可以在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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM