0

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
4

2 回答 2

0

问题很简单——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 ?

于 2018-02-13T09:09:40.533 回答
0

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

例如:im2、contours、hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

您正在迭代层次结构。不是轮廓。

有关更多信息,请参阅文档

于 2019-05-06T09:09:43.540 回答