Im trying to develope a captcha solver, but I do not know how openCV works properly. Heres is my code, and the message error is below.
import os
import cv2
import numpy as np
archives = glob.glob('captcha_tratado/*')
for archive in archives:
image = cv2.imread(archive)
image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
_, image = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY_INV)
# find contour of each letter
contours = cv2.findContours(
image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
letter_regions = []
for contour in contours:
(x, y, width, height) = cv2.boundingRect(contour)
area = cv2.countourArea(contour)
if area > 115:
letter_regions.append((x, y, width, height))
if len(letter_regions) <= 2:
pass
# find contour and separate in individual files
image_final = cv2.merge([image] * 3)
i = 0
for rectangle in letter_regions:
x, y, width, height = rectangle
image_letter = image[y-2:y height 2, x-2:x width 2]
i = 1
fileName = os.path.basename(archive).replace(".png", f"letter{i}.png")
cv2.imwrite(f'letras/{fileName}', image_letter)
cv2.rectangle(image_final, (x-2, y-2),
(x width 2, y height 2), (0, 255, 0), 1)
fileName = os.path.basename(archive)
cv2.imwrite(f'identificado/{fileName}', image_final)```
ERROR MESSAGE:
(x, y, width, height) = cv2.boundingRect(contour)
cv2.error: OpenCV(4.6.0) :-1: error: (-5:Bad argument) in function 'boundingRect'
Overload resolution failed:
- array is not a numerical tuple
- Expected Ptr<cv::UMat> for argument 'array'
CodePudding user response:
cv2.findCountours
returns two things: The contour
list retrieved according to the contour approximation method and a list of contour hierarchy
. Expect two return values even if you don't use one, change the call to the function to this:
contours, _ = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
For further reference, here's the function documentation.