如何在 MATLAB 中保存更改的图像? [英] How can I save an altered image in MATLAB?
问题描述
我想将图像读入 MATLAB,在其上绘制一个矩形,然后保存图像.
I want to read an image into MATLAB, draw a rectangle on it, and then save the image.
另外,我只是在学习 MATLAB——请保持温和.看起来应该很简单,但是我好像做不到.
Also, I'm just learning MATLAB--please be gentle. It seems like it should be simple, but I can't seem to do it.
im = imread('image.tif');
imshow(im);
rectangle('Position', [100, 100, 10, 10]);
imwrite(im, 'image2.tif');
即使我可以看到图像上的矩形,保存的图像也不会显示矩形.如何保存图像并显示矩形?
Even though I can see the rectangle on the image, the saved image does not display the rectangle. How can I save the image and have the rectangle show up?
FWIW,我已经尝试过 saveas()
,但这给了我一个巨大的形象.有没有办法使用 saveas()
并使保存的图像大小正确?
FWIW, I've already tried saveas()
, but that gives me a HUGE image. Is there a way to use saveas()
and make the saved image the correct size?
推荐答案
实际上有 The MathWorks 站点上的一个错误 关于这个问题.太糟糕了,他们没有说出真正的答案(恕我直言,将尺子举到显示器上并不是真正的解决方案).
There's actually a bug at The MathWorks site about this issue. Too bad they don't spell out a real answer (as, IMHO, holding up a ruler to your monitor is not a real solution).
使用print
命令,您必须手动更改-r
参数,直到保存图像的大小与输入图像的大小匹配.-r
参数指定保存图像的 DPI.由于大多数屏幕具有不同的 DPI,因此没有一刀切的解决方案.
Using the print
command, you must manually change the -r
parameter until the size of the saved image matches the size of the input image. The -r
parameter specifies the DPI of the saved image. Since most screens have different DPIs, there's no one-size-fits-all solution.
im = imread('image.tif');
f = figure, imshow(im, 'Border', 'tight');
rectangle('Position', [100, 100, 10, 10]);
print(f, '-r80', '-dtiff', 'image2.tif');
使用上面的代码,调整 -r
参数直到它看起来正确,然后瞧!
Use the code above, tweak the -r
parameter until it looks right, and voilà!
这篇关于如何在 MATLAB 中保存更改的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!