簡體   English   中英

使用PYTHON PIL從Captcha Image中刪除背景噪聲線

[英]Removing background noisy lines from Captcha Image using PYTHON PIL

我有一個處理過的驗證碼圖像(放大)看起來像:
驗證碼

如您所見,“TEXT”的字體大小比Noisy Lines的寬度略大。
所以我需要一個算法或代碼來從這個圖像中刪除嘈雜的線條。

在Python PIL庫和下面提到的斬波算法的幫助下,我得不到OCR可以輕松讀取的輸出圖像。

這是我嘗試過的Python代碼:

import PIL.Image
import 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.size
data = 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, 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, y + c] = 255

        # Skip this sequence we just altered.
        y += total

image.save(sys.argv[3])

因此,基本上我想知道一個更好的算法/代碼來擺脫噪音,從而能夠使圖像可以被OCR(Tesseract或pytesser)讀取。

要快速擺脫大多數線條,您可以將兩個或更少相鄰黑色像素的所有黑色像素變為白色。 這應該可以修復雜散線。 然后,當你有很多“塊”時,你可以刪除較小的塊。

這是假設樣本圖像已被放大,並且線僅為一個像素寬。

您可以使用自己的擴張和侵蝕功能,這將刪除最小的線條。 這里可以找到一個很好的實現。

我個人使用如上所述的擴張和侵蝕,但將其與寬度和高度的一些基本統計結合起來,嘗試找出異常值並根據需要消除這些線。 之后,在使用臨時圖像作為原始圖像之前,采用內核的最小值並轉換臨時圖像中的顏色的中心像素(迭代舊圖像)的過濾器應該起作用。 在pillow / PIL中,基於最小的任務是通過img.filter(ImageFilter.MINFILTER)完成的。

如果這還不夠,它應該產生一個可識別的集合,OpenCV的輪廓和最小邊界旋轉框可用於旋轉字母進行比較(我推薦Tesseract或商業OCR,因為他們有大量的字體和額外的集群和清理等功能)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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