# 关于矩阵自由度的解释

在计算机视觉应用中，经常碰见矩阵自由度的说法，矩阵自由度同矩阵本身的性质，以及相关线性方程组的解，有着千丝万缕的联系，这里就集中解释下DOF（degree of freedom）的具体含义。

1.矩阵的自由度的定义

There are several different ways to think about degrees of freedom of a matrix.

Consider a m×n matrix. This matrix has mn entries. We can change mn values in this matrix to make mn unique matrices, so it has mn degrees of freedom.

What if we had a square m×m matrix that we knew was upper triangular? Well then, we know that several values in the matrix are 0. There are actually only m+m−1+⋯+2+1 nonzero entries, and so that's the number of the degrees of freedom of the matrix.

What is we had a 2×2 matrix that we knew was a rotation matrix? That puts huge constraints on the possible values in the matrix. Indeed, once one of the values is chosen, then all other values have been decided. There is only one degree of freedom in this matrix. This is easy to see geometrically; a rotation matrix on R2 can only rotate by an angle, which is its degree of freedom.

What if we had "equivalence classes"? What if we knew that all scalings of any matrix were equivalent. How many degrees of freedom do we have left? For any matrix, when the (1,1) element is non-zero, we can divide all elements of the matrix by the first element to make it 1. So if we had two matrices A and B=2A, when we scaled these matrices so that their first elements were 1, we'd see that they were equivalent. And thus, we've eliminated a degree of freedom. This is the case with homographies. So, for a 3×3 homography matrix, there are only 8 degrees of freedom. These degrees of freedom can also be interpreted geometrically.

选自：https://math.stackexchange.com/questions/2771591/degrees-of-freedom-for-a-matrix

简而言之，矩阵的自由度反映了矩阵中每个元素互相约束的状态。

关于homography矩阵，多说两句。它是up to a scale的，就是说 kH ~ H，一般在实现的时候（OpenCV），为了保证这个约束，一般采用两种方式。

以3*3矩阵为例，一种是H33 = 1，或者是 H11*H11 + H12*H12 + H13*H13 + H21*H21 + H22*H22 + H23*H23 + H31*H31 + H32*H32 + H33*H33 = 1

又如：以3*3的Rotation Matrix为例，为什么其自由度为3？

In nn dimensions you have 1/2n(n−1) degrees of freedom.

One argument goes like these lines: In nn dimensions a rotation matrix R has n^2 degrees of freedom. Rotations preserve the dot products of vectors. So utv=utRtRv for any vectors u and v and so RtR=I.

That's n^2 equations. But those below the diagonal are the same as those above. So we only have 1/2n(n+1) equations leaving 1/2n(n−1) degrees of freedom.

There's actually one more constraint: rotations need to preserve volume so detR=1.

You can already deduce (detR)2=1 from RtR=I so this extra constraint doesn't reduce the degrees of freedom, it just cuts the space in half. A full proof requires showing that the remaining equations are algebraically independent.

2.方程自由度的定义

与矩阵自由度类似的，还有方程自由度的定义。

The degrees of freedom is used when you have more variables than equations and the degrees of freedom is how many more variables you have than equations. So if you have 10 variables and 7 equations you have 3 degrees of freedom. I.e. 3 of the variables can assume whatever value you want and for any given choice of values for these 3 variables the remaining 7 have a specific value for a solution.

I.e. the number of equations + the degree of freedom always gives you how many variables you have.

If the number of equations are more than the number of variables you don't have any freedom and the system of equations are typically not solvable at all.

If you do have degrees of freedom, you typically need to find some additional constraint that can add additional equations. The idea is to have exactly the same number of equations as variables in which case there is one and only one solution. This is under the assumption that the equations are linearly independent. You must first remove all linearly dependent equations before you count the degrees of freedom.

For example: 2x + 3y = 5 and 4x + 6y = 10 are two equations but they are not linearly independent so when you remove one you see that you have only one equation with two variables so you have one equation and one degree of freedom.

3. 举例

以张正友的标定为例，在其论文A Flexible New Technique for Camera Calibration中，第2.3节有这么一段话：

These are the two basic constraints on the intrinsic parameters, given one homography. Because a

homography has 8 degrees of freedom and there are 6 extrinsic parameters (3 for rotation and 3 for

translation), we can only obtain 2 constraints on the intrinsic parameters.

可以这么理解：H矩阵有8个自由度，构成它的矩阵有一个R矩阵，有三个自由度，一个T矩阵有三个自由度，那就还剩下A矩阵的两个自由度。但因为A矩阵有四个元素（这里设skew为0），所以必须有两个约束保证其自有度只有两个（4-2=2）