OpenCV 3.4.0视频轮廓/ arclength错误

时间:2018-02-09 22:23:08

标签: python opencv opencv3.3

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

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

  

cv2.error:/io/opencv/modules/imgproc/src/shapedescr.cpp:237:错误:   (-215)计数> = 0&& (深度== 5 ||深度== 4)函数arcLength

错误是由行

引起的
  

EPS = cv2.arcLength(CNT,真)

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

  

EPS = cv2.arcLength(CNT,真)

     

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

我收到以下错误

  

cv2.error:/io/opencv/modules/imgproc/src/drawing.cpp:2506:错误:   (-215)npoints>函数drawContours中的0

来自行:

  

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

2 个答案:

答案 0 :(得分:0)

问题非常简单 - findcountours方法中的变量顺序错误

WRONG:

  

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

RIGHT:

  

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

答案 1 :(得分:0)

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

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

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

有关更多信息,请参见documentation