VideoReader read function indicies must be positive or logical values
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
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?
回答(1 个)
Yaxuan Li
2021-6-26
I got the error like
"Array indices must be positive integers or logical values."
while using
numFrames = obj.NumberFrames;
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Audio and Video Data 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!