Animated GIF contains offset in consecutive timestamps

조회 수: 1(최근 30일)
hmhuang
hmhuang 2022년 1월 12일
편집: DGM 2022년 1월 13일
I have an animated GIF that contains offsets from timestamp to timestamp, which is not desired.
The relevant code snippet is as follows:
for iter = 1 : 100
% Create an animated GIF image to illustrate the evolution of
% material distribution of optimized design
frame = getframe(axMatDist);
im = frame2im(frame);
[imind, cm] = rgb2ind(im, 256);
if (iter == 1)
imwrite(imind, cm, 'myanimation.gif', 'gif', 'Loopcount', inf);
else
imwrite(imind, cm, 'myanimation.gif', 'gif', 'WriteMode', 'append');
end
end
What could be the problem?

답변(1개)

DGM
DGM 2022년 1월 13일
편집: DGM 2022년 1월 13일
To be more precise, the file does not contain any frame offsets. All the frames are positioned at [1 1]. The problem is that the frame sizes differ. This is an issue with the way the figures are drawn and captured.
h = imfinfo('https://www.mathworks.com/matlabcentral/answers/uploaded_files/860820/myanimation.gif');
unique([h.Top])
ans = uint16 1
unique([h.Left])
ans = uint16 1
unique([h.Height])
ans = 1×3
245 248 252
unique([h.Width])
ans = 1×3
491 495 504
Since I don't know how the figure is being drawn, I'll just suggest that it may suffice to explicitly set the axes geometry with xlim() and ylim(). If the axes position keeps changing (e.g. due to tick labels changing), or if the figure size keeps changing, that will change the output size. If so, removing extraneous objects from the figure and explicitly setting object sizes may make things more consistent.
If that's not an option, perhaps the captured images can be resized or padded to a fixed geometry using imresize(), but that's probably the less attractive option, as it's unclear whether there's a scale difference between frames or not. It's also unclear what the correct registration would be.

Community Treasure Hunt

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

Start Hunting!

Translated by