如何在matlab中加快读取速度

时间:2023-02-02 02:44:03

I am have a task that needed to search a movie. I would like to get every frame from the movie and do what I want, as shown in the following code.

我有一个需要搜索电影的任务。我希望从电影中获取每一帧并按照我的要求进行操作,如下面的代码所示。

videoObject = VideoReader(fileName);
numberOfFrames = videoObject.NumberOfFrames;
frameRate = videoObject.FrameRate;

for frame = 1 : numberOfFrames
    thisFrame = read(videoObject, frame);     

    %% Here I do what I need
end

Then I found that I can pick every 10 frames and still do what I want. So I changed the code like this.

然后我发现我可以每隔10帧挑选一次并仍然按照自己的意愿行事。所以我改变了这样的代码。

videoObject = VideoReader(fileName);
numberOfFrames = videoObject.NumberOfFrames;
frameRate = videoObject.FrameRate;

for frame = 1 : 10 : numberOfFrames
    thisFrame = read(videoObject, frame);     

    %% Here I do what I need
end

But I found that the time spent by the second version is very long. I measure the time spent like this.

但我发现第二个版本花费的时间很长。我衡量这样花的时间。

videoObject = VideoReader(fileName);
numberOfFrames = videoObject.NumberOfFrames;
frameRate = videoObject.FrameRate;

tic
for frame = 1 : numberOfFrames
    thisFrame = read(videoObject, frame);     

    if(mod(frame, 100) == 1)
        toc
        tic
    end
    %% Here I do what I need
end

Output:

Elapsed time is 0.049704 seconds.
Elapsed time is 1.180742 seconds.
Elapsed time is 1.220797 seconds.
Elapsed time is 1.188777 seconds.
Elapsed time is 1.238565 seconds.

I do the same thing to the second version and get

我对第二个版本做同样的事情并得到

Elapsed time is 0.018620 seconds.
Elapsed time is 4.982572 seconds.
Elapsed time is 15.634653 seconds.
Elapsed time is 24.626017 seconds.
Elapsed time is 5.307010 seconds.
Elapsed time is 14.970220 seconds.
Elapsed time is 24.230995 seconds.

The speed is very strange and also for the same 100 frames it takes much more time. How to fix this problem?

速度非常奇怪,同样的100帧也需要更多的时间。如何解决这个问题?

1 个解决方案

#1


You may be in a Shlemiel the Painter situation: it looks like each call to read() is reading all the frames up to the one you specify. Which makes sense, because most movie file formats are not random access. Instead they encode occasional key frames and then a series of deltas to that key frame.

您可能处于Shlemiel the Painter情境中:看起来每次调用read()都会读取您指定的所有帧。这是有道理的,因为大多数电影文件格式不是随机访问。相反,它们会对偶然的关键帧进行编码,然后对该关键帧进行一系列的增量编码。

The one where you're striding by 10 takes longer on each pass because it has to read 10 times as many frames on each call.

你通过10的那个在每次通过时需要更长的时间,因为它必须在每次通话时读取10倍的帧数。

It looks like the read method is designed more to take a list of frame indexes. Since you know the indexes of all the frames you want to get, you can replace it with a single call to read.

看起来read方法的设计更多是为了获取帧索引列表。由于您知道要获取的所有帧的索引,因此可以使用单个调用来替换它。

allFrames = read(videoObject, 1:10:numberOfFrames)

Or switch to the readFrame method, which will allow you to grab frames sequentially while maintaining a placeholder in videoObject's state. The help for VideoObject.read says read is deprecated in favor of readFrame, anyway.

或者切换到readFrame方法,这将允许您按顺序抓取帧,同时在videoObject的状态中保持占位符。 VideoObject.read的帮助说不推荐使用read而不支持readFrame。

#1


You may be in a Shlemiel the Painter situation: it looks like each call to read() is reading all the frames up to the one you specify. Which makes sense, because most movie file formats are not random access. Instead they encode occasional key frames and then a series of deltas to that key frame.

您可能处于Shlemiel the Painter情境中:看起来每次调用read()都会读取您指定的所有帧。这是有道理的,因为大多数电影文件格式不是随机访问。相反,它们会对偶然的关键帧进行编码,然后对该关键帧进行一系列的增量编码。

The one where you're striding by 10 takes longer on each pass because it has to read 10 times as many frames on each call.

你通过10的那个在每次通过时需要更长的时间,因为它必须在每次通话时读取10倍的帧数。

It looks like the read method is designed more to take a list of frame indexes. Since you know the indexes of all the frames you want to get, you can replace it with a single call to read.

看起来read方法的设计更多是为了获取帧索引列表。由于您知道要获取的所有帧的索引,因此可以使用单个调用来替换它。

allFrames = read(videoObject, 1:10:numberOfFrames)

Or switch to the readFrame method, which will allow you to grab frames sequentially while maintaining a placeholder in videoObject's state. The help for VideoObject.read says read is deprecated in favor of readFrame, anyway.

或者切换到readFrame方法,这将允许您按顺序抓取帧,同时在videoObject的状态中保持占位符。 VideoObject.read的帮助说不推荐使用read而不支持readFrame。