【问题标题】:Covariance Matrix of an Ellipse椭圆的协方差矩阵
【发布时间】:2025-12-31 15:35:11
【问题描述】:

我一直在尝试解决一个问题。我很惊讶我无法在网上找到任何真正有用的东西。

我知道从椭圆的协方差矩阵的特征值,可以计算出椭圆的长轴和短轴。如下:

a1 = 2*sqrt(e1)
a2 = 2*sqrt(e2)

其中a1a2是长轴和短轴,e1e2是协方差矩阵的特征值。

我的问题是:给定图像椭圆的边缘点(xi,yi),如何找到该椭圆的 2×2 协方差矩阵?

【问题讨论】:

  • 矩阵不应该只是所有xi-syi-s的协方差吗?
  • 我不确定!我为半径为 100 的圆生成一个边缘点。然后我定义了一个p = [xi,yi],其中 P 是一个边缘点矩阵n x 2。我使用了matlab命令cov(P)。我从协方差矩阵重新计算了圆的半径。但是给出了与原始半径不同的值。 (它给出了 141,140)!!
  • ...这个数字除以 100 应该会响起 :)

标签: matlab image-processing matrix covariance ellipse


【解决方案1】:

仅通过纯逆向工程(我不再熟悉这种材料),我可以做到这一点:

%// Generate circle
R = 189;
t = linspace(0, 2*pi, 1000).';
x = R*cos(t);
y = R*sin(t);

%// Find the radius?
[~,L] = eig( cov([x,y]) );

%// ...hmm, seems off by a factor of sqrt(2)
2*sqrt( diag(L) )        

%// so it would come out right when I'd include a factor of 1/2 in the sqrt():
2*sqrt( diag(L)/2 )        

那么,让我们来测试一下一般椭圆的理论:

%// Random radii
a1 = 1000*rand;
a2 = 1000*rand;

%// Random rotation matrix
R = @(a)[
    +cos(a) +sin(a); 
    -sin(a) +cos(a)];

%// Generate pionts on the ellipse 
t = linspace(0, 2*pi, 1000).';
xy = [a1*cos(t)  a2*sin(t);] * R(rand);

%// Find the deviation from the known radii
%// (taking care of that the ordering may be different)
[~,L] = eig(cov(xy));
min(abs(1-bsxfun(@rdivide, 2*sqrt( diag(L)/2 ), [a1 a2; a2 a1])),[],2)

它总是返回一些可以接受的小东西。

所以,似乎可行:) 任何人都可以验证这确实是正确的吗?

【讨论】:

  • 因子sqrt(2) 是因为协方差矩阵是根据椭圆周长上的点计算的,而不是实心椭圆。 OP 方程对实心椭圆的协方差矩阵有效。
【解决方案2】:

为了扩展 Rody 的答案,实心椭圆的协方差矩阵具有由 lambda_i = r_i^2/4 给出的特征值。这导致了 OP 的 r_i = 2*sqrt(lambda_i) 方程。

对于(非实心)椭圆,如 OP 的情况,特征值是实心情况的两倍:lambda_i = r_i^2/2,导致 r_i = sqrt(2*lambda_i)(等于 Rody 的 2*sqrt(lambda_i/2))。

我无法直接找到此参考,但协方差矩阵的数学与惯性矩的数学相同。 On Wikipedia你可以看到“圆环”和“实心盘”的情况,它们相差相同的2倍。

这是对 Rody 的测试的改编,同时进行了实体和非实体情况:

% Radius to test with
r = rand(1,2);

% Random rotation matrix
R = @(a)[+cos(a) +sin(a); 
         -sin(a) +cos(a)];

% Generate pionts on the ellipse
N = 1000;
t = linspace(0, 2*pi, N).';
xy = r.*[cos(t),sin(t)] * R(rand);
% Compute radii, compare to known radii
L = eig(cov(xy));
r_ = sqrt(2*L)';
err = max(abs(1 - sort(r_) ./ sort(r)))

% Generate points in the ellipse (SOLID CASE)
N = 10000;
t = 2*pi * rand(N,1);
xy = r .* sqrt(rand(N,1)) .* [cos(t),sin(t)] * R(rand);
% Compute radii, compare to known radii
L = eig(cov(xy));
r_ = 2*sqrt(L)';
err_solid = max(abs(1 - sort(r_) ./ sort(r)))

如果您运行此代码,您将看到 1e-3 和 ~6e-3 的错误(对于实体情况,我生成更多点,因为该区域需要更多点才能足够密集地采样;点越多,误差越小)。

【讨论】: