如何在MATLAB中保存绘制的图像并保持原始图像大小? [英] How do I save a plotted image and maintain the original image size in MATLAB?

查看:4382
本文介绍了如何在MATLAB中保存绘制的图像并保持原始图像大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要显示图像并在其上绘制一些内容,然后将其保存为与原始图像大小相同的图像。我的MATLAB代码是:

I'd like to show an image and plot something on it and then save it as an image with the same size as the original one. My MATLAB code is:

figH = figure('visible','off');
imshow(I);
hold on;
% plot something
saveas(figH,'1','jpg');
close(figH);

但是生成的图像1.jpg已经保存了图中的非图像区域以及图片。我该如何解决这个问题呢?

But the resulting image "1.jpg" has saved non-image areas in the plot as well as the image. How can I solve this problem?

推荐答案

你的新图片大于原版的原因是因为 SAVEAS 功能保存整个数字窗口,不仅仅是轴的内容(显示图像的位置)。

The reason your new image is bigger than your original is because the SAVEAS function saves the entire figure window, not just the contents of the axes (which is where your image is displayed).

您的问题非常类似于另一个 SO问题,所以我首先要指出这些答案所包含的两个主要选项:

Your question is very similar to another SO question, so I'll first point out the two primary options encompassed by those answers:

  • Modify the raw image data: Your image data is stored in variable I, so you can directly modify the image pixel values in I then save the modified image data using IMWRITE. The ways you can do this are described in my answer and LiorH's answer. This option will work best for simple modifications of the image (like adding a rectangle, as that question was concerned with).

修改数字的方式已保存:您还可以修改图形的保存方式,以便更好地匹配原始图像的尺寸。您可以采用的方法(使用 PRINT )和 GETFRAME 功能代替SAVEAS)描述于来自 Azim 的答案,< a href =https://stackoverflow.com/questions/575475/how-can-i-save-an-altered-image-in-matlab/575895#575895> jacobko ,SCFrench 。如果您使用文本标签,箭头或其他更复杂的绘图对象覆盖图像,则可以执行此选项。

Modify how the figure is saved: You can also modify how you save the figure so that it better matches the dimensions of your original image. The ways you can do this (using the PRINT and GETFRAME functions instead of SAVEAS) are described in the answers from Azim, jacobko, and SCFrench. This option is what you would want to do if you were overlaying the image with text labels, arrows, or other more involved plot objects.

通过保存整个数字来使用第二个选项可能很棘手。具体来说,如果您在一个小窗口(比如700×700像素)中绘制一个大图像(例如1024×1024像素),则可能会丢失图像分辨率。您必须设置图形和轴属性以适应。以下是一个示例解决方案:

Using the second option by saving the entire figure can be tricky. Specifically, you can lose image resolution if you were plotting a big image (say 1024-by-1024 pixels) in a small window (say 700-by-700 pixels). You would have to set the figure and axes properties to accommodate. Here's an example solution:

I = imread('peppers.png');      %# Load a sample image
imshow(I);                      %# Display it
[r,c,d] = size(I);              %# Get the image size
set(gca,'Units','normalized','Position',[0 0 1 1]);  %# Modify axes size
set(gcf,'Units','pixels','Position',[200 200 c r]);  %# Modify figure size
hold on;
plot(100,100,'r*');             %# Plot something over the image
f = getframe(gcf);              %# Capture the current window
imwrite(f.cdata,'image2.jpg');  %# Save the frame data

输出图像 image2.jpg 上应该有一个红色的星号,并且应该与输入图像具有相同的尺寸。

The output image image2.jpg should have a red asterisk on it and should have the same dimensions as the input image.

这篇关于如何在MATLAB中保存绘制的图像并保持原始图像大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆