Finding the inverse matrix

I want to find the inverse of the XYZ color space matrix for LMS. Looking around, I found that the processing has a transpose function. But I know that matrix transfer is not always the opposite. I could figure out a way to calculate the inverse by hand, but does the handling have any built-in functionality for this or has anyone already figured out a way to do this and don't mind sharing?

float []lms2xyzConverter(float[] lmsVals){               

   //Normalized to D65
   float[][]xyz2lms =  {   {0.4002 , 0.7076 , -0.0808},           
                           {-0.2263 , 1.1653 , 0.0457},       
                           {0.00000 , 0.00000 , 0.9182} }; 

   float [][]lms2xyz = xyz2lms.transpose();
}; 

      

+3


source to share


2 answers


Transpose is not the same as the inverse for color matrices, since they are not orthogonal ( M transpose times M = I

)

There are processing libraries that can invert matrices: http://adilapapaya.com/papayastatistics/

float[][] invA = Mat.inverse(A);

      



Alternatively, there are a huge number of matrix libraries for Java that you might be able to use eg. https://code.google.com/p/efficient-java-matrix-library/ .

However, since you only need the inverse of the 3D matrix, you can manually invert it using 9 lines of code, each with corresponding formulas (see http://www.wikihow.com/Inverse-a-3X3-Matrix ). It will probably be faster and less memory than any of the out-of-the-box solutions!

+1


source


Hopes below developed code might be helpful to you

package com.test.testprogramms;

/**
 * 
 * @author bhavikambani
 *
 */
public class Matrix {

    private int nrows;
    private int ncols;
    private double[][] data;

    public Matrix(double[][] dat) {
        this.data = dat;
        this.setNrows(dat.length);
        this.setNcols(dat[0].length);
    }

    public Matrix(int nrow, int ncol) {
        this.setNrows(nrow);
        this.setNcols(ncol);
        data = new double[nrow][ncol];
    }

    public int getNcols() {
        return ncols;
    }

    public void setNcols(int ncols) {
        this.ncols = ncols;
    }

    public int getNrows() {
        return nrows;
    }

    public void setNrows(int nrows) {
        this.nrows = nrows;
    }

    public double getValueAt(int i, int j) {
        return data[i][j];
    }

    public void setValueAt(int i, int j, double valueToBeSet){
        data[i][j] = valueToBeSet;
    }
}

      



And below is the utility class

package com.test.testprogramms;

/**
 * 
 * @author bhavikambani
 *
 */
public class MatrixUtil {

    public static Matrix transpose(Matrix matrix) {
        Matrix transposedMatrix = new Matrix(matrix.getNcols(), matrix.getNrows());
        for (int i = 0; i < matrix.getNrows(); i++) {
            for (int j = 0; j < matrix.getNcols(); j++) {
                transposedMatrix.setValueAt(j, i, matrix.getValueAt(i, j));
            }
        }
        return transposedMatrix;
    }
}

      

-1


source







All Articles