0
votes

When I use cap=cv2.VideoCapture('L1.mp4') to read a video, it gives the below error. This error is only thrown for some videos. Any suggestions please?

cap=cv2.VideoCapture('L1.mp4')
_,img=cap.read()
fgbg = cv2.createBackgroundSubtractorMOG2()
fgmask = fgbg.apply(img)
fgmask = cv2.morphologyEx(fgmask, cv2.MORPH_CLOSE, kernel)

OpenCV Error: Assertion failed (dims <= 2 && step[0] > 0) in cv::Mat::locateROI, file C:\build\master_winpack-bindings-win64-vc14-static\opencv\modules\core\src\matrix.cpp, line 949 1 Traceback (most recent call last): File "E:\py_work\lipclips\PlotBox\may17.py", line 109, in fgmask = cv2.morphologyEx(fgmask, cv2.MORPH_CLOSE, kernel) cv2.error: C:\build\master_winpack-bindings-win64-vc14-static\opencv\modules\core\src\matrix.cpp:949: error: (-215) dims <= 2 && step[0] > 0 in function cv::Mat::locateROI

warning: Error opening file (/build/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:779) warning: L1.mp4 (/build/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:780)

1
Please edit your post with enough code to create a minimal, complete, and verifiable example.alkasm

1 Answers

0
votes

You can't just extract fgmask from a BackgroundSubtractor with only one image. In this case, your fgmask is empty and you can't apply morphologyEx. Try:

cap = cv2.VideoCapture('L1.mp4')
fgbg = cv2.createBackgroundSubtractorMOG2()

while True:
    ret, img = cap.read()
    if not ret:
        break

    fgmask = fgbg.apply(img)

    # check if fgmask is valid
    if fgmask is None:
        continue

    fgmask = cv2.morphologyEx(fgmask, cv2.MORPH_CLOSE, kernel)