This site uses strictly necessary cookies. More Information

X# How to invert an 8x8 matrix or calculate 8DOF homography

Hi, I'm trying to write my own distortion tool. I have 2 sets of corners and want to solve for the transformation matrix between them. I found I can do that by solving the system below for H but it means I need to invert an 8x8 matrix. How do I do that? or otherwise how do I find the transformation matrix from the points?

**Edit:** I'm trying to find the 4x4 matrix H=[h11, ..., h32] that transformed 4 given pairs of 2d points. Once I have it I can use it to transform all of a mesh's vertices and get a distorted mesh (I'll do it by extending BaseMeshEffects). So I need to invert the large matrix on the left & Left-multiply the inverse by the vector on the right to get H's coefficients.

**Answer** by Bunny83
·
Jul 22, 2020 at 11:46 AM

Well I'm not sure what you actually want to do here. However to invert a 8x8 matrix (or any sized matrix) you can use my GaussMatrixF which is just an arbitrary sized matrix which can be solved by Gaussian elimination. It's meant to be setup as an augmented matrix and it will solve the first square part of the matrix into an identity matrix. So to solve an 8x8 matrix you would need to create a 8x16 matrix (8 rows, 16 columns) and place you original matrix in the left half. Fill the right half with the 8x8 identity matrix and call Solve. It's explained here.

Something like this:

```
var gm = GaussMatrixF(16, 8);
gm.rows[0].cols[0] = x1;
gm.rows[0].cols[1] = y1;
// [ ... ]
// fill your whole 8x8 matrix
// [ ... ]
// initialize second half with identity matrix
for(int r = 0; r < 8 r++)
{
for(int c = 0; c < 8 c++)
{
if (r == c)
gm.rows[r].cols[8+c] = 1;
else
gm.rows[r].cols[8+c] = 0;
}
}
if (gm.Solve())
{
// access inverted matrix by reading back columns index 8 to 15
}
```

Thanks! This is a good start, I'll try extending this to support matrix multiplication as well.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.