【问题标题】:How can I save an altered image in MATLAB?如何在 MATLAB 中保存更改后的图像?
【发布时间】:2010-10-09 04:42:10
【问题描述】:

我想将一张图片读入 MATLAB,在上面画一个矩形,然后保存图片。

另外,我只是在学习 MATLAB——请轻点。看起来应该很简单,但我似乎做不到。

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

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

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

【问题讨论】:

标签: matlab file-io image-manipulation


【解决方案1】:

矩形未显示在保存的图像中的原因是您没有修改变量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 的文档。

【讨论】:

    【解决方案2】:

    对于上面的问题,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');
    

    并继续使用它:)

    劳拉,您好

    【讨论】:

    • 保存 gca 帧可以解决问题,但不会以原始分辨率保存图像。
    • 输出图像大小与输入图像大小相同,因此这似乎保留了原始图像的分辨率。这是我的解决方案。非常感谢!
    【解决方案3】:

    实际上有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 参数直到它看起来正确,然后瞧!

    【讨论】:

    • 这听起来是个不错的解决方案;只有2个小问题。首先,必须为 -r 参数尝试一系列设置有点烦人。其次,我认为输出图像会包含出现在轴周围的边框,这在某些情况下是不可取的。
    • 我将 imshow() 函数的 'Border' 参数更新为 'tight',因此图像周围的灰色边框不会出现。
    【解决方案4】:

    跟进jacobko 的回答。设置图形 paperpositionpaperunits 属性以及轴 unitsposition 属性通常会给我想要的结果而无需调整分辨率。所以,

    >> 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.5cm x 2.5cm,分辨率为 80dpi,没有围绕轴的边框。

    【讨论】:

    • +1:我想说这是给出的最好的“数字级别”答案。
    【解决方案5】:

    如果要保存im,必须先修改它的值。 我不熟悉矩形功能, 但您可以执行以下操作(蛮力):

    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;
     ....
    

    【讨论】:

      【解决方案6】:

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

      【讨论】:

      • getframe 是一个不错且快速的解决方案。但是,我认为有一个警告。它创建的图像的尺寸将取决于图形大小。如果您在小窗口(例如 700 x 700)中绘制大图像(例如 1024 x 1024),您的图像会丢失一些分辨率。
      【解决方案7】:
      [f,p] = uigetfile('*.*');
      I = imread([p,f]);
      imwrite(I,'img12.tif');%
      

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

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

      【讨论】:

        【解决方案8】:
        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);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-10-19
          • 1970-01-01
          • 2017-08-11
          • 2016-02-13
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多