Forming Video from frames but getting the error

4 visualizaciones (últimos 30 días)
muhammad choudhry
muhammad choudhry el 23 de Jul. de 2020
Comentada: muhammad choudhry el 23 de Jul. de 2020
Hi,
I am trying to made a video using images (30 fps) but I am getting an error which I am failing to understand or correct it. Both code and error shown below.
Thanks for the help in advance!
Code:
%Read the Images
for m=2:10
images =imread(sprintf('frame%d.jpg',m));%name of files are frame2.jpg till frame190.jpg so on
end
% create the video writer with 30 fps
writerObj = VideoWriter('myVideo.avi');
writerObj.FrameRate = 30;
% open the video writer
open(writerObj);
% write the frames to the video
for u=1:length(images)
% convert the image to a frame
frame = im2frame(images{u});
end
% close the writer object
close(writerObj);
Error:
Brace indexing is not supported for variables of this type.
Error in videowriter (line 16)
frame = im2frame(images{u});

Respuesta aceptada

Walter Roberson
Walter Roberson el 23 de Jul. de 2020
for m=2:10
images =imread(sprintf('frame%d.jpg',m));%name of files are frame2.jpg till frame190.jpg so on
end
That loop overwrites all of images each time.
for m=2:10
images{m-1} =imread(sprintf('frame%d.jpg',m));%name of files are frame2.jpg till frame190.jpg so on
end
  3 comentarios
Walter Roberson
Walter Roberson el 23 de Jul. de 2020
writerObj = VideoWriter('myVideo.avi');
writerObj.FrameRate = 30;
open(writerObj);
for m=2:190
thisimage =imread(sprintf('frame%d.jpg',m));%name of files are frame2.jpg till frame190.jpg so on
writeVideo(writerObj, thisimage);
end
close(writerObj);
im2frame() is intended for converting colormapped images into a structure suitable for writing as "frames", but can also be used for RGB images. You are working with .jpg images, so the two ways that you could end up with a frame that would give that particular error message are:
  1. One of the images was empty, []; Or
  2. One of the images was a true grayscale jpg image. True grayscale jpg images are very uncommon; more than 99% of the time, grayscale jpg images are saved as RGB images that just happen to have the same content for all three color panes. But real grayscale jpg are technically possible. If you happen to have an actual grayscale jpg then chances are that you want the output frame to be grayscale as well. In that situation you should either skip converting to "frame" structure or else you should explicitly pass in a gray colormap when you im2frame()
If you were using an image format such as .png or .tif I would give more weight to the possibility that you have indexed images that you want to write out after being processed through a colormap. However, .jpg is not a suitable file format for images that are indexed.
muhammad choudhry
muhammad choudhry el 23 de Jul. de 2020
Thanks alot for the explanation!
I actually extracted the frames from the video hence did converted to greyscale images then did the backgroud substraction to see the movement of object in the frames. Your code worked and now I can only see the object moving in the video.
I will try to avoid .jpg format. Thanks alot once again!

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by