use*_*852 3 python image-processing python-imaging-library python-2.7
我有一个图像(*.png),其中包含两个文本块.我试图使用python27中的python映像库(PIL)单独获取每个文本块.
我试图模糊图像,然后找到模糊块的边缘,以便我可以恢复每个块的边界(稍后用于"裁剪").然而,当我模糊图像(我已经尝试了几次迭代)时,"find_edges"过滤器似乎只是抓住每个角色的边缘.
pic = Image.open("a.jpg")
out = pic.filter(ImageFilter.BLUR)
out = out.filter(ImageFilter.FIND_EDGES)
Run Code Online (Sandbox Code Playgroud)
我想我正在寻找类似于photoshop"磁性套索工具"的东西任何想法哪种方法可能更好?
Hoo*_*ked 12
我首先制作投影到一个轴上的图像的直方图.拍摄图像,先裁剪到外边框.投影直方图到y轴的示例:
from PIL import Image
import numpy as np
im = Image.open("dummytext.png")
pix = np.asarray(im)
pix = pix[:,:,0:3] # Drop the alpha channel
pix = 255 - pix # Invert the image
H = pix.sum(axis=2).sum(axis=1) # Sum the colors, then the y-axis
Run Code Online (Sandbox Code Playgroud)

从这里,确定最大的空白区块.这决定了要分割的最佳y坐标.注意在上面的直方图中它是如何显而易见的.如果两个文本块更接近,则需要更好的标准,只需调整方法以满足您的需求.分割后,您可以单独裁剪图像.