2016-06-09 83 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