ComponentMeasurements[ _ ,"Centroid"] 结果偏移
我需要获取一组具有亚像素精度的二进制图像中的组件的质心(质心)。
Mathematica 8 附带了一个很好的补充:
i = Import@"https://i.sstatic.net/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@"https://i.sstatic.net/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}}
*)
您可以看到这些之间有 0.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 版本可能会修改此行为并破坏结果。
我还可以使用已知图像运行先前的“校准”并计算偏移,因此它会自动更正,但我想首先了解发生了什么。
这是一个错误吗?
关于为什么会发生这种情况有什么想法吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我觉得 ComponentMeasurements 的文档页面包含解决方案:
您正在计算整个像素,
ComponentMeasurements
测量像素位置。在此系统中,左下像素的中心位于 {1/2,1/2}。I feel the doc page for
ComponentMeasurements
contains the solution:You are counting whole pixels and
ComponentMeasurements
measures pixel positions. In this system, the center of the bottom left pixel is at {1/2,1/2}.