如何使用 Matlab 并行分析视频帧?
How to analyse video frames in parallel using Matlab?
我正在逐帧处理大型视频文件。每个帧的处理独立于其他帧(解压缩时),而且计算量很大,所以我认为并行处理是加快分析速度的好方法。虽然我已经自学了使用并行循环的基础知识,但我在将 parfor 的细节与 VideoReader 对象结合起来时遇到了问题。在我的脑海里,我想象代码 运行 像这样
video = VideoReader('video.mp4');
parfor ii = 1 : 90000
frame = read(video, ii);
...analysis on frame...
end
然而,这警告我不要使用 read(),因为它将在未来的版本中被删除,所以我所知道的唯一选择是使用 frameRead()。但是,frameRead 使用 VideoReader 对象的 CurrentTime 属性,它会在每次调用 frameRead 时自行递增(根据 fps)。这对于在正常循环中读取帧来说效果很好,但它会让 parfor 不高兴,因为每一帧都依赖于根据最后一帧增加 CurrentTime。有没有办法使用 readFrame 或其他方式访问并行循环中的独立帧?我尝试使用循环索引和帧速率在每个循环中设置 CurrentTime 值,如下所示:
video = VideoReader('video.mp4');
fps = video.FrameRate
results = cell(totalFrames, 1);
parfor ii = 1 : 900000
video.CurrentTime = ii/fps;
frame = readFrame(video);
results{ii} = customAnalysisFunction(frame)
end
在此示例中,parfor 为 underlined/flagged,原因在此消息中提供:
MATLAB runs loops in parfor functions by dividing the loop iterations into
groups,and then sending them to MATLAB workers where they run in parallel.
For MATLAB to do this in a repeatable, reliable manner, it must be able to
classify all the variables used in the loop. The code uses the indicated
variable in a way that is incompatible with classification
我可以采取哪些步骤才能并行读取兼容的视频帧?
我应该只使用读取功能吗?我不应该这样做的原因是什么?是否有其他适用于 Matlab 的视频工具?
经常向我建议的一个解决方案是,为什么不将视频分成单独的片段?我不想这样做,因为它非常慢并且需要很多额外的步骤和文件处理。很难相信在 Matlab 中没有解决这个问题的方法,所以我期待您的回答!
我不希望并行阅读框架在 MATLAB 中起作用。视频 reader 是一个对象,它具有关于其定位位置的内部状态。您可能会尝试使用该对象的副本。看看这个:
http://mathworks.com/help/matlab/ref/matlab.mixin.copyable-class.html
我正在逐帧处理大型视频文件。每个帧的处理独立于其他帧(解压缩时),而且计算量很大,所以我认为并行处理是加快分析速度的好方法。虽然我已经自学了使用并行循环的基础知识,但我在将 parfor 的细节与 VideoReader 对象结合起来时遇到了问题。在我的脑海里,我想象代码 运行 像这样
video = VideoReader('video.mp4');
parfor ii = 1 : 90000
frame = read(video, ii);
...analysis on frame...
end
然而,这警告我不要使用 read(),因为它将在未来的版本中被删除,所以我所知道的唯一选择是使用 frameRead()。但是,frameRead 使用 VideoReader 对象的 CurrentTime 属性,它会在每次调用 frameRead 时自行递增(根据 fps)。这对于在正常循环中读取帧来说效果很好,但它会让 parfor 不高兴,因为每一帧都依赖于根据最后一帧增加 CurrentTime。有没有办法使用 readFrame 或其他方式访问并行循环中的独立帧?我尝试使用循环索引和帧速率在每个循环中设置 CurrentTime 值,如下所示:
video = VideoReader('video.mp4');
fps = video.FrameRate
results = cell(totalFrames, 1);
parfor ii = 1 : 900000
video.CurrentTime = ii/fps;
frame = readFrame(video);
results{ii} = customAnalysisFunction(frame)
end
在此示例中,parfor 为 underlined/flagged,原因在此消息中提供:
MATLAB runs loops in parfor functions by dividing the loop iterations into
groups,and then sending them to MATLAB workers where they run in parallel.
For MATLAB to do this in a repeatable, reliable manner, it must be able to
classify all the variables used in the loop. The code uses the indicated
variable in a way that is incompatible with classification
我可以采取哪些步骤才能并行读取兼容的视频帧?
我应该只使用读取功能吗?我不应该这样做的原因是什么?是否有其他适用于 Matlab 的视频工具?
经常向我建议的一个解决方案是,为什么不将视频分成单独的片段?我不想这样做,因为它非常慢并且需要很多额外的步骤和文件处理。很难相信在 Matlab 中没有解决这个问题的方法,所以我期待您的回答!
我不希望并行阅读框架在 MATLAB 中起作用。视频 reader 是一个对象,它具有关于其定位位置的内部状态。您可能会尝试使用该对象的副本。看看这个: http://mathworks.com/help/matlab/ref/matlab.mixin.copyable-class.html