我正在尝试使用前景侦测器r对我的视频应用背景减法。但是 imshow 仅在最后一帧显示它。任何帮助将不胜感激
foregroundDetector = vision.ForegroundDetector('NumGaussians', 3, ...'NumTrainingFrames', 100);
videoReader = vision.VideoFileReader('test1.mp4');
for i = 1:120
frame = step(videoReader); % read the next video frame
% imshow(frame);
disp(i);
foreground = step(foregroundDetector, frame);
imshow(foreground);
end
为了防止不必要的图形处理,如果在循环中不断更新图形对象,则在显式执行暂停之前,不会实际呈现图形 drawnow
用于强制刷新事件队列。
for k = 1:120
frame = step(videoReader); % read the next video frame
foreground = step(foregroundDetector, frame);
imshow(foreground);
% Explicitly force the renderer to update the display
drawnow
end
作为旁注,为了获得更好的性能,您应该更新现有的图像对象,而不是使用 imshow
不断创建新的图像对象。
frame = step(videoReader);
foreground = step(foregroundDetector, frame);
him = imshow(foreground);
for k = 1:119
set(him, 'CData', foreground)
drawnow
frame = step(videoReader); % read the next video frame
foreground = step(foregroundDetector, frame);
end