Matlab crashes with imwrite in loop

3 次查看(过去 30 天)
I try to create a gif of a function changing over time. Therefore I plot the function and get the individual frames with the getframe function,
Plotting the function and creating a cell with the frames does not seem to be a problem. Only when I enter the loop with the imwrite function there seems to be a problem and the whole MATLAB Program just shuts down.
load('Fovertime.mat'); % loads cell array im
check_flag=0;
first = find(~cellfun(@isempty,im),1);
safename = 'Fovertime.gif';
for i = first:length(im)
[A,map] = rgb2ind(im{i},256);
% Write to the GIF File
if check_flag == 0;
imwrite(A,map,safename,'gif', 'Loopcount',inf, 'DelayTime', 0.2);
check_flag = 1;
else
imwrite(A,map,safename,'gif','WriteMode','append','DelayTime',1/15);
end
end
After crashing a gif file with roughly 25% of the full gif has been saved.
  2 个评论
Theo Husby
Theo Husby 2023-3-22
I have the same problem. It is intermittent in my case, sometimes running to completion, and other times running partway through the iterations of my loop. I'm also running 2021a.
Anton Semechko
Anton Semechko 2024-7-4
I just came across the same problem while generaing .gifs and saving them to a netwrok. I have never seen this issue while writing locally, so I suspect it might have something to do with the function being unable to acces the image file data while writing (?). Maybe the MathWorks wizzards can look into this??? What is bizzare is that Matlab just shuts down without any warning.

请先登录,再进行评论。

回答(2 个)

Pranesh
Pranesh 2022-7-12
Hi ,
To my understanding your function stops execution without fully iterating through the loop.
A possible work around for this would be to remove the semicolon (;) in your first if statement.
if check_flag == 0
imwrite(A,map,safename,'gif', 'Loopcount',inf, 'DelayTime', 0.2);
check_flag = 1;
To get more information about if statements refer to this link.
  1 个评论
Churchkey
Churchkey 2022-7-12
Yes, the function stops without fully iterating but the MATLAB Program as a whole just shuts down during the iteration.
Send the script to two colleagues and it was running perfectly fine on their PCs

请先登录,再进行评论。


Thomas Bielser
Thomas Bielser 2023-4-25
We are facing the same problem: Matlab crashes at different unpredicted places when writing animated gif images with the imwrite-command in a loop. Writing "jpg"-format works fine. A wait command in the loop is not practical and brings only moderate improvement. This error occurs on all our computers, both on internal drives and network drives. Here is our code for testing purposes:
% clear workspace
clear all
% image properties
image_x = 128;
image_y = 128;
image_n = 1000;
image_delay_s = 0.1;
% number of colors
color_n = 256;
% generate colormap
color_map = jet( color_n );
% generate image dataset
image = randi( [ 0, 255 ], [ image_x, image_y, image_n ], 'uint8' );
% initialize counter
loop_counter = 1;
% forever
while( 1 )
% output comment
disp( strcat( sprintf( "%04d", loop_counter ), ". iteration" ) );
% over all images
for image_counter = 1:image_n
% extract image
current_image = image( :, :, image_counter );
% first image?
if ( image_counter == 1 )
% write initial image
imwrite( current_image, color_map, strcat( "W:\image_", sprintf( "%04d", loop_counter ), ".gif" ), 'Loopcount', inf, 'DelayTime', image_delay_s );
else
% append further images
imwrite( current_image, color_map, strcat( "W:\image_", sprintf( "%04d", loop_counter ), ".gif" ), 'WriteMode', 'append', 'DelayTime', image_delay_s );
end
% image_counter
end
% increase loop counter
loop_counter = loop_counter + 1;
end
  3 个评论
Thomas Bielser
Thomas Bielser 2023-9-22
Thanks for your reply.
As mentioned, in my case adding a wait command in the loop (up to the range of seconds) brought only moderate improvement apart from not beeing practical if you have to write a very large number of frames. My gut feeling tells me that it is a fundamental problem in the synchronisation of the writing process of such frames.
Anton Semechko
Anton Semechko 2024-7-4
I agree with Thomas. Pausing can cause significant overhead when .gifs have >20 frames and you need to generate 100+ .gifs. But if its the only option available, I will take it.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by