OpenCV VideoWriter() video output is much faster than normal time. [closed]

asked 2017-03-08 10:33:18 -0600

iPikachu gravatar image

Hi everyone, I have a issue with my video capture code where the output of the video is super fast when outputted, I searched some documents and they all suggest that the problem is due to the fps of the camera not in sync to the output video fps, therefore making the output video faster or slower than normal time (capture fps > write fps: slower, capture fps < write fps: faster).

Back to my case, I did manually set the fps of the two webcams to 10 fps and matched 10 fps when using VideoWriter() as well, I also set the waitKey() to 100ms per frame so that it matches with 10 fps, but I'm still getting super fast video output, is it due to the delay processing time of my video_multi_cam_light_detection() function as it does take sometime to process, such that individual frame have longer delay which decrease the overall preset 10 fps? (I'm suspecting this because I tested another simple VideoCapture program with out my detection function and it's only faster for like 3 seconds).

Again, Thank you all for the help, if my logic or code are wrong in any ways please feel free to indicate it as I'm really new to OpenCV.

Here is my code:

int video_light_detection() {
    string raw_video_path = "./RAW_VIDEO";
    string processed_video_path = "./PROCESSED_VIDEO";
    string raw_image_path = "./SAMPLE_CAPTURED";
    string processed_image_path = "./SAMPLE_CAPTURED";

    VideoCapture cap(0);
    cap.set(CV_CAP_PROP_FPS, 10);
    VideoCapture cap1(1);
    cap1.set(CV_CAP_PROP_FPS, 10);

    time_t current_time = time(0);
    tm *time_p = localtime(&current_time);
    int year = 1900 + time_p->tm_year;
    int month = 1 + time_p->tm_mon;
    int day = time_p->tm_mday;
    int hour = time_p->tm_hour;
    int min = time_p->tm_min;

    ostringstream oss;
    oss << "_" << hour << "_" << min << "_" << month << "_" << day << "_" << year;
    string file_suffix = oss.str();

    raw_video_path += file_suffix + string(".mkv");
    processed_video_path += file_suffix + string(".mkv");
    raw_image_path += file_suffix + string(".jpg");
    processed_image_path += file_suffix + string(".jpg");

    if (!cap.isOpened() || !cap1.isOpened()) {
      cerr << "Camera Open Failure" << endl;
      return -1;
    }

    cap.set(CV_CAP_PROP_FRAME_WIDTH, 1280);
    cap.set(CV_CAP_PROP_FRAME_HEIGHT, 960);
    cap1.set(CV_CAP_PROP_FRAME_WIDTH, 1280);
    cap1.set(CV_CAP_PROP_FRAME_HEIGHT, 960);

    VideoWriter video_raw;
    VideoWriter video_processed;

    namedWindow("Capture Window", WINDOW_NORMAL);
    resizeWindow("Capture Window", 640, 1280);
    namedWindow("Detection Window", WINDOW_NORMAL); 
    resizeWindow("Detection Window", 640, 1280);

    int iterations = 0;

    while (true) {
        Mat frame, frame1, merged, bgr_image_filter_applied;
        cap >> frame;
        cap1 >> frame1;
        vconcat(frame, frame1, merged);
        if (iterations == 0) {
            int width = merged.cols;
            int height = merged.rows;
            video_raw.open(raw_video_path, CV_FOURCC('M', 'J', 'P', 'G'), 10, Size(width, height), true);
            video_processed.open(processed_video_path, CV_FOURCC('M', 'J', 'P', 'G'), 10, Size(width, height), true);
        }

        video_raw << merged;
        bgr_image_filter_applied = video_multi_cam_light_detection(merged);
        video_processed << bgr_image_filter_applied;

        imshow("Capture Window", merged); // Show origional captured image
        imshow("Detection Window", bgr_image_filter_applied); // Show processed image 

        iterations++;

        if (waitKey(100) == 27) {
            imwrite(raw_image_path, merged);
            imwrite(processed_image_path, bgr_image_filter_applied);
            video_raw.release();
            video_processed.release();
            break;
        }
     }

     return 0;
}
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by iPikachu
close date 2017-03-20 09:35:42.608718

Comments

Not sure, but as far as I know cv::waitKey() blocks only the last cv::imshow and does not block the whole program. So what happens if you put another cv::waitKey(100) after your first cv::imshow?

matman gravatar imagematman ( 2017-03-08 12:02:12 -0600 )edit

the video writer is not at all synchronized to anything.

so, if you want to write a 10 fps video, your processing (including the waitKey() and writing the frames) has to be at least that fast. e.g. increasing the waitKey() time will only make it worse.

berak gravatar imageberak ( 2017-03-09 10:20:07 -0600 )edit

Hi berak, are you implying that waitKey() and my processing time are delaying the capture rate ( making capture fps less than 10 fps), therefore when I use VideoWriter at 10 fps it made the video output fast as the capture fps is less than write fps?

iPikachu gravatar imageiPikachu ( 2017-03-09 12:03:23 -0600 )edit

"are you implying that waitKey() and my processing time are delaying the capture rate"

yes, exactly

berak gravatar imageberak ( 2017-03-09 12:05:32 -0600 )edit

@iPikachu: could you please share the solution for this problem?

priya sharma gravatar imagepriya sharma ( 2017-10-18 21:11:21 -0600 )edit