【问题标题】:Inputting images into array or matrix elements in MATLAB在 MATLAB 中将图像输入到数组或矩阵元素中
【发布时间】:2013-08-12 15:40:24
【问题描述】:

我试图让用户在 MATLAB 中使用以下命令从文件夹中选择图像:

uigetfile('*.tiff','Select the image files')

我希望将图像写入带有n 元素的数组或矩阵(n 是图像选择循环中选择的图像数量)。

我尝试了多种不同的方法,因此我们将不胜感激。 非常感谢。

这是我最近的尝试:

function imagereader

x={};
i=1;

response = 1;

while response ~= 0
    [FileName] = uigetfile('*.tiff','Select the image files')
    x{i} = FileName;

    choice = questdlg('Do you wish to select more images?','Image             selection','Yes','No','No');
      switch choice
          case 'Yes'
              response = 1;
              i+1;
          case 'No'
              response = 0;
      end
end

while i >= 1
    image(x{i})
    i-1;
end    

【问题讨论】:

  • 你尝试了哪些不同的方法?

标签: image matlab image-processing


【解决方案1】:

最直接的方法是将图像存储在具有n 单元格的单元格数组中:

filenames = uigetfiles('*.tiff','Select the image files', 'multiselect', 'on' );
n = numel(filenames); % number of files selected
imgs = cell( n , 1 ); % pre-allocate space
for ii=1:n
    imgs{ii} = imread( filenames{ii} );
end

如果您的所有图像尺寸相同,您可以将它们堆叠成 4D(彩色图像)/3D(灰度图像)数组。

【讨论】:

  • 谢谢你。您能否解释一下 ii 部分,以及为什么方括号在单元格(n,1)中包含 1?我不确定我是否跟随。这是我的代码,最后一个循环似乎不起作用。我收到一条错误消息,指出数组必须是数字和/或逻辑...
  • @JonahPeltz 你得到一个错误,因为你的第二个 while 循环中的x{i}char。它只是一个文件名,而不是图像日期。
【解决方案2】:

我修改了你的例子。希望对您有所帮助:

function imagereader

x={};
i=1;

response = 1;

while response ~= 0
    [FileName,PathName] = uigetfile('*.tiff','Select the image files');
     [FileName,PathName]
    x{i} = imread([PathName, FileName]);

    choice = questdlg('Do you wish to select more images?','Image             selection','Yes','No','No');
      switch choice
          case 'Yes'
              response = 1;
              i+1;
          case 'No'
              response = 0;
      end
end

while i >= 1
    figure;
    imshow(x{i});
    i = i-1;
end    

【讨论】:

  • 非常感谢您,Marcin。最后一个问题:当我打印出数组时,只有第一个图像被打印出来。有什么方法可以让我在单独的窗口或图形中看到所有打印出来的图像?最好是网格?再次感谢!
  • @JonahPeltz 只需将figure; 放在imshow 之前。这将在单独的图中创建和显示图像。
  • 奇怪的是,它只打印出一个数字。有没有可能的解释?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多