博客
关于我
matlab2018在图片上添加文字并保存且图片没有白边
阅读量:725 次
发布时间:2019-03-21

本文共 1013 字,大约阅读时间需要 3 分钟。

图片添加文字并保存

步骤如下:

  • 读取图片并显示
  • 在指定位置添加文字
  • 调整图窗去掉白边
  • 捕获并保存图片
  • 第一步是从文件读取图片,然后显示出来,代码如下:

    clear allI = imread('cameraman.tif'); imshow(I);

    接下来是在图片上添加文字。为了放在图片中心,一般建议使用坐标的中心点 [m/2, n/2],这里用简单的位置 [30, 30]

    text = '我加入的文字';% 添加文字J = text('Position',[30 30], 'String', text, 'FontSize', 12, 'Color', 'r');

    为了确保添加的文字位于图片的中央,你需要知道图片的尺寸。

    % 获取图片尺寸[m, n] = size(I);

    以下几种方法可以去掉图窗的白边:

    • 也可以简化为 axis off;来去掉坐标轴,移除白边
    • 或者设置图窗的_position属性为可以隐藏白边的范围
    % 调整图窗位置以去除白边set(gcf, 'Position', [0, 0, m, n]);axis off;  % 去掉坐标轴和白边

    现在捕获图窗并保存图片:

    % 拍摄当前图像f = getframe(figure(1));% 保存图片imwrite(f.cdata, 'newphoto.jpg');

    如果图片有白边,说明图窗范围包括了白边。请结合路径或应用进程调整图窗范围:

    % 如图窗有白边,调整图内容范围h = get(gca, 'children');set(h, 'InitialMagnification', 'fit');

    完整的代码如下:

    clear allI = imread('cameraman.tif');imshow(I);J = text('Position', [m/2, n/2], 'String', '我加入的文字', 'FontSize', 12, 'Color', 'r');set(gcf, 'Position', [0, 0, m, n]);axis off;% 或者使用宽松模式imshow(I);axis('off');axis('tight');f = getframe;imwrite(f.cdata, 'newphoto.jpg');

    图片示例:

  • 添加文字后,图片在图窗中显示,完整保存为 newphoto.jpg
  • 去除白边后的图片
  • 转载地址:http://nrsrz.baihongyu.com/

    你可能感兴趣的文章
    Prometheus 云原生 - 微服务监控报警系统 (Promethus、Grafana、Node_Exporter)部署、简单使用
    查看>>
    Prometheus 云原生 - 监控 Linux、MySQL、Redis、RabbitMQ、Docker、SpringBoot 3.x
    查看>>
    Prometheus 介绍
    查看>>
    Prometheus 安全配置详解
    查看>>
    prometheus 安装node_exporter, node_exporter 安装最新版 普罗米修思安装监控服务器client
    查看>>
    Prometheus 安装部署实战
    查看>>
    prometheus 持久化存储方案
    查看>>
    Prometheus 监控系统企业级实战
    查看>>
    Prometheus 监控系统简介
    查看>>
    Prometheus 配置详解
    查看>>
    Prometheus 采集器使用详解
    查看>>
    Prometheus 黑盒监控实战
    查看>>
    prometheus+alertmanager+grafana监控部署教程
    查看>>
    Prometheus+Grafana构建智能化Kubernetes监控系统实战
    查看>>
    Prometheus+SpringBoot应用监控全过程详解
    查看>>
    Prometheus+SpringBoot应用监控全过程详解
    查看>>
    prometheus安装
    查看>>
    Prometheus实战教程:监控Kafka消息
    查看>>
    Prometheus实战教程:监控mysql数据库
    查看>>
    Prometheus实战教程:监控Nginx状态
    查看>>