Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve an equation with variables in a matrix in Python?

i'm coding in Pyhon, and I'm working on stereo-correlation. I want to resolve this equation : m = K.T.M

m,K,M are know.

where :

M is the homogeneous coordinate of a point in the cartesian coordinate system "world"

M=np.array([X,Y,Z,1])

K is my intrinsic matrix for the left camera

K=np.matrix([ [fx,  0, cx, 0],
              [ 0, fy, cy, 0], 
              [ 0,  0,  1, 0]])

m its the M point views by the left camera

m=np.array([x,y,1])

and T is the transformation to pass to the "world" coordinate system to the left camera coordinate system.

T= np.matrix([[x00, x01, x02, Tx],
              [x10, x11, x12, Ty], 
              [x20, x21, x22, Tz], 
              [0  , 0  , 0  , 1 ]])

So I want to solve this equation to find T but it's impossible to create a matrice without give values to variables.

someone have a solution?

Thanks best regards

like image 807
Golgii Avatar asked Feb 04 '16 14:02

Golgii


People also ask

How to solve a system of equations using matrices?

This is also known as inverse matrix equation and hence the process of using the above formula to solve a system of equations is known as the " inverse matrix method". Thus, here are the steps to solve a system of equations using matrices: Find the inverse, A -1. Multiply it by the constant matrix B to get the solution. i.e., X = A -1 B.

How to solve equations in Python?

When we solve this equation we get x=1, y=0 as one of the solutions. In Python, we use Eq () method to create an equation from the expression. For example, if we have expression as x+y = 1. It can be written as Eq (x+y,1) Construct the equations using Eq () method. To solve the equations pass them as a parameter to the solve () function.

How do you represent a set of linear equations as matrices?

If our set of linear equations has constraints that are deterministic, we can represent the problem as matrices and apply matrix algebra. Matrix methods represent multiple linear equations in a compact manner while using the existing matrix library functions. We will be using NumPy ( a good tutorial here) and SciPy ( a reference guide here ).

How to create a matrix in Python?

Let's first create the matrix A in Python. To create a matrix, the array method of the Numpy module can be used. A matrix can be considered as a list of lists where each list represents a row. In the following script we create a list named m_list, which further contains two lists: [4,3] and [-5,9].


1 Answers

If you want a generic solution, you can use Sympy, which allows you to work with symbolic expression. In the following code the expression K.T.M = m is reformulated to a standard linear equation HH.xx = mm, where xx is the vector with the unknowns extracted from T:

from IPython.display import display
import sympy as sy

sy.init_printing()  # LaTeX like pretty printing for IPython

# declaring symbolic variables:
x, y, X, Y, Z, fx, fy, cx, cy = sy.symbols("x y X Y Z f_x f_y c_x c_y", real=True)
x00, x01, x02, x10, x11 = sy.symbols("x00, x01, x02, x10, x11", real=True)
x12, x20, x21, x22 = sy.symbols("x12, x20, x21, x22", real=True)
Tx, Ty, Tz = sy.symbols(" T_x T_y T_z", real=True)

# Building matrices and vectors:
M = sy.Matrix([X, Y, Z, 1])
m = sy.Matrix([x, y, 1])
K = sy.Matrix([[fx,  0, cx, 0],
               [0,  fy, cy, 0],
               [0,   0,  0, 1]])
T = sy.Matrix([[x00, x01, x02, Tx],
               [x10, x11, x12, Ty],
               [x20, x21, x22, Tz],
               [0,     0,   0,  1]])

print("KTM = K.T.M = ")
KTM = sy.simplify(K*T*M)
display(KTM)

print("Vector of Unkowns xx.T = ")
xx = sy.Matrix(list(T.atoms(sy.Symbol)))
display(xx.T)
print("For equation HH.xx = mm, HH = ")
HH = KTM[:2, :].jacobian(xx)  # calculate the derivative for each unknown
display(HH)

As @Sven-Marnach already noted, there are not enough equations for a unique solution. Since the last row of the vector KTM and of m is 1, there are only two equations for twelve variables.

If you have multiple pixelsto evaluate, i.e., multiple pairs of (m, M), you can use Numpy's Least Squares Solver to find a solution.

like image 52
Dietrich Avatar answered Sep 19 '22 10:09

Dietrich