OpenCV断言失败:(-215:断言失败)npoints> = 0 &&(深度== CV_32F ||深度== CV_32S)

纳粹克林别科夫:

我在该网站上找到了以下代码

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

贝里尔:

这是在做错事:

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]

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在函数'cv :: contourArea'中出现以下错误消息:错误:(-215:声明失败)npoints> = 0 &&(深度== CV_32F ||深度== CV_32S)

错误: (-215:Assertion failed) npoints >= 0 && (depth == CV_32F || depth == CV_32S) in function 'pointSetBoundingRect'

错误:(-215) count >= 0 && (depth == CV_32F || depth == CV_32S) in function arcLength

错误:(-215:Assertion failed) 函数 'cv::convexHull' 中的总数 >= 0 && (depth == CV_32F || depth == CV_32S)

OpenCV 错误:断言失败 ((scn == 3 || scn == 4) && (depth == CV_8U || depth == CV_32F)) 在 cvtColor 中

cv2.error : OPENCV(4.4.0) ERROR(-215 断言失败) size.height>0 && size,width>0)

错误:(-215:断言失败)使用OpenCV处理轮廓时,npoints> 0

函数'cv :: dnn :: ConvolutionLayerImpl :: getMemoryShapes'中的OpenCV深度学习面部检测断言错误

不支持的输入图像深度:“ VDepth :: contains(depth)”,其中“ depth”为4(CV_32S)

\ resize.cpp:3787:错误:(-215:断言失败)函数!= 0在函数'cv :: hal :: resize'中

cv2.error:函数'cv :: cvtColor'中的OpenCV(4.0.0)(-215:断言失败)!_src.empty()

在 OpenCV 中将灰度图像类型转换为 CV_32F 时,Mat 的 convertTo 函数会抱怨断言错误

OpenCV断言失败错误:(-215)scn == 3 || scn == 4在函数cv :: cvtColor中工作ALTERNATE次

错误:OpenCV(4.1.0)错误:(-215:断言失败)函数'cv :: resize'中的!ssize.empty()

错误:(-215:断言失败)bmi && width >= 0 && height >= 0 && (bpp == 8 || bpp == 24 || bpp == 32) in function 'FillBitmapInfo

深度克隆实践失败

OpenCV 3.1.0断言cv :: HOGDescriptor :: setSVMDetector中的checkDetectorSize失败

OpenCV错误:(-215:断言失败)(mtype == CV_8U || mtype == CV_8S)&& _mask.sameSize(* psrc1)在函数'cv :: binary_op'中

CV4.1:函数detectAndCompute级别中的断言失败>=0

(-215: 断言失败) !_src.empty() 函数 'cv::cvtColor'(PHYTON OPEN CV ERROR)

克隆在深度较大时失败

TestDisk在深度搜索中失败?

断言失败:颜色和深度缓冲区的kSurfaceUseResolvedBuffer标志不匹配

错误:(-215:断言失败)函数“cv::perspectiveTransform”中的 scn + 1 == m.cols

(-215:断言失败)函数<cv :: icvExtractPattern'中的数字<max_number错误?

OpenCV 不支持輸入圖像的深度,使用 cv2.cvtColor 時“深度”為 3 (CV_16S)

python:不支持图像的OpenCV深度(CV_64F)

OpenCV(4.0.0)Python错误:(-215:断言失败)(mtype == CV_8U || mtype == CV_8S)&& _mask.sameSize(* psrc1)在函数'cv :: binary_op'中

cv :: erode导致错误:OpenCV错误:断言失败(m.dims> = 2)在Mat中