如您所见,“TEXT”的字体大小比Noisy lines的宽度略大.
所以我需要一个算法或代码来从这个图像中删除嘈杂的线条.
在Python PIL库和下面提到的斩波算法的帮助下,我得不到OCR可以轻松读取的输出图像.
这是我尝试过的Python代码:
import PIL.Imageimport sys# python chop.py [chop-factor] [in-file] [out-file]chop = int(sys.argv[1])image = PIL.Image.open(sys.argv[2]).convert('1')wIDth,height = image.sizedata = image.load()# Iterate through the rows.for y in range(height): for x in range(wIDth): # Make sure we're on a dark pixel. if data[x,y] > 128: continue # Keep a total of non-white contiguous pixels. total = 0 # Check a sequence ranging from x to image.wIDth. for c in range(x,wIDth): # If the pixel is dark,add it to the total. if data[c,y] < 128: total += 1 # If the pixel is light,stop the sequence. else: break # If the total is less than the chop,replace everything with white. if total <= chop: for c in range(total): data[x + c,y] = 255 # Skip this sequence we just altered. x += total# Iterate through the columns.for x in range(wIDth): for y in range(height): # Make sure we're on a dark pixel. if data[x,y] > 128: continue # Keep a total of non-white contiguous pixels. total = 0 # Check a sequence ranging from y to image.height. for c in range(y,height): # If the pixel is dark,add it to the total. if data[x,c] < 128: total += 1 # If the pixel is light,replace everything with white. if total <= chop: for c in range(total): data[x,y + c] = 255 # Skip this sequence we just altered. y += totalimage.save(sys.argv[3])
因此,基本上我想知道一个更好的算法/代码来摆脱噪音,从而能够使图像可以被OCR(Tesseract或PyTesser)读取.
解决方法 要快速摆脱大多数线条,您可以将两个或更少相邻黑色像素的所有黑色像素变为白色.这应该可以修复杂散线.然后,当你有很多“块”时,你可以删除较小的块.这是假设样本图像已被放大,并且线仅为一个像素宽.
总结以上是内存溢出为你收集整理的使用PYTHON PIL从Captcha Image中删除背景噪声线全部内容,希望文章能够帮你解决使用PYTHON PIL从Captcha Image中删除背景噪声线所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)