本文介绍了如何在MATLAB中保存绘制的图像并保持原始图像大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我想要显示图像并在其上绘制一些内容,然后将其保存为与原始图像大小相同的图像。我的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 somethingsaveas(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: 修改原始图像数据:您的图像数据存储在变量 I中,因此您可以直接修改 I 中的图像像素值,然后使用 IMWRITE 。 我的描述了您可以这样做的方法。回答和 LiorH的回答。此选项最适用于图像的简单修改(如添加矩形,因为该问题与此有关)。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 的答案, 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 imageimshow(I); %# Display it[r,c,d] = size(I); %# Get the image sizeset(gca,'Units','normalized','Position',[0 0 1 1]); %# Modify axes sizeset(gcf,'Units','pixels','Position',[200 200 c r]); %# Modify figure sizehold on;plot(100,100,'r*'); %# Plot something over the imagef = getframe(gcf); %# Capture the current windowimwrite(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中保存绘制的图像并保持原始图像大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-30 05:23