フィルターのクリア

Matlab crashes with imwrite in loop

12 ビュー (過去 30 日間)
Churchkey
Churchkey 2022 年 7 月 12 日
コメント済み: Anton Semechko 2024 年 7 月 4 日 18:41
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 日 18:41
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 日 18:40
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 Center および File ExchangeOrange についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by