ComponentMeasurements [_,“Centroid”]结果偏移

时间:2011-06-24 16:50:56

标签: image-processing mathematica-8 wolfram-mathematica

我需要以亚像素精度获得一组二进制图像中的组件的质心(Centroid)。

Mathematica 8附带了一个很好的补充:

i =  Import@"http://i.stack.imgur.com/2pxrN.png";  
m1 = ComponentMeasurements[MorphologicalComponents[i], "Centroid"] /. 
                                                                Rule[_, x_] -> x
(*
-> {{403.229, 453.551}, {660.404, 371.383}, {114.389, 434.646}, {295.5, 206.}}
*)

但是,当这些结果显示与其他地方进行的其他计算有些不一致时,我遇到了一些麻烦。

所以我自己动手,也许不好看:

i = Import@"http://i.stack.imgur.com/2pxrN.png";
f[i_] := N@{#[[2]], ImageDimensions[i][[2]] - #[[1]]} & /@
         ( Mean /@
             Function[x, Map[
                Position[x, #, 2] &,
                Complement[Union@Flatten[x], {0}]]]
             [MorphologicalComponents[i]]);
f[i]
Show[i, Graphics[{Red, Disk[#, 10] & /@ f[i]}]]
(*
-> {{403.729, 453.051}, {660.904, 370.883}, {114.889, 434.146}, {296., 205.5}}
*)

enter image description here

您可以看到这些结果之间存在.5偏差:

Thread[Subtract[m1, f[i]]]
(*
-> {{-0.5, -0.5, -0.5, -0.5}, {0.5, 0.5, 0.5, 0.5}}
*)

起初我认为问题与图像尺寸是偶数还是奇数有关,但事实并非如此。

我更喜欢使用ComponentMeasurements[ ..,"Centroid"]并更正结果,但我担心未来的Mma版本可能会修改此行为并破坏结果。

我还可以使用已知图像运行之前的“校准”并计算偏移量,因此它会自动纠正,但我想先了解发生了什么。

这是一个错误吗? 关于为什么会发生这种情况的任何想法?

1 个答案:

答案 0 :(得分:6)

我认为ComponentMeasurements的文档页面包含解决方案:

  

位置,面积和长度   测量在标准中进行   图像坐标系所在的位置   {0,0}对应于左下角   角,x从0到宽度,和y   从0到高度。

您计算整个像素,ComponentMeasurements测量像素位置。在这个系统中,左下角像素的中心位于{1 / 2,1 / 2}。

相关问题