Edge function error while loop video frames, VideoReader - Index in position is invalid

1 view (last 30 days)
v = VideoReader('images_video/Maldives.mp4');
while hasFrame(v)
frame = readFrame(v);
figure, imshow(frame);
BW = rgb2gray(frame);
edge = edge(BW, 'canny', 0.2);
figure, imshow(edge);
end
When we run this, the first frame shows with imshow(frame) then the edge function works and the first edge shows with imshow(edge), the loop starts again and the next imshow(frame) works, and then we get an error:
Index in position 3 is invalid. Array indices must be positive integers or logical values.
Otherwise, the loop runs fine. Clearly something to do with the edge function is trying to operate on itself or something. Probably something simple. Any ideas please?
MATLAB: R2018b

Accepted Answer

KALYAN ACHARJYA
KALYAN ACHARJYA on 9 Oct 2018
Edited: KALYAN ACHARJYA on 9 Oct 2018
% I have tested it with the different video file, change edge name to different ones, like edge1 , as edge is the inbuilt function name and possible of name conflict.
v=VideoReader('images_video/Maldives.mp4');
while hasFrame(v)
frame=readFrame(v);
figure,imshow(frame);
BW=rgb2gray(frame);
edge1=edge(BW, 'canny', 0.2);
figure,imshow(edge1);
end
%If you try the larger video files, it may show the following error
Out of memory. Type HELP MEMORY for your options.
Otherwise the code is OK, test it will very small video file, having 10-12 frames.
On the otherhand if you remove the both imshow statement, it perfectly works if video size little larger too.
  2 Comments
Walter Roberson
Walter Roberson on 9 Oct 2018
Edited: Walter Roberson on 9 Oct 2018
Which is to say that the original code calls edge as a function and writes the results to a variable named edge. The next time though the loop edge now refers to the variable not the function.
KALYAN ACHARJYA
KALYAN ACHARJYA on 9 Oct 2018
Edited: KALYAN ACHARJYA on 9 Oct 2018
@Walter Roberson Yes Sir, Thanks for pointing it more clearly.
I am truly grateful to you and @Image Analyst. You people always encourage to learners.

Sign in to comment.

More Answers (0)

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!