ComponentMeasurements[ _ ,"Centroid"] 结果偏移

发布于 2024-11-16 22:55:52 字数 1381 浏览 7 评论 0 原文

我需要获取一组具有亚像素精度的二进制图像中的组件的质心(质心)。

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 版本可能会修改此行为并破坏结果。

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

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

I need to get the Center of Mass (Centroid) for components in a set of binary images with subpixel accuracy.

Mathematica 8 comes with a nice addition:

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.}}
*)

But I went through some troubles when those results showed some inconsistencies with other calculations done elsewhere.

So I rolled my own, perhaps not nice:

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}}
*)

enter image description here

You can see that there is a .5 offset between these results:

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

At first I thought that the problem was related with the image dimensions being even or odd, but that is not the case.

I'd prefer using ComponentMeasurements[ ..,"Centroid"] and correcting the results, but I'm afraid future Mma versions may modify this behavior and spoil the results.

I could also run a previous "calibration" with a known image and calculate the offsets, so it will auto-correct, but I would like to understand what is going on first.

Is this a bug?
Any ideas about why is this happening?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

似梦非梦 2024-11-23 22:55:52

我觉得 ComponentMeasurements 的文档页面包含解决方案:

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

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

I feel the doc page for ComponentMeasurements contains the solution:

Position, area, and length
measurements are taken in the standard
image coordinate system where position
{0,0} corresponds to the bottom-left
corner, x runs from 0 to width, and y
runs from 0 to height.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文