VideoReader read function indicies must be positive or logical values

2 ビュー (過去 30 日間)
Benjamin Bloomfield
Benjamin Bloomfield 2021 年 2 月 5 日
回答済み: Yaxuan Li 2021 年 6 月 26 日
Hello,
I'm trying to read specific frames of a video in, add text and then write it out as a new video. I keep getting the error message below after it has read the first frame:
"Error using ProcessVideo (line 11)
Array indices must be positive integers or logical values."
Edited to add that have tried further it looks like if I change the iFrame loop to increment by 1 (i.e. iFrame=1:1:NFrames) then it works. So it looks like read can't hanlde non-sequential calls which is in contradiction with the MATLAB example linked below. While I can read in all frames and discard most of them this makes it very slow so is not desireable.
My script is:
clear all
close all
files=dir('\\192.168.1.184\d\CCTV\*.mp4'); %Path containing files
vout=VideoWriter([files(1).folder, '\VideoOut.mp4'],'MPEG-4'); %Output video
open(vout);
for ifile=1:numel(files) %Loop over all files
fprintf(['File ', num2str(ifile), ' of ', num2str(numel(files)), '\n']); %Display progress
vin=VideoReader([files(ifile).folder, '\', files(ifile).name]); %Open video
NFrames=floor(vin.Duration*vin.FrameRate); %Calculate number of frames (as vin.NumFrames is not populated for some reason)
for iFrame=1:15:NFrames %Loop over desired frames
Frame=read(vin,iFrame); %Read in desired frame
Frame=AddTextToImage(Frame,files(ifile).name,[1700 125],[1 1 1],'Arial',125); %Annotate with file name
writeVideo(vout,Frame); %Write frame to output video
end
clear vin %Shouldn't be necesarry - added during debug
end
close(vout); %Finish writing video
So line 11 is where I attempt to read the frame. The syntax I am using appears to match the example in the read videos using frame index section of https://www.mathworks.com/help/matlab/ref/videoreader.html .
I've checked and nothing is shadowing read, vin or iFrame:
>> which read
read is a built-in method % asyncioimpl.InputStream method
>> which vin
vin is a variable.
>> which iFrame
iFrame is a variable.
So I'm a bit confused why this error is occuring as iFrame is a positive integer.
If anyone has any ideas it would be most appreciated.
I'm running MATLAB R2020b update 3.
  2 件のコメント
Nitin Kapgate
Nitin Kapgate 2021 年 2 月 8 日
Can you share the MAT file which includes the variables like vin, NFrames, iFrame for 1 iteration of the for loop so that I can reproduce the issue at my end?
Benjamin Bloomfield
Benjamin Bloomfield 2021 年 2 月 8 日
I tried to upload a mat file containing vin, iFrame, NFrames however it is 10.5MB which the file exchange will not let me upload (I don't know if I can email it or some other way of uploading it)? I don't know why it is so large as if I call whos all of the variables are 8 bytes.
One interesting observation I found this morning is if I try reading an mp4 that MATLAB wrote then the above script works but if I try reading a different file (from a Swann NVR) it doesn't work. With the file that MATLAB wrote vin.NumFrames is populated with a sensible value but for the other file vin.NumFrames has no value. So there must be some compatability issue with the file (even though it plays fine in VLC media player).
I found a workaround which is to set vin.CurrentTime and then call readFrame so this is no longer urgent but it would be nice to understand as I spent quite a while trying to understand why the code in the help doesn't work before I found a way that does.

サインインしてコメントする。

回答 (1 件)

Yaxuan Li
Yaxuan Li 2021 年 6 月 26 日
I got the error like
"Array indices must be positive integers or logical values."
while using
numFrames = obj.NumberFrames;

カテゴリ

Help Center および File ExchangeAudio and Video Data についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by