VideoReader read function indicies must be positive or logical values
6 views (last 30 days)
Show older comments
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.
AddTextToImage is from https://www.mathworks.com/matlabcentral/fileexchange/40959-add-text-to-image.
2 Comments
Nitin Kapgate
on 8 Feb 2021
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?
Answers (1)
Yaxuan Li
on 26 Jun 2021
I got the error like
"Array indices must be positive integers or logical values."
while using
numFrames = obj.NumberFrames;
0 Comments
See Also
Categories
Find more on Audio and Video Data in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!