Forming Video from frames but getting the error

2 次查看(过去 30 天)
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});

采纳的回答

Walter Roberson
Walter Roberson 2020-7-23
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 个评论
Walter Roberson
Walter Roberson 2020-7-23
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 2020-7-23
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!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Convert Image Type 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by