我拥有这些系列的2D CT图像,并且已经能够使用“ imread”将其读取到Matlab中。但是,问题是我需要将图像读入为单个3D矩阵,而不是几个2D矩阵的堆栈。我已经知道可以将2D图层的数量存储为第3维,但是我仍然不知道如何做,因为我仍然是一个学习者。我要在2D堆栈中读取的代码如下:
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任何帮助/建议/想法都受到欢迎。谢谢!
好吧,第一个建议是尝试不要在Matlab中使用变量i
,j
因为在变量中它们是保留的(请在此处和此处查看)。之后,取决于要存储2D图像的尺寸:
如果要沿第一个维度存储图像,请使用以下代码:
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
:
matrix_3D_images(:,ii,:)=b_binary;
或者matrix_3D_images(:,:,ii)=b_binary;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句