9/02/2013

OpenCV Video Writer example source code (VideoWriter function)

This is video writer example source code.
This source code was used from video load & display.

...
int main()
{
    VideoCapture capture("rhinos.avi");
    Mat frame;

    //Set properties
    int askFileTypeBox = 0; //-1 is show box of codec
    int Color = 1;
    Size S = Size((int)capture.get(CV_CAP_PROP_FRAME_WIDTH), (int)capture.get(CV_CAP_PROP_FRAME_HEIGHT));

    //make output video file
    VideoWriter outVideo;
    //save video file by origin paramers
    outVideo.open(".\\outVideo.avi", askFileTypeBox, capture.get(CV_CAP_PROP_FPS), S, Color);

    double fps = capture.get(CV_CAP_PROP_FPS);
    //check
    if (!capture.isOpened())
    {
        printf("AVI file can not open.\n");
        return 0;
    }

    //create window
    namedWindow("w");

    while (1)
    {
        //grab frame from file & throw to Mat
        capture >> frame;
        if (frame.empty()) //Is video end?
            break;

        //processing example
        //Sobel(frame, frame, frame.depth(), 1, 0);

        ////////////////////
        //outVideo.write(frame);
        outVideo << frame;
        //display and delay
        imshow("w", frame);

        //delay by origin fps
        if (waitKey((1 / fps) * 1000) > 0)
            break;

    }

    return 0;
}
...

1 comment:

  1. 쒋은 κ°•μ˜ 잘 λ“£κ³  μžˆμŠ΅λ‹ˆλ‹€.
    그런데 4.0.1 λ²„μ „μ—μ„œλŠ” CV_CAP_PROP_FRAME_WIDTH 등이 μ•ˆ λ¨Ήλ„€μš”. CAP_PROP_FRAME_WIDTH둜 μ“°λ©΄ λ™μž‘ν•˜λŠ” 것 κ°™μŠ΅λ‹ˆλ‹€.
    그리고 ν•˜λ‚˜ 질문 μžˆμŠ΅λ‹ˆλ‹€. μ΄μƒν•˜κ²Œ λ…ΈνŠΈλΆμ— 달린 카메라λ₯Ό μ‚¬μš©ν•  λ•Œ capture.get(CAP_PROP_FPS)둜 읽어 보면 항상 0 μž…λ‹ˆλ‹€. μ™œ κ·ΈλŸ°κ±ΈκΉŒμš”? 일단 0인지 κ²€μ‚¬ν•΄μ„œ 0일 경우 μž„μ‹œλ‘œ 30μ •λ„λ‘œ 할당은 ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€λ§Œ μ™œ κ·ΈλŸ°μ§€ κΆκΈˆν•΄μ„œμš”.
    계속 쒋은 κ°•μ˜ 뢀탁 λ“œλ¦½λ‹ˆλ‹€.

    ReplyDelete