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

查看:260
本文介绍了如何在 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).

修改图形的保存方式:您还可以修改图形的保存方式,使其更好地匹配原始图像的尺寸.您可以这样做的方法(使用 PRINTGETFRAME 函数而不是 SAVEAS)在来自 Azim 的答案,jacobkoSCFrench.如果您使用文本标签、箭头或其他更复杂的绘图对象覆盖图像,则您需要使用此选项.

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 x 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天全站免登陆