15

我在这个网站上找到了以下代码:

import os
import os.path
import cv2
import glob
import imutils
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)))

    # Since the filename contains the captcha text (i.e. "2A2X.png" has the text "2A2X"),
    # grab the base filename as the text
    filename = os.path.basename(captcha_image_file)
    captcha_correct_text = os.path.splitext(filename)[0]

    # Load the image and convert it to grayscale
    image = cv2.imread(captcha_image_file)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    # Add some extra padding around the image
    gray = cv2.copyMakeBorder(gray, 8, 8, 8, 8, cv2.BORDER_REPLICATE)

    # threshold the image (convert it to pure black and white)
    thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]

    # find the contours (continuous blobs of pixels) the image
    contours = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    # Hack for compatibility with different OpenCV versions
    contours = contours[0] if imutils.is_cv2() else contours[1]

    letter_image_regions = []

    # Now we can loop through each of the four contours and extract the letter
    # inside of each one
    for contour in contours:
        # Get the rectangle that contains the contour
        (x, y, w, h) = cv2.boundingRect(contour)

        # Compare the width and height of the contour to detect letters that
        # are conjoined into one chunk
        if w / h > 1.25:
            # This contour is too wide to be a single letter!
            # Split it in half into two letter regions!
            half_width = int(w / 2)
            letter_image_regions.append((x, y, half_width, h))
            letter_image_regions.append((x + half_width, y, half_width, h))
        else:
            # This is a normal letter by itself
            letter_image_regions.append((x, y, w, h))

    # If we found more or less than 4 letters in the captcha, our letter extraction
    # didn't work correcly. Skip the image instead of saving bad training data!
    if len(letter_image_regions) != 4:
        continue

    # Sort the detected letter images based on the x coordinate to make sure
    # we are processing them from left-to-right so we match the right image
    # with the right letter
    letter_image_regions = sorted(letter_image_regions, key=lambda x: x[0])

    # Save out each letter as a single image
    for letter_bounding_box, letter_text in zip(letter_image_regions, captcha_correct_text):
        # Grab the coordinates of the letter in the image
        x, y, w, h = letter_bounding_box

        # Extract the letter from the original image with a 2-pixel margin around the edge
        letter_image = gray[y - 2:y + h + 2, x - 2:x + w + 2]

        # Get the folder to save the image in
        save_path = os.path.join(OUTPUT_FOLDER, letter_text)

        # if the output directory does not exist, create it
        if not os.path.exists(save_path):
            os.makedirs(save_path)

        # write the letter image to a file
        count = counts.get(letter_text, 1)
        p = os.path.join(save_path, "{}.png".format(str(count).zfill(6)))
        cv2.imwrite(p, letter_image)

        # increment the count for the current key
        counts[letter_text] = count + 1

当我尝试运行代码时,出现以下错误:

[INFO] processing image 1/9955
Traceback (most recent call last):
  File "extract_single_letters_from_captchas.py", line 47, in <module>
    (x, y, w, h) = cv2.boundingRect(contour)
cv2.error: OpenCV(4.0.0) /Users/travis/build/skvark/opencv-python/opencv/modules/imgproc/src/shapedescr.cpp:741: error: (-215:Assertion failed) npoints >= 0 && (depth == CV_32F || depth == CV_32S) in function 'pointSetBoundingRect'

我尝试在 StackOverflow 上搜索解决方案,但没有找到任何类似的东西。


编辑(见评论):

  • type(contour[0])=<class 'numpy.ndarray'>

  • len(contour)=4

4

7 回答 7

27

这是做错事:

contours = contours[0] if imutils.is_cv2() else contours[1]

imutils.is_cv2()正在返回False,即使它应该返回True。如果您不介意删除此依赖项,请更改为:

contours = contours[0]

我找到了原因。您所关注的教程可能是在 OpenCV 4 发布之前发布的。OpenCV 3 更改cv2.findContours(...)为 return image, contours, hierarchy,而OpenCV 2cv2.findContours(...)OpenCV 4 的cv2.findContours(...)return contours, hierarchy。因此,在 OpenCV 4 之前,说如果你使用 OpenCV 2 应该是contours[0]else是正确的contours[1]。如果你还想拥有这个“兼容性”,可以改成:

contours = contours[1] if imutils.is_cv3() else contours[0]
于 2019-02-17T15:27:27.527 回答
4

在 OpenCV4 中,cv2.findContours 只有 2 个返回值。 轮廓是第一个值

contours, _ = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

请注意,我添加了下划线以放弃层次结构的其他返回值

于 2020-04-17T18:32:54.537 回答
3
 (x, y, w, h) = cv2.boundingRect(contour.astype(np.int))
于 2020-09-16T11:55:29.510 回答
1

这是因为 opencv-python 版本 4.0.0。如果您想在不更改代码的情况下解决此问题,请将 opencv-python 降级到版本 3.4.9.31

  • 卸载 opencv-python

    pip 卸载 opencv-python

  • 安装opencv-python==3.4.9.31

    pip install opencv-python==3.4.9.31

如果遇到函数“pointSetBoundingRect”的问题,您需要安装“opencv-python-headless”

pip install opencv-python-headless==3.4.9.31
于 2020-02-21T07:44:22.223 回答
1

我用以下方式编写了相同的代码:

_, contours, hierarchy = cv2.findContours(img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)

我的代码有效。我认为以前它返回 2 个变量,现在我们必须解压缩成三个变量。如果这不起作用,请尝试以下操作:

_, contours, _ = cv2.findContours(img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)

这应该有效。

有关更多信息,您可以访问 OpenCV 文档页面:https ://docs.opencv.org/3.1.0/d4/d73/tutorial_py_contours_begin.html

我希望这能帮到您。

于 2020-05-23T13:00:51.293 回答
0

原因在于 findContours()。

在 OpenCV 版本 3 中,我们写道:

_, contours, _ = cv.findContours()

在 OpenCV 版本 4 中,我们更改为:

contours, _ = cv.findContours()

使用任一解决问题的方法。

或者,我们可以使用这些命令稳定我们的 OpenCV 版本,假设您已经anaconda安装。

conda install -c conda-forge opencv=4.1.0 

pip install opencv-contrib-python  
于 2021-02-27T04:22:14.917 回答
-1

【OpenCV 3 将 cv2.findContours(...) 改为返回图像、轮廓、层次结构】这个内容对我很有帮助。我在前面添加了一个新变量并修复了所有错误..

于 2020-01-08T06:35:41.517 回答