matlab 使用 contourf 或 scatter 绘制动图输出成视频,避免帧与帧之间重合

一般的读取场的每个时刻的状态,然后输出成视频的 matlab 代码类似这样

video_name = 'zeta_video';
obj = VideoWriter(video_name);
obj.FrameRate = 10;
open(obj);

T_STEP = 1.8e-3;
MAX_STEP = 50000;
OUTPUT_GAP = 500;

x = linspace(0, 8, 513);
y = linspace(0, 8, 513);
[X,Y] = meshgrid(x,y);

levels = linspace(-15, 15, 100);

t_len = MAX_STEP/OUTPUT_GAP;

n = 0:OUTPUT_GAP:MAX_STEP-OUTPUT_GAP;

nImages = length(n);
fig = figure;
%Draw multiple images to display and store each image into a cell array%
for idx = 1:nImages
    zeta = csvread(sprintf('./zeta/zeta_%d.csv', n(idx)));
    [C,h] = contourf(X, Y, zeta', levels);
    clim([-15 15]);
    xlim([1 3]);
    ylim([3 5]);
    colorbar;
    set(h,'LineColor','none');
    title(sprintf('zeta t = %.2fs', n(idx)*T_STEP));
    drawnow
    frame = getframe(fig); 
    writeVideo(obj,frame);
end

close(obj);

后来我想用 scatter 画的时候,发现帧与帧之间的图像重合了

video_name = 'p_video';
obj = VideoWriter(video_name);
obj.FrameRate = 20;
open(obj);

T_STEP = 1.8e-3;
MAX_STEP = 50000;
OUTPUT_GAP = 200;

x = linspace(0, 8, 512);
y = linspace(0, 8, 512);
[X,Y] = meshgrid(x,y);

levels = linspace(-0.001, 0.001, 100);

t_len = MAX_STEP/OUTPUT_GAP;

n = OUTPUT_GAP:OUTPUT_GAP:MAX_STEP-OUTPUT_GAP;

nImages = length(n);
fig = figure;
%Draw multiple images to display and store each image into a cell array%
for idx = 1:nImages
    p = csvread(sprintf('./p/p_%d.csv', n(idx)));
    p = p';

    ax = axes();


    scatter(X(:), Y(:), [], p(:), 'filled');

    colormap(ax, jet(numel(levels)-1)); % set colormap
    caxis(levels([1,end]));             % set color range
    cb = colorbar();
    
    xlim([1 3]);
    ylim([3 5]);

    title(sprintf('zeta t = %.2fs', n(idx)*T_STEP));
    drawnow
    frame = getframe(fig); 
    writeVideo(obj,frame);
end\

close(obj);

最后发现是我在循环里面创建 axes 的缘故

改成

ax = axes();

%Draw multiple images to display and store each image into a cell array%
for idx = 1:nImages
    ...
end

就好了

相关推荐

  1. Android Glide 获取的第一

    2024-03-14 06:12:01       33 阅读
  2. 提取视频光流并写入视频

    2024-03-14 06:12:01       60 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-14 06:12:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-14 06:12:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-14 06:12:01       82 阅读
  4. Python语言-面向对象

    2024-03-14 06:12:01       91 阅读

热门阅读

  1. js下载svg文件和预览svg文件

    2024-03-14 06:12:01       38 阅读
  2. STM32学习和实践笔记(2): STM32的学习方法

    2024-03-14 06:12:01       39 阅读
  3. Ubuntu22.04 MySQL8.0安装教程

    2024-03-14 06:12:01       42 阅读
  4. C#中的线程和Apartment状态

    2024-03-14 06:12:01       43 阅读