save matrices from loop
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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
2021년 3월 15일
채택된 답변
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
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
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.
- 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
2021년 3월 18일
편집: Adrian Brown
2021년 3월 18일

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
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
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개)
카테고리
도움말 센터 및 File Exchange에서 Video Formats and Interfaces에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
