OpenCV Error: Assertion failed (m.dims >= 2) why?

asked 2015-12-02 18:30:05 -0500

Guilherme gravatar image

I'm study background subtraction with OpenCV, but when the processes ends, the Terminal shows this error:

OpenCV Error: Assertion failed (m.dims >= 2) in Mat, file /home/guilherme/opencv/modules/core/src/matrix.cpp, line 437
Traceback (most recent call last):
  File "bgsub.py", line 10, in <module>
    erode = cv2.erode(fgmask,kernel,iterations = 1)
cv2.error: /home/guilherme/opencv/modules/core/src/matrix.cpp:437: error: (-215) m.dims >= 2 in function Mat

Code:

import numpy as np
import cv2
cap = cv2.VideoCapture('sample.avi')
fgbg = cv2.createBackgroundSubtractorMOG2(detectShadows=False)
kernel = np.ones((5,5), np.uint8)
key = True
while(1):
    ret, frame = cap.read()
    fgmask = fgbg.apply(frame)
    erode = cv2.erode(fgmask,kernel,iterations = 1)
    dilation = cv2.morphologyEx(erode, cv2.MORPH_OPEN, kernel)
    cv2.imshow('frame', dilation)
    cv2.imshow('original', frame)
    k = cv2.waitKey(30) & 0xff
    if k == 27:
        break
cap.release()
cv2.destroyAllWindows()

I'm using Python 2.7 and OpenCV 3. How can I solve this? Thanks a lot!

edit retag flag offensive close merge delete

Comments

i don't know python equivalent but you need to check if frame is empty after ret, frame = cap.read()

if( frame.empty() )
{
    break;
}
sturkmen gravatar imagesturkmen ( 2016-01-16 13:52:11 -0500 )edit