Average Optical Flow vectors and plot over multiple frames
조회 수: 8 (최근 30일)
이전 댓글 표시
Hello,
I am trying to get the average optical flow vectors for a whole video (e.g. for a 10 second video). Currently I am using the opticalFlowFarneback method from Estimate optical flow example with the Computer Vision Toolbox (https://www.mathworks.com/help/vision/ref/opticalflowhs.estimateflow.html):
%% Estimating Optical Flow
% This example uses the Farneback Method to to estimate the direction and speed of moving
% cars in the video
% Copyright 2018 The MathWorks, Inc.
%% Read the video into MATLAB
vidReader = VideoReader('visiontraffic.avi');
opticFlow = opticalFlowFarneback;
%% Estimate Optical Flow of each frame
while hasFrame(vidReader)
frameRGB = readFrame(vidReader);
frameGray = rgb2gray(frameRGB);
flow = estimateFlow(opticFlow,frameGray);
imshow(frameRGB)
hold on
% Plot the flow vectors
plot(flow,'DecimationFactor',[25 25],'ScaleFactor', 2)
% Find the handle to the quiver object
q = findobj(gca,'type','Quiver');
% Change the color of the arrows to red
q.Color = 'r';
drawnow
hold off
end
Although this produces a nice visualization of flow vectors between two frames (see attached image), I need more like an average of flow vectors across all frames of the video. I am quite new to MatLab, I would be super thankful for any feedback how to change abovementioned code.

댓글 수: 2
Adam Danz
2021년 4월 19일
Your question is framed in such a way that only people familiar with the tutorial you mentioned can help.
If you can redefine the question more generally or provide a minimal working example, you might have a better chance of a bite.
채택된 답변
Adam Danz
2021년 4월 21일
편집: Adam Danz
2021년 4월 22일
Here's a demo that shows how to
- Store the flow structure within a loop (assumes all flow arrays are the same size)
- Average a variable in the flow structure across loop iterations.
% preallocate structure array
flow = opticalFlow;
% Create and store optic flow object within loop
for i = 1:10
Vx = randn(100,100);
Vy = randn(100,100);
flow(i) = opticalFlow(Vx,Vy);
end
% flow is a 1x10 structure array
% Concatenate Orientation along 3rd dimension
% flowOrientation is a 100x100x10 array for
% 100x100 flow vectors across 10 iterations.
flowOrientation = cat(3,flow.Orientation);
% Average Orientation values across loop iterations
% meanOrientation is 100x100 mean vectors
meanOrientation = mean(flowOrientation,3)
댓글 수: 6
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Tracking and Motion Estimation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



