【问题标题】:OpenCV error: (-215) npoints > 0 in function cv::drawContoursOpenCV 错误:函数 cv::drawContours 中的 (-215) npoints > 0
【发布时间】:2017-08-20 07:22:47
【问题描述】:

这是我为进行手部检测而编写的整个代码,但不幸的是,当输入到 drawContours() 函数时,我被卡住了,因为凸面输出给出了错误。请帮忙! @attached 也是错误。

import cv2
import numpy as np
import math

key = 0
skin_lower = (0, 44, 44)
skin_upper = (28, 133, 128)
camera = cv2.VideoCapture(0)

print("Play something!")
while True:
    retval, img = camera.read()
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    mask = cv2.inRange(hsv, skin_lower, skin_upper)
    blurSize = 5
    elementSize = 5
    mask = cv2.medianBlur(mask, blurSize)
    element = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (11, 11), (5, 5))
    mask = cv2.dilate(mask, element)
    _, contours, hierarchy = cv2.findContours(mask.copy(), 
cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    largestContour = 0
    for i in range(1, len(contours)):
        if cv2.contourArea(contours[i]) > cv2.contourArea(contours[largestContour]):
            largestContour = i
    print largestContour
    cv2.drawContours(img, contours, largestContour, (0, 255, 0), 1)
    if len(contours) != 0:
        hull = cv2.convexHull(contours[largestContour], returnPoints=False)
        print hull
        cv2.drawContours(img, hull, 0, (255, 0, 0), 3) #error here

    cv2.imshow('mask', mask)
    cv2.imshow('image', img)

    k = cv2.waitKey(1)
    if k == 27:  # wait for ESC key to exit
        cv2.destroyAllWindows()
        break

输出:

Play something!
0
0
0
OpenCV Error: Assertion failed (npoints > 0) in cv::drawContours, file 
C:\projects\opencv-python\opencv\modules\imgproc\src\drawing.cpp, line 2481
Traceback (most recent call last):
File "F:/PYTHON/AIRKEY/main.py", line 42, in <module>
cv2.drawContours(img, hull, 0, (255, 0, 0), 3)
cv2.error: C:\projects\opencv-
python\opencv\modules\imgproc\src\drawing.cpp:2481: error: (-215) npoints > 
0 in function cv::drawContours

3
[[310]
[290]
[288]
[ 97]
[ 96]
[ 92]
[ 81]
[ 74]
[ 68]
[ 43]
[ 42]
[ 38]
[ 34]
[ 32]
[ 31]
[ 13]
[ 11]
[  0]
[464]
[458]
[402]
[389]
[387]
[360]
[359]
[353]
[349]
[347]
[345]
[311]]

【问题讨论】:

  • cv2.drawContours(img, contours, largestContour, (0, 255, 0), 1)放在if len(contours) != 0:里面
  • drawContours 是否正确处理 ConvexHull 输出作为输入?
  • 啊对...应该是:cv2.drawContours(img, [hull], 0, (255, 0, 0), 3)
  • 尝试了@Miki,但仍然是同样的错误!你可以尝试编译...

标签: python opencv numpy image-processing opencv-drawcontour


【解决方案1】:

这样做:

cv2.drawContours(img, [hull.astype(int)], 0, (255, 0, 0), 3)

【讨论】:

    猜你喜欢
    • 2016-06-24
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 2016-08-04
    • 2020-09-03
    • 2021-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多