【问题标题】:Saving image using MATLAB使用 MATLAB 保存图像
【发布时间】:2020-04-14 03:13:34
【问题描述】:

我在 MATLAB 中编写了一个程序来生成具有不同规格的图像,但每次我更改其中一个规格时,我都必须以不同的名称和路径重新保存图像。所以,我做了一个for循环来改变这些规范,但是我不知道如何让MATLAB用不同的名称和不同的路径保存生成的图像......

如何编写程序让 MATLAB 保存多个生成的具有不同名称和不同路径的图像作为for–loop 的一部分?

【问题讨论】:

    标签: matlab


    【解决方案1】:

    在你的循环末尾添加这样的内容:

    for i = 1:n
      <your loop code>
      file_name=sprintf('%d.jpg',i);  % assuming you are saving image as a .jpg
      imwrite(your_image, file_name);  % or something like this, however you choose to save your image
    end
    

    【讨论】:

      【解决方案2】:

      如果您想保存 JPEG、PNG 等,请查看@AGS 的帖子。如果要保存FIG文件,请使用

      hgsave(gcf, file_name)
      

      而不是 imwrite 行。还有

      print('-djpeg', file_name)  %# for JPEG file (lossy)
      print('-dpng', file_name)   %# for PNG file (lossless)
      

      作为imwrite 的替代品。

      【讨论】:

      • 当使用print时,图像应该首先通过imshow等函数显示。
      【解决方案3】:

      由于我想将循环中的图保存在当前工作目录 (pwd) 的特定文件夹中,因此我修改了命名例程如下:

      for s = 1:10
          for i = 1:10 
          <loop commands>
          end
      end
      
      % prints stimuli to folder created for them
      file_name=sprintf('%s/eb_imgs/%0.3f.tif',pwd,s(i)); % pwd = path of present working  
                                                          % directory and s(i) = the value 
                                                          % of the changing variable
                                                          % that I wanted to document
      
      file_name = /Users/Miriam/Documents/PSYC/eb_imgs/0.700.tif % how my filename appears
      print('-dtiff', '-r300', file_name); % this saves my file to my desired location
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-05-29
        • 2012-08-22
        • 2014-09-28
        • 1970-01-01
        • 1970-01-01
        • 2016-10-31
        • 2014-07-24
        相关资源
        最近更新 更多