How to write video file in OpenCV 2.4.3

前端 未结 2 776
生来不讨喜
生来不讨喜 2020-12-03 14:39

I am using OpenCV 2.4.3 to read and write a video file. My code is like this:

cv::VideoCapture video;
video.open ( \"D:\\\\testVideo.avi\" );
cv::VideoWrite         


        
相关标签:
2条回答
  • 2020-12-03 15:10

    The problem might be the codec you are using.

    A simple test to make sure your stuff is working properly is to simply retrieve frames from a webcam and write them on a video file:

    // Load input video
    cv::VideoCapture input_cap(argv[1]);
    if (!input_cap.isOpened())
    {
            std::cout << "!!! Input video could not be opened" << std::endl;
            return;
    }
    
    // Setup output video
    cv::VideoWriter output_cap(argv[2], 
                   input_cap.get(CV_CAP_PROP_FOURCC),
                   input_cap.get(CV_CAP_PROP_FPS),
                   cv::Size(input_cap.get(CV_CAP_PROP_FRAME_WIDTH),
                   input_cap.get(CV_CAP_PROP_FRAME_HEIGHT)));
    
    if (!output_cap.isOpened())
    {
            std::cout << "!!! Output video could not be opened" << std::endl;
            return;
    }
    
    
    // Loop to read from input and write to output
    cv::Mat frame;
    
    while (true)
    {       
        if (!input_cap.read(frame))             
            break;
    
        output_cap.write(frame);
    }
    
    input_cap.release();
    output_cap.release();
    
    0 讨论(0)
  • 2020-12-03 15:21

    You declare cv::VideoWriter output of frame size 1200*1600. So resize the frame to 1200*1600 using cv::resize(img,img,cv::Size(1200,1600));before output.write(img);

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