Matlab绘制像素风字母颜色及透明度随机变化动画

本文是使用 Matlab 绘制像素风字母颜色及透明度随机变化动画的教程

实现效果

实现代码

如果需要更改为其他字母组合,在下面代码的基础上简单修改就可以使用。

步骤:(1) 定义字母形状;(2) 给出字母组合顺序;(3) 重新运行程序;

Matlab源文件代码也已经上传,下载链接就在文章头部的位置。

matlab 复制代码
% 定义字母的形状
D_single = [
    1 1 1 0 0;
    1 0 0 1 0;
    1 0 0 0 1;
    1 0 0 0 1;
    1 0 0 0 1;
    1 0 0 1 0;
    1 1 1 0 0
];

G_single = [
    0 1 1 1 0;
    1 0 0 0 1;
    1 0 0 0 0;
    1 0 1 1 1;
    1 0 0 0 1;
    1 0 0 0 1;
    0 1 1 1 1
];

O_single = [
    0 1 1 1 0;
    1 0 0 0 1;
    1 0 0 0 1;
    1 0 0 0 1;
    1 0 0 0 1;
    1 0 0 0 1;
    0 1 1 1 0
];

% 存储需要显示的字母形状的单元格数组
letters = {G_single, O_single, O_single, D_single};

% 设置参数
blockSize = 10; % 每个方格的大小
rows = 7;       % 字母'A'的行数
cols = 5;       % 字母'A'的列数
numLetters = length(letters); % 插入的字母数量
extraCols = 10; % 额外增加的背景栅格列数
numFrames = 50; % 动画总帧数
fps = 3;        % 动画帧率
scaleFactor = 5;  % 窗口大小缩放系数,可调整以更好地控制窗口大小
gifFilename = 'animated_letters.gif'; % 输出的GIF文件名

% 定义两个字母之间的间隔
spaceCols = 1;  % 间隔1列

% 计算总列数
totalCols = numLetters * cols + (numLetters-1) * spaceCols + extraCols;  % 增加背景栅格的列数

% 计算左右两侧额外的空列数,以居中字母
leftPadding = floor(extraCols / 2);
rightPadding = extraCols - leftPadding;

% 初始化整个矩阵
A = zeros(rows, totalCols); % 初始化全为0的矩阵

% 在矩阵中插入numLetters个字母,,并居中
% 如果n小于1,退出程序,并打印警告信息
if numLetters < 1
    disp('Warning: n must be greater than or equal to 1.');
    return;
end

for k = 1:numLetters
    singleLetter = letters{k};
    colStart = leftPadding + (k - 1) * (cols + spaceCols) + 1;
    colEnd = leftPadding + (k - 1) * (cols + spaceCols) + cols;
    A(:, colStart:colEnd) = singleLetter;
end

% 初始化图形窗口,设置合适的窗口大小
windowWidth = blockSize * totalCols * scaleFactor + 20;  % 绘图窗口宽度
windowHeight = blockSize * rows * scaleFactor + 20;      % 绘图窗口高度
figure('Units', 'pixels', 'Position', [100 100 windowWidth windowHeight]);
axis equal;
hold on;
axis off; % 隐藏坐标轴

% 设置紧凑布局,移除边缘空白
set(gca, 'Units', 'normalized', 'Position', [0 0 1 1]);

% 设置浅灰色背景
set(gca, 'Color', [0.9 0.9 0.9]);  % 0.9是浅灰色

% 绘制浅色栅格背景
lightGrayColor = [0.8, 0.8, 0.8];  % 浅灰色
for i = 0:rows
    y = i * blockSize;
    plot([0, totalCols * blockSize], [y, y], 'Color', lightGrayColor, 'LineWidth', 0.5); % 绘制横线
end
for j = 0:totalCols
    x = j * blockSize;
    plot([x, x], [0, rows * blockSize], 'Color', lightGrayColor, 'LineWidth', 0.5); % 绘制竖线
end

% 创建矩形句柄数组
hRect = zeros(rows, totalCols);

% 初始化矩形句柄和位置
for i = 1:rows
    for j = 1:totalCols
        if A(i, j) == 1
            x = (j - 1) * blockSize;
            y = (rows - i) * blockSize;
            hRect(i, j) = rectangle('Position', [x, y, blockSize, blockSize], ...
                                    'EdgeColor', 'w', 'LineWidth', 1);
        end
    end
end

% 动画循环
for frame = 1:numFrames
    for i = 1:rows
        for j = 1:totalCols
            if A(i, j) == 1
                % 随机生成新的颜色和透明度
                randomColor = rand(1, 3);
                randomAlpha = 0.5+ (1-0.5)*rand(); % 透明度在(0.5-1)之间变化
                % 更新方块的颜色和透明度
                set(hRect(i, j), 'FaceColor', randomColor,'FaceAlpha', randomAlpha);
            end
        end
    end
    
    % 捕获当前帧的图像
    frameImage = getframe(gcf);
    im = frame2im(frameImage); % 将帧转换为图像
    
    % 将图像写入GIF文件
    % 使用64种颜色而不是256种,以减小文件大小
    [imind, cm] = rgb2ind(im, 64);
    if frame == 1
        imwrite(imind, cm, gifFilename, 'gif', 'Loopcount', inf, 'DelayTime', 1/fps);
    else
        imwrite(imind, cm, gifFilename, 'gif', 'WriteMode', 'append', 'DelayTime', 1/fps);
    end
    
    % pause(1/fps);  % 控制matlab中显示的帧率,和输出的gif图像一致
end

hold off;
相关推荐
2zcode20 小时前
基于MATLAB元胞自动机(CA)的AZ80A镁合金动态再结晶(DRX)过程模拟
开发语言·matlab·动态再结晶
jiushiapwojdap1 天前
LU分解法求解线性方程组Matlab实现
数据结构·其他·算法·matlab
MATLAB代码顾问1 天前
改进遗传算法(IGA)求解作业车间调度问题(JSSP)——附MATLAB代码
开发语言·matlab
米饭不加菜1 天前
机器人矩阵运算MATLAB计算
matlab·矩阵·机器人
机器学习之心1 天前
多智能体遗传算法(MAGA)优化最优投影方向的投影寻踪聚类评价,MATLAB代码
matlab·聚类·投影寻踪聚类评价
MATLAB代码顾问1 天前
多种群协同进化算法(MPCE)求解大规模作业车间调度问题——附MATLAB代码
开发语言·算法·matlab
Evand J1 天前
【MATLAB绘图教程】空间误差场图的概念及其应用,附代码
matlab·平面·绘图·定位
alphageek81 天前
Matlab linspace函数完全指南:从基础用法到进阶技巧
开发语言·其他·matlab
xrgs_shz1 天前
MATLAB 纹理特征提取:一文读懂 graycomatrix 与 graycoprops
人工智能·计算机视觉·matlab
2zcode1 天前
基于MATLAB的深度学习工业表面缺陷多分类检测系统设计与实现(GUI界面+数据集+训练代码)
深度学习·matlab·分类