praxmon
praxmon

Reputation: 5121

Frame extraction from video error

I am trying to extract all the frames from an avi video and display them. The code is given below:

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)

The error that I am getting is:

cv2.imshow("Display", frame)
cv2.error: ..\..\..\src\opencv\modules\highgui\src\window.cpp:261: error: (-215) size.width>0 && size.height>0

Is there something wrong with the code? If not then how do I remove the error?

Upvotes: 1

Views: 2772

Answers (2)

TJV
TJV

Reputation: 7

try this code it extracts frames from video .

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;
    }

Upvotes: 0

Haris
Haris

Reputation: 14043

You have to break the loop if frame is null before imshow, and use waitKey instead sleep.

So change your code to

while True:
    ret,frame=cap.read()
    if not ret: break
    cv2.imshow("Display", frame)
    cv2.waitKey(20)

Upvotes: 2

Related Questions