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;
相关推荐
机器不会学习CL3 小时前
分类预测|基于改进的灰狼IGWO优化支持向量机SVM的数据分类预测matlab程序 改进策略:Cat混沌与高斯变异
支持向量机·matlab·分类
yanyanwenmeng10 小时前
matlab基础
开发语言·算法·matlab
梦想科研社11 小时前
【无人机设计与控制】四旋翼无人机轨迹跟踪及避障Matlab代码
开发语言·matlab·无人机
顶呱呱程序13 小时前
2-100 基于matlab的水果识别
开发语言·matlab·边缘检测·水果识别·特征提取·matlab-gui
youcans_14 小时前
【永磁同步电机(PMSM)】 3. 基于Matlab 的仿真与控制
matlab·电机·pmsm·仿真模型
Sol-itude15 小时前
关于MATLAB计算3维图的向量夹角总是不正确的问题记录
开发语言·matlab·问题解决·向量
不想当个技术宅18 小时前
【图像压缩与重构】基于标准+改进BP神经网络
matlab·bp神经网络·gui·图像压缩
珞瑜·1 天前
Matlab R2024B软件安装教程
开发语言·matlab
机器学习之心1 天前
综合评价 | 基于熵权-变异系数-博弈组合法的综合评价模型(Matlab)
matlab·博弈组合法·综合评价模型·变异系数·熵权
吱吱鼠叔1 天前
MATLAB数据文件读写:1.格式化读写文件
前端·数据库·matlab