2014-09-03 80 views
2

我正在處理顯微鏡圖像堆棧。通過堆我的意思是一對夫婦的圖像獲取的一個在另一個的頂部,因爲這很自制圖所示:(對於質量抱歉)其中:以像素爲單位向量化在Matlab中切斷圖像堆棧視圖

H =圖像高度

W =以像素爲單位的圖像寬度

Z =堆棧中的切片數量(即圖像數量)。

enter image description here

我想要做什麼是措施,對於在堆疊的每個「高度」位置,正交「WZ視圖」,即切斷沿着一個矩形視圖諸如圖像上衝一個。現在,我有以下的代碼效果很好:

DummyProjMatrixYZ = zeros(SliceNumber,ImageWidth,ImageHeight,3,'uint16'); 

for h = ImageHeight:-1:1 
    for z = 1:SliceNumber 

     DummyProjMatrixWZ(z,:,h,:) = MovieCell{z}(h,:,:); % MovieCell is a cell array containing the individual frames (i.e. slices in the stack) of dimensions (Height,Width,3). 
    end  
end 

的代碼工作正常,但我通過每一個「高度單位」,這是不是最佳的,在所有的全片有循環。

我的問題是:爲了獲得速度,以前的代碼怎麼會被矢量化,因爲我最終會處理非常大的數據集。我想我有一個腦凍結,不知道如何有效地做到這一點。

+0

1)什麼是MovieCell的大小? ,2)爲什麼它在單元陣列而不是4D矩陣? – bla 2014-09-03 18:32:59

+0

@natan 1)MovieCell的大小爲(1,z),其中z是切片的數量,即通常爲20至200。 2)它在一個單元陣列中,因爲之前的操作要比其中的圖像要簡單得多。但我同意將圖像存儲在4D矩陣中會更有意義。 – 2014-09-03 18:36:24

+0

如果它只是z只是20-200爲什麼不留在循環?你會節省多少時間? – bla 2014-09-03 18:48:29

回答

1

這對我來說基本上看起來像一個indexing problem。看看這個工程爲你 -

%// Get all of the data into SliceNumber*ImageWidth x ImageWidth x 3 numeric 
%// array. No more messing around with cell arrays is needed after this point. 
A = vertcat(MovieCell{:}); 

%// Cocatenate data along dim3 to create a 2D array 
A1 = reshape(permute(A,[1 3 2]),size(A,1)*size(A,3),[]); 

%// Cut A1 after every ImageHeight rows to form a 3D array 
A2 = permute(reshape(A1,ImageHeight,size(A1,1)/ImageHeight,[]),[1 3 2]); 

%// Cut A2's dim3 into SliceNumber x 3 to create a 4D array 
A3 = reshape(A2,ImageHeight,ImageWidth,SliceNumber,3); 

%// Change the dimensions to match with original DummyProjMatrixWZ's 
DummyProjMatrixWZ = permute(A3,[3 2 1 4]); 

較短的版本 -

%// Get all of the data into SliceNumber*ImageWidth x ImageWidth x Ch numeric 
%// array. No more messing around with cell arrays is needed after this point. 
A = vertcat(MovieCell{:}); 

%// Cut A at every H rows and resize to H x W x Slice*Ch to form a 3D array 
A2 = permute(reshape(permute(A,[1 3 2]),ImageHeight,[],ImageWidth),[1 3 2]); 

%// Cut A2's dim3 into Slice x Ch to create a 4D array and rearrange dims 
DPrjMatWZ = permute(reshape(A2,ImageHeight,ImageWidth,SliceNumber,[]),[3 2 1 4]); 

這裏,Ch表示使用的信道的數量,這是問題的情況下3DPrjMatWZ是最後輸出。

+0

是的,它完美的工作,我的220圖像堆棧的計算時間減少了一半,所以大數據集的好處將是非常重要的,這非常棒! – 2014-09-03 19:11:14

+0

@ Benoit_11非常酷!很高興看到真正的好處! :)你可以嘗試將最後兩行合併成一行,看看它是否有進一步改進。 – Divakar 2014-09-03 19:11:43

+0

是的。我必須承認,我希望你能看看我的問題,並提出一些矢量化的魔力,謝謝! – 2014-09-03 19:13:17