2016-09-27 1048 views
0

我有這些系列的二維CT圖像,我已經能夠使用「imread」將它們讀入Matlab中。然而,問題是我需要將圖像讀入爲單個3D矩陣,而不是一堆2D矩陣。我已經意識到可以將第二維圖層的數量作爲第三維來存儲,但我不知道如何做到這一點,因爲我仍然是學習者。 代碼我有用於在2D堆棧閱讀如下:如何在Matlab中將二維矩陣序列存儲到三維數組中?

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 歡迎任何幫助/建議/想法。謝謝!

+1

我要記住(因爲你是新的),您可以接受的答案之一。所以請做到這一點,如果你欣賞他們 – Leos313

回答

0

那麼第三個維度分配,第一個建議是儘量不要使用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;
+1

感謝你們倆:Suever和Leos ......它的作品! – oma11

1

你可以簡單地沿着使用i作爲索引

stack_of_images(:,:,i) = b_binary