Search code examples
python-2.7opencvmkv

Why is OpenCV retrivieng this error in the middle of a video processing? error: (-215) scn == 3 || scn == 4 in function cv::cvtColor


I am using openCV to process a video capture (.mkv file) with python2

import cv2
(....)

videofile = 'a.mkv'
cap = cv2.VideoCapture(videofile)
(....)
t_frames, fps, t_time, w, h = getVideoInfo(cap)
(....)
cap.set(cv2.CAP_PROP_POS_FRAMES ,first_frame)
(....)
frame = 0
while (not stopped==1):
    ret, frame = cap.read(frame)
    (...some functions related with cv2 are here called...)
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    (...)

However at almost the end of the video processing some erros appear: error: (-215) scn == 3 || scn == 4 in function cv::cvtColor

And the error occurs at cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) Any idea? A bad frame? However I called before some functions related with the cv2 and it was ok.


Solution

  • After load a resource check if the resource was loaded properly.

    ret, frame = cap.read(frame)
    (....)
    if frame is not None
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)