2016-06-09 84 views
0

我的圖像陣列:C_filled = 256x256x3270更新用新值的結構體在for循環中

我想要做的是計算每個圖像的質心,並存儲對應於每個「片」每個質心和/或圖像成陣列。然而,當我嘗試更新的陣列,像一個普通的數組,我得到這個錯誤:

"Undefined operator '+' for input arguments of type 'struct'." 

我有以下代碼:

for i=1:3270; 

cen(i) = regionprops(C_filled(:,:,i),'centroid'); 

centroids = cat(1, cen.Centroid);% convert the cen struct into a regular array. 

cen(i+1) = cen(i) + 1; <- this is the problem line 

end 

如何更新陣列存儲每個新心?

在此先感謝。

回答

0

這是因爲regionprops(即cen(i))的輸出是一個結構,它嘗試添加值1。但既然你嘗試將值添加到結構,而不是它的領域之一,它失敗。假設每個圖像可以包含多個對象(並因此包含質心),最好(我認爲)將它們的座標存儲到單元格數組中,其中每個單元格可以具有不同的大小,這與數值數組相反。如果每個圖像中的對象數量完全相同,則可以使用數字數組。

如果我們看一下「電池陣列」選項的代碼:

%// Initialize cell array to store centroid coordinates (1st row) and their number (2nd row) 
centroids_cell = cell(2,3270); 

for i=1:3270; 

%// No need to index cen...saves memory 
cen = regionprops(C_filled(:,:,i),'centroid'); 

centroids_cell{1,i} = cat(1,cen.Centroid);  
centroids_cell{2,i} = numel(cen.Centroid); 

end 

,就是這樣。您可以使用以下表示法訪問任何圖像的質心座標:centroids_cell{some index}

+0

謝謝!完美地工作,有沒有辦法將一個1-3270的額外列添加到質心,例如[x,y,z],因爲這對應於所討論的對象的長度。 – Idrawthings

+0

是的,請看我編輯 –

+0

嗯,所有的索引值似乎是2,我也在尋找3x3270類型的數組,類似於3D座標系統。基本上我會有[x,y]質心座標,添加1:3270的額外矩陣。 – Idrawthings