函数'contourArea'中的OpenCV(4.0.0)断言失败

Ajinkya

我的目的是使用HOG描述符识别汽车徽标。我正在跟踪链接的教程https://gurus.pyimagesearch.com/lesson-sample-histogram-of-iented-gradients-and-car-logo-recognition/#我在单独的文件夹中测试和训练图像。

使用以下代码提取HOG功能时:

# import the necessary packages
from sklearn.neighbors import KNeighborsClassifier
from skimage import exposure
from skimage import feature
from imutils import paths
import argparse
import imutils
import cv2

# construct the argument parse and parse command line arguments
ap = argparse.ArgumentParser()
ap.add_argument("-d", "--training", required=True, help="Path to the logos training dataset")
ap.add_argument("-t", "--test", required=True, help="Path to the test dataset")
args = vars(ap.parse_args())

# initialize the data matrix and labels
print('[INFO] extracting features...')
data = []
labels = []


# loop over the image paths in the training set
for imagePath in paths.list_images(args["training"]):
    # extract the make of the car
    make = imagePath.split("/")[-2]

    # load the image, convert it to grayscale, and detect edges
    image = cv2.imread(imagePath)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    edged = imutils.auto_canny(gray)

    # find contours in the edge map, keeping only the largest one which
    # is presmumed to be the car logo
    cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if imutils.is_cv2() else cnts[1]
    c = max(cnts, key=cv2.contourArea)

    # extract the logo of the car and resize it to a canonical width
    # and height
    (x, y, w, h) = cv2.boundingRect(c)
    logo = gray[y:y + h, x:x + w]
    logo = cv2.resize(logo, (200, 100))

    # extract Histogram of Oriented Gradients from the logo
    H = feature.hog(logo, orientations=9, pixels_per_cell=(10, 10),
        cells_per_block=(2, 2), transform_sqrt=True, block_norm="L1")

    # update the data and labels
    data.append(H)
    labels.append(make)

我遇到此错误:

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sklearn/externals/joblib/externals/cloudpickle/cloudpickle.py:47: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
  import imp
[INFO] extracting features...
Traceback (most recent call last):
  File "hog.py", line 36, in <module>
    c = max(cnts, key=cv2.contourArea)
cv2.error: OpenCV(4.0.0) /Users/travis/build/skvark/opencv-python/opencv/modules/imgproc/src/shapedescr.cpp:272: error: (-215:Assertion failed) npoints >= 0 && (depth == CV_32F || depth == CV_32S) in function 'contourArea'

如何清除此错误?

爵士乐

在找到边缘图像之前,请将其转换为uint8类型:

edged = np.uint8(edged)
cnts, _ = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

我已经编辑了您的代码。我在系统上检查了它,它工作正常。尝试这个:

# import the necessary packages
from sklearn.neighbors import KNeighborsClassifier
from skimage import exposure
from skimage import feature
from imutils import paths
import argparse
import imutils
import cv2
# construct the argument parse and parse command line arguments
ap = argparse.ArgumentParser()
ap.add_argument("-d", "--training", required=True, help="Path to the logos training dataset")
ap.add_argument("-t", "--test", required=True, help="Path to the test dataset")
args = vars(ap.parse_args())

# initialize the data matrix and labels
print('[INFO] extracting features...')
data = []
labels = []


# loop over the image paths in the training set
for imagePath in paths.list_images(args["training"]):
    # extract the make of the car
    make = imagePath.split("/")[-2]

    # load the image, convert it to grayscale, and detect edges
    image = cv2.imread(imagePath)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    edged = imutils.auto_canny(gray)

    # find contours in the edge map, keeping only the largest one which
    # is presmumed to be the car logo
    edged = np.uint8(edged)
    cnts, _ =  = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    if cnts is not None:
        cnts = cnts[0] if imutils.is_cv2() else cnts[1]
        c = max(cnts, key=cv2.contourArea)

        # extract the logo of the car and resize it to a canonical width
        # and height
        (x, y, w, h) = cv2.boundingRect(c)
        logo = gray[y:y + h, x:x + w]
        logo = cv2.resize(logo, (200, 100))

        # extract Histogram of Oriented Gradients from the logo
        H = feature.hog(logo, orientations=9, pixels_per_cell=(10, 10),
            cells_per_block=(2, 2), transform_sqrt=True, block_norm="L1")

        # update the data and labels
        data.append(H)
        labels.append(make)

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

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

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

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

OpenCV错误:断言失败(size.width> 0 && size.height> 0)简单代码

OpenCV错误:断言失败(size.width> 0 && size.height> 0)python

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

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

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

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

断言和失败显示为 0

暗网重量训练'断言'0'失败'

断言失败(size.width> 0 && size.height> 0)

0 不是 Scheme 中的函数

从C中的函数返回{0}?

OpenCV调整大小在具有“错误:(-215)ssize.area()> 0在函数cv :: resize中的错误”的大图像上失败

为什么断言后此Solidity函数返回0?

Python-OpenCV cv2 OpenCV错误:在未知函数,文件.. \ .. \ .. \ modules \ imgproc \ src \ color.cpp中,断言失败(scn == 3 || scn == 4)

与openCV 3中contourArea的兼容性问题

我遇到此错误的麻烦(-215:断言失败)!opencv中的函数'resize'中的!ssize.empty()

对模拟函数的断言失败

对函数指针的断言失败

调整图像大小时出错:“错误:(-215:声明失败)函数'resize'中的func!= 0”

-O0处的内联函数导致clang中的链接失败

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

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

加载图像在函数cv :: inRange中返回断言失败

访问DLL中的函数时出现调试断言失败错误

Spring Boot中构造函数的参数0

BigDecimal加法在函数中给出0