【问题标题】:OpenCV 3.4.0 video contours/arclength error(s)OpenCV 3.4.0 视频轮廓/弧长错误
【发布时间】:2018-07-20 18:20:06
【问题描述】:

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

在尝试近似找到的轮廓时,我遇到了以下错误:

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

错误是由线路引起的

eps=cv2.arcLength(cnt,True)

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

eps=cv2.arcLength(cnt,True)

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

我收到以下错误

cv2.error: /io/opencv/modules/imgproc/src/drawing.cpp:2506: 错误: (-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

【问题讨论】:

    标签: python opencv opencv3.3


    【解决方案1】:

    问题很简单——findcountours 方法中的变量顺序错误

    错误:

    _ , 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 ?

    【讨论】:

      【解决方案2】:

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

      例如: im2, 轮廓, 层次结构 = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

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

      请参阅documentation 了解更多信息。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-03-20
        • 1970-01-01
        • 2019-07-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多