Forming Video from frames but getting the error

조회 수: 2 (최근 30일)
muhammad choudhry
muhammad choudhry 2020년 7월 23일
댓글: muhammad choudhry 2020년 7월 23일
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