我尝试首先通过阈值处理来删除验证码数字,然后对其进行侵蚀,以获得细长的连续线以获得更好的输出。
问题:如您所见,腐蚀的图像不连续。
原图:
阈值图像:(这里数字区域太厚,所以我希望它缩小、细长和连续):
我的输出:
所需输出:
代码:
import os
import os.path
import cv2
import glob
import imutils
import matplotlib.pyplot as plt
import numpy as np
CAPTCHA_IMAGE_FOLDER = "generated_captcha_images"
OUTPUT_FOLDER = "extracted_letter_images"
# Get a list of all the captcha images we need to process
captcha_image_files = glob.glob(os.path.join(CAPTCHA_IMAGE_FOLDER, "*"))
counts = {}
# loop over the image paths
for (i, captcha_image_file) in enumerate(captcha_image_files):
print("[INFO] processing image {}/{}".format(i + 1, len(captcha_image_files)))
filename = os.path.basename(captcha_image_file)
captcha_correct_text = os.path.splitext(filename)[0]
image = cv2.imread(captcha_image_file)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(
gray, 36, 255, cv2.THRESH_BINARY_INV)[1]
erode = cv2.erode(thresh, np.ones((2, 2), np.uint8), iterations=1)
plt.imshow(erode, cmap="gray")
plt.show()
按照你的情况
skeleton
操作会更好
import cv2
import numpy as np
img = cv2.imread('/Users/alex/Downloads/fOrmgm.jpeg',0)
thinned = cv2.ximgproc.thinning(img)
element = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
thinned = cv2.dilate(thinned, element) # this operation is optional
cv2.imshow("skeleton", thinned)
cv2.waitKey()
更新
您可以使用变形操作在 python 中find实现骨架。对我来说结果并不好。也许你可以过滤这个天线。
cv2.ximgproc.thinning()
的实现您可以在here找到。