如何将验证码图像的所有字符与python中的一行对齐?

SIs*_*lam 0 python captcha opencv image-processing python-tesseract

我是图像处理新手。我需要将图像传递给 pytesseract 来获取图像的内容。在此之前,我需要以图像的所有字符与图像底部对齐的方式预处理图像,而 pytesseract 可以轻松检测到这些字符。

我使用 opencv-python,4.5.5 和 Python 3.8

我正在处理的图像看起来像-

1

更新: 我已经尝试过使用下面提到的代码:

import cv2
import numpy as np

img = cv2.imread(r"dialated.jpg", cv2.IMREAD_GRAYSCALE)
ret, img = cv2.threshold(img, 50, 255, cv2.THRESH_BINARY_INV)

Contours = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
Contours = sorted(Contours, key=lambda x : cv2.boundingRect(x)[0])
#Contours.sort(key=lambda x : cv2.boundingRect(x)[0]) #throws exception so commented out and used the above line instead.

newImg = np.zeros(img.shape, dtype=np.uint8)
bb = cv2.boundingRect(Contours[0])
newY = (bb[1] + bb[3])
for Contour in Contours:
    [x, y, w, h] = cv2.boundingRect(Contour)

    newImg[newY-h+1:newY+1, x:x+w] = img[y:y+h, x:x+w].copy()

cv2.imshow("img", img)
cv2.imshow("newImg", newImg)

cv2.waitKey(0)
cv2.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)

但我面临如下错误:

File "E:\Zone8\Shariful\Zone8\PrepaidMeters\captchaTest\removeBG\aligningContours.py", line 17, in <module>
  newImg[newY-h+1:newY+1, x:x+w] = img[y:y+h, x:x+w].copy()

builtins.ValueError: could not broadcast input array from shape (168,245) into shape (167,245)
Run Code Online (Sandbox Code Playgroud)

Rah*_*dia 5

方法很简单,只需读取图像,获取每个数字的轮廓,从左到右对轮廓进行排序,创建一个新图像,然后将旧图像中的数字复制粘贴到新图像上,保持 y- 相同左下角的坐标。下面是相同的代码:

import cv2
import numpy as np

img = cv2.imread("CapthaImg.png", cv2.IMREAD_GRAYSCALE)
ret, img = cv2.threshold(img, 50, 255, cv2.THRESH_BINARY_INV)

Contours = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
Contours.sort(key=lambda x : cv2.boundingRect(x)[0])

newImg = np.zeros(img.shape, dtype=np.uint8)
bb = cv2.boundingRect(Contours[0])
newY = (bb[1] + bb[3])
for Contour in Contours:
    [x, y, w, h] = cv2.boundingRect(Contour)

    newImg[newY-h+1:newY+1, x:x+w] = img[y:y+h, x:x+w].copy()

cv2.imshow("img", img)
cv2.imshow("newImg", newImg)

cv2.waitKey(0)
cv2.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)

下面是输入和输出图像: 请注意,我已经反转了输入图像以进行轮廓检测。

输入图像

输入图像

输出图像

输出图像