Numpy PIL Python:在空白区域上裁剪图像或使用直方图阈值裁剪文本 [英] Numpy PIL Python : crop image on whitespace or crop text with histogram Thresholds

查看:141
本文介绍了Numpy PIL Python:在空白区域上裁剪图像或使用直方图阈值裁剪文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How would I go about finding the bounding box or window for the region of whitespace surrounding the numbers in the image below?:

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

Height: 762 pixels Width: 1014 pixels

类似:{x-bound:[x-upper,x-lower], y-bound:[y-upper,y-lower]},所以我可以裁剪文本并输入到tesseract或一些OCR中.

Something like: {x-bound:[x-upper,x-lower], y-bound:[y-upper,y-lower]} so I can crop to the text and input into tesseract or some OCR.

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

I had thought of slicing the image into hard coded chunk sizes and analysing at random, but i think it would be too slow.

使用pyplot的示例代码改编自(

Example code using pyplot adapted from (Using python and PIL how can I grab a block of text in an image?):

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

根据图表,每个山谷都将指示出一个绑定的地方.

Based on the graphs each of the valleys would indicate a place to bound.

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

相关帖子/文档:

  • Trim whitespace using PIL
  • Using python and PIL how can I grab a block of text in an image?
  • Use Python / PIL or similar to shrink whitespace
  • Crop the image using PIL in python
  • Rectangular bounding box around blobs in a monochrome image using python
  • How can I improve my paw detection?
  • http://scipy-lectures.github.io/advanced/image_processing/
  • http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.html

推荐答案

我认为您可以在scipy.ndimage中使用形态学功能,下面是一个示例:

I think you can use Morphology functions in scipy.ndimage, here is an example:

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")

输出为:

这篇关于Numpy PIL Python:在空白区域上裁剪图像或使用直方图阈值裁剪文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆