当前位置: 动力学知识库 > 问答 > 编程问答 >

c++ - OpenCV VideoWriter taking too long

问题描述:

I have a simple application using OpenCV 2.4.7 (compiled with gcc 4.7.2 in Mac OS Mavericks) to create a 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 am profiling just the command video << im and I have seen it takes longer and longer as the number of frames in the video increases. In 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, around 170 ms. At the same time, the file "video.avi" is being creating and growing.

With another set of images (same size and format), I am obtaining up to 3.5 seconds per image after 10 thousand images.

My question is: is this behavior normal?

网友答案:

My setup may be completely different than yours but here is what I have. Can you post more details about your setup, codec, etc.?

  • Windows 7
  • XVID codec as installed directly from xvid site
  • OpenCV 2.4.3

It doesn't seem normal to me. Below is a simple video writer with opencv and python. The histogram of write times for 10,000 frames ranges from ~0 to 2ms for 640x480 (black) images.

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_)

yields

  • frames: 10000

and for frame count: frame time

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

I haven't done a graph but I could if you want me to.

分享给朋友:
您可能感兴趣的文章:
随机阅读: