博客
关于我
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/

    你可能感兴趣的文章
    PROFINET 模拟器使用教程
    查看>>
    Program type already present: android.support.v4.widget.EdgeEffectCompat
    查看>>
    PyTorch中文版官方教程来啦(附下载)
    查看>>
    Progress Kemp LoadMaster 远程命令执行漏洞复现(CVE-2024-1212)
    查看>>
    Project configuration is not up-to-date with pom.xml. Run Maven->Update Project
    查看>>
    Project Euler 15 Lattice paths
    查看>>
    Project Euler 48 Self powers( 大数求余 )
    查看>>
    Project Euler Problem 12: Highly divisible triangular number
    查看>>
    ProjectEuler 2
    查看>>
    projection介绍及EPSG:4326和EPSG:3857的投射转换
    查看>>
    project打开文件时,显示无法识别此文件格式?
    查看>>
    Prometheus + Grafana on Kubernetes部署
    查看>>
    prometheus + grafana进行服务器资源监控
    查看>>
    Prometheus Alertmanager 告警配置详解
    查看>>
    Prometheus Grafana 展示平台
    查看>>
    Prometheus pushgateway使用详解
    查看>>
    Prometheus 云原生 - Prometheus 数据模型、Metrics 指标类型、Exporter 相关
    查看>>
    Prometheus 云原生 - 基于 file_sd、http_sd 实现 Service Discovery
    查看>>
    Prometheus 云原生 - 微服务监控报警系统 (Promethus、Grafana、Node_Exporter)部署、简单使用
    查看>>
    Prometheus 云原生 - 监控 Linux、MySQL、Redis、RabbitMQ、Docker、SpringBoot 3.x
    查看>>