Creating .avi video out of images but the resulted video cannot be open nor replay or ends with "out of memory" error.
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I want to create a video based on several jpg or jpeg pictures, here is the code:
clear all;
clf;
nname = 'name';
extprn = ['.jpg'];
images{1} = imread('namel010.jpg');
images{2} = imread('name020.jpg');
images{3} = imread('name030.jpg');
images{4} = imread('name040.jpg');
images{5} = imread('name050.jpg');
images{6} = imread('name060.jpg');
images{7} = imread('name070.jpg');
images{8} = imread('name080.jpg');
images{9} = imread('name090.jpg');
for i = 1:1:60
nameprn = [nname,num2str(90+10*i) extprn];
images{9+i} = imread(nameprn);
end
% create the video writer
writerObj = VideoWriter('myVideo.avi');
writerObj.FrameRate = 2;
% set the seconds per image
for i = 1:2: 1: 69
secsPerImage(i) = 1;
end
% 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});
for v=1:secsPerImage(u)
writeVideo(writerObj, frame);
end
end
% close the writer object
close(writerObj);
However, when I run the code, I either get an error, saying:
% Error using VideoWriter/writeVideo (line 414)
% Out of memory.
%
% Error in video (line 39)
% writeVideo(writerObj, frame);
%
% Related documentation
or it creates a avi file with somehow a content (size of file is not zero) which replays anything. It says that the file is broken. All figures has the exact same size, size of arrays are the same, the computer has enough free storage. This code was used successfully previous from other people but we don't know why it does not work here. There is no other error observed, we already search the community for related questions. Can someone help me to get this video/codes successful?
7 commentaires
Walter Roberson
le 31 Oct 2023
When you call VideoWriter() you can pass in a second parameter which is a "profile". If you are creating an AVI then you could try 'Uncompressed AVI' . Uncompressed video should need only trivial buffering, whereas the default is 'Motion JPEG AVI' which would need to store several frames in order to optimize the output.
Réponses (1)
Siraj
le 31 Oct 2023
Hi!
It appears that you're attempting to generate a video using “VideoWriter,” but you're encountering issues, such as running out of memory or ending up with a corrupted .avi file.
Since I lack information about the dimensions and size of the images, you're using to compose the video, I am assuming they might be substantial, which could be causing the error. As a potential solution, you could consider resizing the images to a smaller dimension using the "imresize" function before proceeding with the video creation process. Additionally, you may explore the option of decreasing the video quality by adjusting the "Quality" property within the "VideoWriter" object.
The code you provided seems to be working well without any issues when tested with smaller input data. I have attached the code used to generate the necessary images and the code that creates a video from those images for your reference.
Code to create images as per your naming convention.
% Define the base name and extension
nname = 'name';
extprn = '.jpg';
% Specify the number of images to create
numImages = 69;
% Loop to create and save images
for i = 1:numImages
% Generate the filename based on the naming convention
if i <= 9
filename = sprintf('%s%03d%s', nname, 10 * i, extprn);
else
filename = sprintf('%s%03d%s', nname, 90 + 10 * (i - 9), extprn);
end
% Create a sample image (replace this with your image creation code)
% For example, create a simple black image with the image number:
img = zeros(100, 100, 3);
img = insertText(img, [10 10], num2str(i), 'FontSize', 20, 'TextColor', [255, 255, 255]);
% Save the image
imwrite(img, filename);
end
The code that you've provided, with just one minor correction in the for loop.
nname = 'name';
extprn = ['.jpg'];
images{1} = imread('name010.jpg');
images{2} = imread('name020.jpg');
images{3} = imread('name030.jpg');
images{4} = imread('name040.jpg');
images{5} = imread('name050.jpg');
images{6} = imread('name060.jpg');
images{7} = imread('name070.jpg');
images{8} = imread('name080.jpg');
images{9} = imread('name090.jpg');
for i = 1:1:60
nameprn = [nname,num2str(90+10*i) extprn];
images{9+i} = imread(nameprn);
end
% create the video writer
writerObj = VideoWriter('myVideo.avi');
writerObj.FrameRate = 2;
% set the seconds per image
for i = 1:1:69
secsPerImage(i) = 1;
end
% 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});
for v=1:secsPerImage(u)
writeVideo(writerObj, frame);
end
end
% close the writer object
close(writerObj);
The above code successfully generates the video file. However, if the issue continues to persist on your side, you can consider implementing the following workaround.
- Go to MATLAB > Preferences > Workspace and ensure the Maximum array size limit is set to 100%.
- Also, check that the Java Heap Memory is not set to a very large value because that might restrict the amount of memory available to perform computations.
Hope this helps.
0 commentaires
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!