Reading a video with openCV

前端 未结 3 729
清歌不尽
清歌不尽 2020-12-20 16:18

I have a video engine2.avi that I want to read and show with openCV. Here\'s my code:

#include 
#include 

        
相关标签:
3条回答
  • 2020-12-20 16:48

    If you are just looking out for displaying the video, here's the code that worked for me, Please check if it helps you in any way.

    #include <stdio.h>
    #include <opencv2/opencv.hpp>
    int main(){
    CvCapture *camera=cvCaptureFromFile("C:\\test.avi");
    if (camera==NULL)
        printf("camera is null\n");
    else
        printf("camera is not null");
    
    cvNamedWindow("img");
    while (cvWaitKey(10)!=atoi("q")){
        double t1=(double)cvGetTickCount();
        IplImage *img=cvQueryFrame(camera);
        /*if(img){
            cvSaveImage("C:/opencv.jpg",img);
        }*/
        double t2=(double)cvGetTickCount();
        printf("time: %gms  fps: %.2g\n",(t2-t1)/(cvGetTickFrequency()*1000.), 1000./((t2-t1)/(cvGetTickFrequency()*1000.)));
        cvShowImage("img",img);
    }
    cvReleaseCapture(&camera);
    }
    

    Hope this helps you.

    0 讨论(0)
  • 2020-12-20 16:58

    Here is a couple of links that might help you:

    • Load, Save and Show YUV 420 images
    • How to read a frame from YUV file in OpenCV?
    • Converting YUV into BGR or RGB in OpenCV

    Edit:

    I must clear something first: OpenCV is capable of reading YUV frames from a video file because it's the underlying library (FFmpeg/GStreamer) that does the job. OpenCV also supports converting between a specific type of YUV and RGB through cvCvtColor() with CV_YCrCb2RGB or CV_RGBYCrCb.

    Upon examining your question again, I noticed you didn't specify the kind of error that happened. You could do a better job at dealing with a possible failure from the capture interface by printing a message to the screen instead of throwing it.

    I tested the video file you shared and I had no problems playing it on a window using the following code:

    #include <cv.h>
    #include <highgui.h>
    
    #include <iostream>
    
    int main(int argc, char* argv[])
    {
        cv::VideoCapture cap(argv[1]);
        if (!cap.isOpened())
        {
            std::cout << "!!! Failed to open file: " << argv[1] << std::endl;
            return -1;
        }
    
        cv::Mat frame;
        for(;;)
        {
    
            if (!cap.read(frame))             
                break;
    
            cv::imshow("window", frame);
    
            char key = cvWaitKey(10);
            if (key == 27) // ESC
                break;
        }
    
        return 0;
    }
    

    If, for some reason, the capture interface fails to open the file it will quit the application immediately, instead of going further just to crash at cap.read(frame).

    0 讨论(0)
  • 2020-12-20 17:06

    I realised that, the VideoCapture object needs opencv_ffmpeg310_64.dll. Once you copy this dll in your binary folder your VideoCapture object should be able to read video file.

    0 讨论(0)
提交回复
热议问题