在 for 循环中使用新值更新结构

Updating a struct with new values in a for loop

我的图像数组:C_filled = 256x256x3270

我想做的是计算每个图像的质心,并将每个'slice'/图像对应的每个质心存储到一个数组中。但是,当我尝试像更新常规数组一样更新数组时,出现此错误:

"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

如何更新数组以存储每个新质心?

提前致谢。

那是因为 regionprops(即 cen(i))的输出是一个结构,您尝试将值 1 添加到其中。但是由于您尝试将值添加到结构而不是它的领域之一,它失败了。

假设每个图像可以包含多个对象(因此包含质心),最好(我认为)将它们的坐标存储到一个元胞数组中,其中每个元胞可以具有不同的大小,这与数字相反大批。如果每个图像中的对象数量完全相同,则可以使用数字数组。

如果我们用代码查看 "cell array" 选项:

%// 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}.