从视频中提取帧错误



我试图从avi视频中提取所有帧并显示它们。代码如下:

import cv2
from cv2 import cv
import time
cap=cv2.VideoCapture('video1.avi')
count=cap.get(cv.CV_CAP_PROP_FRAME_COUNT)
cap.set(cv.CV_CAP_PROP_POS_FRAMES, count-1)
cv2.namedWindow("Display", cv2.CV_WINDOW_AUTOSIZE)
while True:
    ret,frame=cap.read()
    cv2.imshow("Display", frame)
    time.sleep(0.1)
我得到的错误是:
cv2.imshow("Display", frame)
cv2.error: ......srcopencvmoduleshighguisrcwindow.cpp:261: error: (-215) size.width>0 && size.height>0

代码有问题吗?如果不是,那么我如何删除错误?

如果frame在imshow之前为null,则必须打破循环,并使用waitKey代替sleep。

所以把你的代码改成
while True:
    ret,frame=cap.read()
    if not ret: break
    cv2.imshow("Display", frame)
    cv2.waitKey(20)

试试这段代码,它从视频中提取帧。

it works for me

#include <stdio.h>
    #include <iostream>
    #include <conio.h>
    #include <opencv2/core/core.hpp>
    #include <opencv2/highgui/highgui.hpp>
    #include <opencv2/imgproc/imgproc.hpp>
    using namespace cv;
    int main(int argc, char** argv)
    {
        // Read a video file from file
        CvCapture* capture = cvCaptureFromAVI("C:\Users\Pavilion\Documents\Visual Studio 2013\Projects\video\optical illusions.avi");
        int loop = 0;
        IplImage* frame = NULL;
        Mat matframe;
        Mat img_hsv, img_rgb;
        char fname[20];
        do
        {
            // capture frames from video
            frame = cvQueryFrame(capture);
            matframe = cv::cvarrToMat(frame);
            // create a window to show frames
            cvNamedWindow("video_frame", CV_WINDOW_AUTOSIZE);
            // Display images in a window
            cvShowImage("video_frame", frame);
            sprintf(fname, "C:\Users\Pavilion\Documents\Visual Studio 2013\Projects\video\video\frames\frame%d.jpg", loop);
            // write images to a folder
            imwrite(fname, matframe);
            // Convert RGB to HSV
            cvtColor(img_rgb, img_hsv, CV_RGB2HSV);
            loop++;
            cvWaitKey(10);
        } while (frame != NULL);
        return 0;
    }

相关内容

  • 没有找到相关文章

最新更新