I have a camera matrix k which I have computed. The value of k is:
[[ 1.92160183e+08 0.00000000e+00 3.06056985e+02]
[ 0.00000000e+00 1.92160183e+08 1.57709172e+02]
[ 0.00000000e+00 0.00000000e+00 1.00000000e+00]]
Now, I have tried to find the inverse of k using numpy (using np.linalg.inv(k)). Let k1 be the inverse of k.
Using numpy, the value of k*k1 is:
[[ 1.00000000e+00 0.00000000e+00 -4.87462472e-04]
[ 0.00000000e+00 1.00000000e+00 -1.29434633e-04]
[ 0.00000000e+00 0.00000000e+00 1.00000000e+00]]
Now, I was expecting a perfect identity matrix but since the values which should be zero are very small, I decided to ignore the fact that the result was not a perfect identity matrix.
Now my problem: I have two other matrices R and h. R is an identity matrix (it is not always an identity matrix but assume so for the sake of simplicity). I need to perform H1 = k*R*k1*h. This should ideally assign the value of h to H1 (since k*R*k1 should turn out to be identity).
My original h matrix:
[[ 1.71025842e+00 -7.51761942e-01 1.02803446e+02]
[ -2.98552735e-16 1.39232576e-01 1.62792482e+02]
[ -1.13518150e-18 -2.27094753e-03 1.00000000e+00]]
My R matrix:
[[ 1. 0. 0.]
[ 0. 1. -0.]
[-0. 0. 1.]]
The value of H1 produced using H1 = k*R*k1*h:
[[ 1.71025842 -0. -0. ]
[-0. 0.13923258 0. ]
[ 0. -0. 1. ]]
The value of H1 produced using H1 = k*k1*h:
[[ 1.71025842 -0. -0.05011282]
[-0. 0.13923258 -0.02107099]
[-0. -0. 1. ]]
Why is the value of H1 not coming equal to h as it should? How can I fix this?
Solution:
Your understanding on what the * operator does is flawed. It does not perform a dot product. But instead performs an elementwise multiplication on the two arrays, also known as the Hadamard product.
So, if you have two 2D matrices, A and B, the dot product is computed with –
Whereas, the hadamard product looks like this –
Which is an elementwise multiplication (and what you’re currently doing). Try replacing this with a call to np.ndarray.dot or using the @ operator:
>>> k.dot(k1)
Or,
>>> k @ k1 # python3.5+
array([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]])
Contrast this with –
>>> k * k1
array([[ 1.00000000e+00, 0.00000000e+00, -4.87462473e-04],
[ 0.00000000e+00, 1.00000000e+00, -1.29434634e-04],
[ 0.00000000e+00, 0.00000000e+00, 1.00000000e+00]])
Which is what you were getting earlier.

