如何在MATLAB中保存更改的图像?

时间:2009-02-22 19:03:49

标签: matlab file-io image-manipulation

我想将图像读入MATLAB,在其上绘制一个矩形,然后保存图像。

另外,我只是在学习MATLAB - 请保持温和。看起来它应该很简单,但我似乎无法做到。

im = imread('image.tif');
imshow(im);
rectangle('Position', [100, 100, 10, 10]);
imwrite(im, 'image2.tif');

即使我可以在图像上看到矩形,保存的图像也不会显示矩形。如何保存图像并显示矩形?

FWIW,我已经尝试了saveas(),但这给了我一个巨大的形象。有没有办法使用saveas()并使保存的图像大小正确?

8 个答案:

答案 0 :(得分:19)

矩形没有显示在保存的图像中的原因是因为您没有修改存储图像数据的变量im。矩形只是在绘图图像上显示的绘图对象。您必须自己修改图像数据。

通常,读入MATLAB的图像被加载为N×M×3矩阵(即,每个像素具有RGB(红 - 绿 - 蓝)值的N×M像素图像)。通常,图像数据是uint8数据类型,因此RGB值的范围为0到255.如果要更改给定像素的RGB值,可以执行以下操作:

im = imread('test.jpg');  % Load a jpeg image
im(1,1,1) = 255;  % Change the red value for the first pixel
im(1,1,2) = 0;    % Change the green value for the first pixel
im(1,1,3) = 0;    % Change the blue value for the first pixel
imwrite(im,'new.jpeg');  % Save modified image

有多种方法可以一次修改多个像素(即矩形区域),这需要您研究如何索引到multidimensional arrays。有关如何将不同类型的图像读入MATLAB的更多细节(即truecolorindexed),我会查看imread的文档。

答案 1 :(得分:13)

对于顶部的问题,matlab提供了一个非常简单的解决方案:

% you so far

im = imread('image.tif');
imshow(im);
rectangle('Position', [100, 100, 10, 10]);

% now you use "getframe" and "frame2im"

f = getframe(gca);
im = frame2im(f);

imwrite(im,'image2.tif');

当我在图像上画了一个矩形并试图保存它时,这对我很有用。如果您想继续使用它,只需添加

即可
imread('image2.tif');

并继续使用它:)

问候,劳拉

答案 2 :(得分:10)

关于这个问题,实际上a bug at The MathWorks site。太糟糕了,他们没有说出真正的答案(因为,恕我直言,为你的显示器举起一把尺子并不是一个真正的解决方案)。

使用print命令,您必须手动更改-r参数,直到保存的图像大小与输入图像的大小相匹配。 -r参数指定已保存图像的DPI。由于大多数屏幕具有不同的DPI,因此没有一种通用的解决方案。

im = imread('image.tif');
f = figure, imshow(im, 'Border', 'tight');
rectangle('Position', [100, 100, 10, 10]);
print(f, '-r80', '-dtiff', 'image2.tif');

使用上面的代码,调整-r参数,直到它看起来正确,然后vo!

答案 3 :(得分:7)

跟随jacobko回答。设置数字 paperposition paperunits 属性以及轴单位位置属性通常可以获得所需的结果而无需调整分辨率。所以,

>> im = imread('image.tif');
>> f = figure, imshow(im);
>> r=rectangle('Position',[100, 100,10,10]);
>> set(r,'edgecolor','b') % change the color of the rectangle to blue
>> set(f,'units','centimeters','position',[1 1 2.5 2.5]) % set the screen size and position
>> set(f,'paperunits','centimeters','paperposition',[1 1 2.5 2.5]) % set size and position for printing
>> set(gca,'units','normalized','position',[0 0 1 1]) % make sure axis fills entire figure
>> print(f, '-r80','-dtiff','image2.tif')

输出图像image2.tif现在为2.5厘米×2.5厘米,重新制作80dpi,没有围绕轴的边框。

答案 4 :(得分:3)

如果要保存即时消息,必须先修改其值。 我不熟悉矩形函数, 但你可以做以下(蛮力):

im = imread('image.tif');
im(100:110,100)=0;
im(100:110,110)=0;
im(100,100:110)=0;
im(110,100:110)=0;
imshow(im);
imwrite(im, 'image2.tif');

请注意,上面的代码适用于灰度图像,如果您的图像是RGB图像,则需要执行以下操作:

 im(100:110,100,:)=0;
 ....

答案 5 :(得分:2)

您可以使用getframe从图窗口中抓取修改后的图像。我认为你可以将getframe返回的结构的cdata和colormap字段分别传递给imwrite作为图像及其颜色图。

答案 6 :(得分:2)

[f,p] = uigetfile('*.*');
I = imread([p,f]);
imwrite(I,'img12.tif');%

我们可以提供用于保存图像的任何名称

自动保存在您的文件夹中,您可以浏览任何图像。

答案 7 :(得分:1)

close all; clear; clc;

r = 240 ; c = 320;

fig = figure('Visible', 'off');
imshow( zeros(r,c) );
hold on;
plot([c-fix(c/2),c-fix(c/2)],[r-fix(r/2),r-fix(r/2)],'r*', 'MarkerSize', 10 );

% Sets position and size of figure on the screen
set(fig, 'Units', 'pixels', 'position', [100 100 c r] ); 

% Sets axes to fill the figure space
set(gca, 'Units', 'pixels', 'position', [0 0 c+1 r+1 ]);

% Sets print properties; Looks like 1 pixel = (3/4)th of a point
set(fig, 'paperunits', 'points', 'papersize', [fix((c-1)*(3/4))+1 fix((r-1)*(3/4))+1]);
set(fig, 'paperunits', 'normalized', 'paperposition', [0 0 1 1]);

print( fig, sprintf('-r%d', ceil(72*(4/3))), '-dpng', 'image.png' ); 


im = imread( 'image.png');
figure; imshow(im);