使用findcontours时出错

时间:2018-03-01 04:02:24

标签: python opencv

这是我的代码

import numpy as np
import cv2

cv2.ocl.setUseOpenCL(False)

cap = cv2.VideoCapture('counter4.avi') #Open video file

fgbg = cv2.createBackgroundSubtractorMOG2(detectShadows = True) #Create the background substractor
ret, frame = cap.read() #read a frame
fgmask = fgbg.apply(frame) #Use the substractor
kernelOp = np.ones((3,3),np.uint8)
kernelCl = np.ones((11,11),np.uint8)

while(cap.isOpened()):
    ret, frame = cap.read() #read a frame
    fgmask = 0.1*fgmask + 0.9*fgbg.apply(frame) #Use the substractor
    mask = cv2.morphologyEx(fgmask, cv2.MORPH_OPEN, kernelOp)
    mask = cv2.morphologyEx(mask , cv2.MORPH_CLOSE, kernelCl)

    _, contours0, hierarchy = cv2.findContours(mask,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    #cv2.drawContours(frame, cnt, -1, (0,255,0), 3)

    cv2.imshow('Frame',frame)
    cv2.imshow('Background',fgmask)
    cv2.imshow('Masking',mask)

    #Abort and exit with 'Q' or ESC
    k = cv2.waitKey(30) & 0xff
    if k == 27:
        break

cap.release() #release video file
cv2.destroyAllWindows() #close all openCV windows

然后我发现这样的错误

Traceback (most recent call last):
  File "D:\Visitor Counter\femb4.py", line 20, in <module>
    _, contours0, hierarchy = cv2.findContours(mask,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
error: C:\builds\master_PackSlaveAddon-win64-vc12-static\opencv\modules\imgproc\src\contours.cpp:198: error: (-210) [Start]FindContours supports only CV_8UC1 images when mode != CV_RETR_FLOODFILL otherwise supports CV_32SC1 images only in function cvStartFindContours

这有什么问题?

0 个答案:

没有答案