【问题标题】:How do i store sequence of 2D matrices into a 3D array in Matlab?如何在 Matlab 中将 2D 矩阵序列存储到 3D 数组中?
【发布时间】:2016-09-27 01:13:52
【问题描述】:

我有这些系列的 2D CT 图像,我已经能够使用“imread”将它们读入 Matlab。然而,问题是我需要将图像作为单个 3D 矩阵读入,而不是几个 2D 矩阵的堆栈。我已经意识到可以将 2D 层的数量存储为第 3 维,但我不知道如何做到这一点,因为我仍然是一个学习者。 我在二维堆栈中读取的代码如下:

a = dir('*.tif');                   

for i = 1: numel(a)
     b           = imread(a(i).name);    %read in the image              
     b_threshold = graythresh(b);        %apply threshold            
     b_binary    = im2bw(b, b_threshold);   %binarize image

     [m, n]      = size(b);              %compute the size of the matrix
     phi(i)      = ((m*n) - sum((b_binary(:))))/(m*n);   %compute the fraction of pore pixels in the image
     phi(:,i)    = phi(i);               %store each of the above result
end

我只添加了一张图片,尽管需要其中几张。然而,人们可以轻松地复制图像以创建一堆 2D 图像。但是,要使代码正常工作,以数字顺序重命名它们很重要。pore_image 欢迎任何帮助/建议/想法。谢谢!

【问题讨论】:

  • 我想记住(因为您是新手)您可以接受其中一个答案。所以请这样做,如果你欣赏他们中的一个

标签: arrays matlab image-processing matrix 3d


【解决方案1】:

您可以简单地使用i 作为索引沿第三维进行分配

stack_of_images(:,:,i) = b_binary

【讨论】:

    【解决方案2】:

    嗯,第一个建议是尽量不要在 matlab 中使用变量 ij,因为它们是保留的(看看 herehere)。 之后取决于您要存储 2D 图像的维度:

    1. 如果您想沿第一维存储图像,只需使用以下代码:

      a = dir('*.tif');                   
      
      for ii = 1: numel(a)
          b           = imread(a(ii).name);    %read in the image              
          b_threshold = graythresh(b);        %apply threshold            
          b_binary    = im2bw(b, b_threshold);   %binarize image
      
          [m, n]      = size(b);              %compute the size of the matrix
          phi(ii)      = ((m*n) - sum((b_binary(:))))/(m*n);   %compute the fraction of pore pixels in the image
          phi(:,ii)    = phi(ii);               %store each of the above result
          matrix_3D_images(ii,:,:)=b_binary;   %adding a new layer
      end
      

    如果您想沿其他维度存储图像,这很容易做到:只需更改“指针”ii 的位置即可:

    1. matrix_3D_images(:,ii,:)=b_binary;
    2. matrix_3D_images(:,:,ii)=b_binary;

    【讨论】:

      猜你喜欢
      • 2015-02-04
      • 2021-07-31
      • 2017-09-23
      • 1970-01-01
      • 2012-01-31
      • 1970-01-01
      • 2021-08-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多