Python OpenCV并不总是识别captch中的矩形

2024-10-02 04:31:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这些CAPTCHA,想知道什么是最好的方法,以获得矩形边框内的部分。我试过了,我的代码可能每4个验证码中只有1个。请帮帮我,这件事我已经耽搁了一段时间了。你知道吗

Captcha Image

Captcha Image 2

    img = cv2.imread('captcha3.png')
    cropB = cv2.medianBlur(img, 3)
    gray = cv2.cvtColor(cropB, cv2.COLOR_BGR2GRAY)
    white_bg = 255*np.ones_like(img)

    ret, thresh = cv2.threshold(gray, 190, 60, 0)
    im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    for contour in contours:
        approx = cv2.approxPolyDP(contour, 0.01 * cv2.arcLength(contour, True), True)
        (x, y, w, h) = cv2.boundingRect(contour)
        roi = img[y:y + h, x:x + w]
        print(str(w) + " " + str(h))
        if w > 50 and w < 150:
            cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
            white_bg[y:y + h, x:x + w] = roi

    plt.figure("Captcha")
    plt.imshow(imutils.opencv2matplotlib(white_bg))
    plt.show()

另外,有时它出于某种原因决定使矩形小于矩形边框本身。就像字母重叠时的75%。你知道吗


Tags: imagetrueimgpltcv2captcha边框contour

热门问题