OpenCV 3.4.0 视频 contours/arclength 个错误

OpenCV 3.4.0 video contours/arclength error(s)

OpenCV 版本:3.4.0(无法为其创建标签)

在尝试近似找到的轮廓时出现以下错误:

cv2.error: /io/opencv/modules/imgproc/src/shapedescr.cpp:237: error: (-215) count >= 0 && (depth == 5 || depth == 4) in function arcLength

错误是由行

引起的

eps=cv2.arcLength(cnt,True)

此外,如果我评论这部分代码

eps=cv2.arcLength(cnt,True)

approx = cv2.approxPolyDP(cnt,0.01*eps,True)

我收到以下错误

cv2.error: /io/opencv/modules/imgproc/src/drawing.cpp:2506: error: (-215) npoints > 0 in function drawContours

来自行:

cv2.drawContours(gray1,[cnt],0,(0,0,255, 1),3)

可能是(某种程度上)视频输入的通道先拆分后合并造成的?

我发现了类似的问题

gray1 = cv2.convertScaleAbs(gray1)

不幸的是,这不是我的情况。帮助将不胜感激。 :)

我提供以下代码:

import numpy as np
import cv2

cap = cv2.VideoCapture('video2.mp4')


while cap.isOpened():
    ret, frame = cap.read()

    frame = cv2.resize(frame, (640, 480), interpolation = cv2.INTER_LINEAR)
    gray1=frame # Is this right?

    frameArea = frame.shape[0]*frame.shape[1]

    # split the RGB image into R,G,B channels respectively
    b, g, r = frame[:, :, 0], frame[:, :, 1], frame[:, :, 2]

    # put back thresholded channels into one RGB image
    retvalb, b = cv2.threshold(b, 90, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
    retvalg,g = cv2.threshold(g, 0, 70, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
    retvalr,r = cv2.threshold(r, 0, 70, cv2.THRESH_BINARY+cv2.THRESH_OTSU)

    frame = cv2.merge((b,g,r))

    # create gray image in order to further threshold the result
    gray1 = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY);
    retvalgray, gray = cv2.threshold(gray1,0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU);

    gray1 = cv2.bilateralFilter(gray1, 11, 17, 17)
    gray1 = cv2.convertScaleAbs(gray1)
    # find the region of interest by drawing contours around it
    _, val,cnts = cv2.findContours(gray1, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) #gray / edged ?

    for cnt in cnts:
                eps=cv2.arcLength(cnt,True)
                approx = cv2.approxPolyDP(cnt,0.01*eps,True)
                cv2.drawContours(gray1,[cnt],0,(0,0,255, 1),3)

    cv2.imshow("Detection", gray1)
    if cv2.waitKey(1) & 0xFF is ord('q'):
            cv2.destroyAllWindows()
            print("Stop programm and close all windows")
            break

问题很简单 - findcontours 方法中的变量顺序错误

错误:

_ , val,cnts = cv2.findContours(gray1, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) #gray / edged ?

右:

_ , cnts, val = cv2.findContours(gray1, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) #gray / edged ?

findContour() 输出修改后的图像、轮廓和层次结构。 contours 是图像中所有轮廓的 Python 列表。每个单独的轮廓都是对象边界点的 (x,y) 坐标的 Numpy 数组。

例如: im2, 等高线, 层级 = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

您正在遍历层次结构。不是轮廓。

有关详细信息,请参阅 documentation