OpenCV VideoWriter Too Long

I have a simple application using OpenCV 2.4.7 (compiled with gcc 4.7.2 on Mac OS Mavericks) to create video from a collection of images:

std::vector<std::string> filenames = ...; // image filenames

const int FPS = 25;
const cv::Size SZ(640, 896);
cv::VideoWriter video;
video.open("video.avi", CV_FOURCC('m', 'p', '4', 'v'), FPS, SZ, true);

if(video.isOpened())
{
   for(size_t i = 0; i < filenames.size(); ++i)
   {
     cv::Mat im = cv::imread(filenames[i], 1);
     if(!im.empty()) video << im;
   }
}

I only profile the team video << im, and I saw that it takes longer and more as the number of frames in the video increases. At the beginning, each image is added in less than 5 ms. After 10 thousand images, it takes 100 ms per image, and after 20 thousand images it takes about 170 ms. At the same time, the video.avi file is created and grows.

With a different set of images (of the same size and format), I get up to 3.5 seconds per image after 10 thousand images.

My question is: is this behavior normal?

+3
source share
1

, , . , ..?

  • Windows 7
  • XVID-, xvid.
  • OpenCV 2.4.3

. opencv python. 10 000 ~ 0 2 640x480 ().

from collections import deque
import os
import time

import cv2
from cv2 import cv
import numpy

images_directory = 'images'
output_name = 'video.avi'
fps = 25
size = (640, 480)
is_color = True
frames_to_produce = 10000
write_times = deque()
writer = cv2.VideoWriter(output_name, -1, fps, size, is_color)
# writer = cv2.VideoWriter(output_name, cv.CV_FOURCC('x', 'v', 'i', 'd'),
#                          fps, size, is_color)

if writer.isOpened():
    for i in xrange(frames_to_produce):
        filename = os.path.join(images_directory, str(i % 10) + '.png')
        image = cv2.imread(filename)
        if image is not None:
            write_time_start = time.time()
            writer.write(image)
            write_times.append(time.time() - write_time_start)

print 'frames: {}'.format(len(write_times))
histogram = numpy.histogram(write_times, bins=5)
for count, bin_ in zip(*histogram):
    print '{0}: {1:.5f}'.format(count, bin_)

  • : 10000

:

  • 1987: 0.00000
  • 6108: 0.00060
  • 0: 0.00120
  • 1891: 0.00180
  • 14: 0.00240

, , .

0

All Articles