save matrices from loop

2 次查看(过去 30 天)
Hello,
I am using this code that estimate Optical flow of a given video.
My purpose is to create a matrix that contain all the flow values for all while loop.
vidReader = VideoReader('Tennis Ball.avi'); % Read the Video size of frame in a video is 240x352
opticFlow = opticalFlowFarneback;
%% Estimate Optical Flow of each frame
while hasFrame(vidReader)
frameRGB = readFrame(vidReader);
frameGray = rgb2gray(frameRGB);
flow = estimateFlow(opticFlow,frameGray); % get 4 parameters size 240x352 each
% I aim to get all the the values get it in flow for each videoframe
%size of the matrix must be 240x352*53 where 53 is the number of frames in
%the video
end
I really apprecaite any help

采纳的回答

Bob Thompson
Bob Thompson 2021-3-15
I could be wrong, but it seems like you just need to index flow.
vidReader = VideoReader('Tennis Ball.avi'); % Read the Video size of frame in a video is 240x352
opticFlow = opticalFlowFarneback;
%% Estimate Optical Flow of each frame
Frame = 0; % Initialize frame index
while hasFrame(vidReader)
Frame = 1; % Increase index for new frame
frameRGB = readFrame(vidReader);
frameGray = rgb2gray(frameRGB);
flow(:,:,frame) = estimateFlow(opticFlow,frameGray); % get 4 parameters size 240x352 each
end
  11 个评论
Bob Thompson
Bob Thompson 2021-3-18
I apologize for my own inability in this area, I hope it hasn't held you back too much.
That being said, I don't have a particularly elegant solution for you as I'm not very familiar with the opticalFlow object. For a more brute force solution see below for two options
OPTION 1
vidReader = VideoReader('Tennis Ball.avi'); % Read the Video size of frame in a video is 240x352
opticFlow = opticalFlowFarneback;
% Initialize stuff
frame = 0;
flow = struct('Magnitude',[],'Frame',[]); % Force flow to be a structure
%% Estimate Optical Flow of each frame
while hasFrame(vidReader)
frame = frame + 1;
frameRGB = readFrame(vidReader);
frameGray = rgb2gray(frameRGB);
flow(frame).object = estimateFlow(opticFlow,frameGray); % stores opticalFlow values to structure element
flow(frame).Magnitude = flow(frame).object.Magnitude; % Pull Magnitude out of opticalFlow object into structure
flow(frame).Frame = frame;
end
C = [flow.Magnitude];
OPTION 2
vidReader = VideoReader('Tennis Ball.avi'); % Read the Video size of frame in a video is 240x352
opticFlow = opticalFlowFarneback;
% Initialize stuff
frame = 0;
%% Estimate Optical Flow of each frame
while hasFrame(vidReader)
frame = frame + 1;
frameRGB = readFrame(vidReader);
frameGray = rgb2gray(frameRGB);
flow = estimateFlow(opticFlow,frameGray); % get 4 parameters size 240x352 each
C(:,((frame-1)*size(flow.Magnitude,2)+1):(frame*size(flow.Magnitude,2))) = flow.Magnitude;
end
Adrian Brown
Adrian Brown 2021-3-18
Dear @Bob Thompson I really appreciate any reply from you. from each suggestion I learned a new thing from you. The two option are very helpfull for me. Thank you for your time and all your suggestion.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Tracking and Motion Estimation 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by