【问题标题】:-215:Assertion failed npoints >= 0 && (depth == CV_32F || depth == CV_32S) in function 'contourArea'-215:断言失败 npoints >= 0 && (depth == CV_32F || depth == CV_32S) in function 'contourArea'
【发布时间】:2020-06-16 12:34:14
【问题描述】:

我正在尝试来自https://github.com/longphungtuan94/ALPR_System 的代码,并且已经修复了 opencv 版本不匹配的大部分问题。但是一直无法修复这个错误:-

 line 61, in segment_characters_from_plate
    c = max(cnts, key=cv2.contourArea)
cv2.error: OpenCV(4.2.0) /io/opencv/modules/imgproc/src/shapedescr.cpp:315: error: (-215:Assertion failed) npoints >= 0 && (depth == CV_32F || depth == CV_32S) in function 'contourArea'

到目前为止我尝试了什么(但都没有奏效):-

OpenCV(4.0.0) assertion failed in function 'contourArea'

OpenCV Contours in Python: How to solve 'list index out of range'

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

代码如下 :--

import cv2
import numpy as np

import imutils
from skimage.filters import threshold_local
from skimage import measure


def sort_contours_left_to_right(character_contours):
    """
    Sort contours from left to right
    """
    i = 0
    boundingBoxes = [cv2.boundingRect(c) for c in character_contours]
    (character_contours, boundingBoxes) = zip(*sorted(zip(character_contours, boundingBoxes),
                                                key=lambda b:b[1][i], reverse=False))
    return character_contours


def segment_characters_from_plate(plate_img, fixed_width):
    """
    extract the Value component from the HSV color space and apply adaptive thresholding
    to reveal the characters on the license plate
    """
    V = cv2.split(cv2.cvtColor(plate_img, cv2.COLOR_BGR2HSV))[2]
    T = threshold_local(V, 29, offset=15, method='gaussian')
    thresh = (V > T).astype('uint8') * 255
    thresh = cv2.bitwise_not(thresh)

    # resize the license plate region to a canoncial size
    plate_img = imutils.resize(plate_img, width=fixed_width)
    thresh = imutils.resize(thresh, width=fixed_width)
    bgr_thresh = cv2.cvtColor(thresh, cv2.COLOR_GRAY2BGR)
    bgr_thresh = np.uint8(bgr_thresh)
    # perform a connected components analysis and initialize the mask to store the locations
    # of the character candidates
    labels = measure.label(thresh, neighbors=8, background=0)
    charCandidates = np.zeros(thresh.shape, dtype='uint8')

    # loof over the unique components
    characters = []
    for label in np.unique(labels):
        # if this is the background label, ignore it
        if label == 0:
            continue
        # otherwise, construct the label mask to display only connected components for the
        # current label, then find contours in the label mask
        labelMask = np.zeros(thresh.shape, dtype='uint8')
        labelMask[labels == label] = 255
        cnts = cv2.findContours(labelMask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        cnts = cnts[0] if imutils.is_cv2() else cnts[1]

        # ensure at least one contour was found in the mask
        if len(cnts) > 0:

            # grab the largest contour which corresponds to the component in the mask, then
            # grab the bounding box for the contour
            c = max(cnts, key=cv2.contourArea)
            (boxX, boxY, boxW, boxH) = cv2.boundingRect(c)

            # compute the aspect ratio, solodity, and height ration for the component
            aspectRatio = boxW / float(boxH)
            solidity = cv2.contourArea(c) / float(boxW * boxH)
            heightRatio = boxH / float(plate_img.shape[0])

            # determine if the aspect ratio, solidity, and height of the contour pass
            # the rules tests
            keepAspectRatio = aspectRatio < 1.0
            keepSolidity = solidity > 0.15
            keepHeight = heightRatio > 0.5 and heightRatio < 0.95

            # check to see if the component passes all the tests
            if keepAspectRatio and keepSolidity and keepHeight and boxW > 14:
                # compute the convex hull of the contour and draw it on the character
                # candidates mask
                hull = cv2.convexHull(c)
                cv2.drawContours(charCandidates, [hull], -1, 255, -1)

    _, contours, hier = cv2.findContours(charCandidates, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    if contours:
        contours = sort_contours_left_to_right(contours)
        characters = []

        addPixel = 4 # value to be added to each dimension of the character
        for c in contours:
            (x,y,w,h) = cv2.boundingRect(c)
            if y > addPixel:
                y = y - addPixel
            else:
                y = 0
            if x > addPixel:
                x = x - addPixel
            else:
                x = 0
            temp = bgr_thresh[y:y+h+(addPixel*2), x:x+w+(addPixel*2)]
            characters.append(temp)
        return characters
    else:
        return None

【问题讨论】:

  • @HansHirse 人它醒了!!!!请写下这个作为答案,我愿意接受它,以便您获得应得的分数。
  • 看看OpenCV version 4.1.0 drawContours。根据 OpenCV 版本,函数有不同的返回签名

标签: python opencv image-processing


【解决方案1】:

cv2.findContours 的方法签名随每个主要版本而变化。这是

contours, hierarchy = cv2.findContours(...)

对于OpenCV 2.x.x 以及对于OpenCV 4.x.x,但它是

image, contours, hierarchy = cv2.findContours(...)

OpenCV 3.x.x

现在,(第一个)问题是以下代码:

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

您只需检查,如果使用的版本是 OpenCV 2.x.x,那么 cnts[1] 也用于 OpenCV 4.x.x,它也应该是 cnts[0]。最好将 imutils 版本检查替换为:

cnts = cnts[0] if len(cnts) == 2 else cnts[1]

在进一步更改方法签名之前,这将适用于所有主要版本的 OpenCV。

请注意,这里还会出现另一个错误:

_, contours, hier = cv2.findContours(...)

这是特定于 OpenCV 3.x.x 的,因此最好也相应地更改该行。

希望有帮助!

【讨论】:

  • cnts = cnts[0] if len(cnts) == 2 else cnts[1] 工作正常。谢谢你。但我在这里有一点疑问.. len(cnts) == 2 => len 在 imutils 中还是我应该卸载 imutils 包?
猜你喜欢
  • 2022-10-17
  • 2019-11-11
  • 2021-04-05
  • 1970-01-01
  • 2019-07-11
  • 2019-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多