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

댓글 수: 1

Adrian Brown
Adrian Brown 2021년 3월 15일
@Image Analyst @Jan I hope if you could please help with any suggest to my question.

댓글을 달려면 로그인하십시오.

 채택된 답변

Bob Thompson
Bob Thompson 2021년 3월 15일

0 개 추천

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

Adrian Brown
Adrian Brown 2021년 3월 16일
편집: Adrian Brown 2021년 3월 16일
@Bob Thompson thank you for your reply, but Unfortunatly when I extract
C = flow(:,:,vidReader.NumFrames).Magnitude
I got 240x352 but instide this matrix I want to get a matrix size 240x352*53
Just to make sure I understand correctly, when you run
C = flow(:,:,vidReader.NumFrames).Magnitude
you get an array of 240x352, but you want an array of 250x352x53?
I'm assuming that vidReader.NumFrames is a single scalar value, in which case you are only calling a single sheet (size 240x352x1) of the flow matrix. To get all the sheets you need to capture all elements at the same time. Try the following:
C = flow.Magnitude; % Not sure if this is going to work, because I can't tell if flow is
% a structure or table. This form should work for a table
C = [flow.Magnitude]; % Should work for a structure
They might require some fiddling, but it should get you going on the right path.
Dear @Bob Thompson thank you so much for your help and support. Actually my purpose is to get a matrix size 240x352*53 which mean 240x18656. For each vidReader.NumFrames a matrix of 240x352 is created
My main purpose is to extract those matrices for each Number Frames.
The instructions :
C = flow.Magnitude;
and
C = [flow.Magnitude];
are only given the flow extraction for the last frame.
I really appreciate any further suggest.
Bob Thompson
Bob Thompson 2021년 3월 16일
Oh, I see the difference now. Getting it to be 240x352*53 shouldn't be to difficult, if we can get it to be 240x352x53 first (just a reshape). Unfortunately, I am unable to access the zip you posted, so I'm going to have to ask for some information from you.
What is the size of flow?
What is the size of vidReader.NumFrames?
What is the size of flow(1).Magnitude?
Dear @Bob Thompson thank you so much for your reply and I am sorry because you can't access to my video. I reupload the video hoping you can download it.
  1. under your suggestion I added this instruction the code
flow(:,:,vidReader.NumFrames) = estimateFlow(opticFlow,frameGray); % the size of flow is 1x1x54 opticalFlow
C = flow(:,:,vidReader.NumFrames).Magnitud; % the size of C now is 240x352 single
2. information about video :
really appreciate your help and support
What is the class of flow?
I just made a quick code to test the concat of a structure and it did what you are looking for:
A.magnitude = randi(100,50,50);
A = repmat(A,1,1,30);
B = [A.magnitude]; % Produces a 50x1500 array of all A.magnitude values
This means that if flow is a structure, then you should be able to get what you want with:
C = [flow.Magnitud];
So, I'll go back to my original question, what is the class of flow?
Adrian Brown
Adrian Brown 2021년 3월 18일
편집: Adrian Brown 2021년 3월 18일
Dear @Bob Thompson thank you so much for your reply. This the class of Flow.
Under your sugestions I got the matrix I want. I really appreciate your support. I read about the Fuction repmat() that you used and as I understand it only for repeating the matrix A a severale times. I am wondering to know as I am trying to store the value of a loop and nt to repeat the same matrix many times.
Bob Thompson
Bob Thompson 2021년 3월 18일
'I am wondering to know as I am trying to store the value of a loop and nt to repeat the same matrix many times.'
I'm not sure I understand what you mean here.
Yes, repmat will duplicate an existing matrix.
as I mention in the begining that the matrix I want to build size 240x352*53 => 240x18656 must contain the value of flow changing during the loop function.
240 is frame height is the input video
352 is frame width in the input video
53 is NumFrames in the input video
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(:,:,vidReader.NumFrames) = estimateFlow(opticFlow,frameGray); % get 4 parameters size 240x352 each
end
In this example we tried to and an idex so to flow flow(:,:,frame) to store the values of flow for each frame.
The main purpose of my question is to create a variable size 240x352*53 => 240x18656 where I can store all my result from while loop for each Numframes.
The repmat function you proposed will only duplicate the flow matrix resulted from while loop for frame = 53, and repeat this matrix 53 times.
I looking for a way where I can build this matrix sizew 240x18656 but instead of repeating matrix flow.magnitude size (240x352) 53 times to store the result from while loop.
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개)

질문:

2021년 3월 15일

댓글:

2021년 3월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by