Paijo S Kom
Paijo S Kom

Reputation: 61

get eigenvalue pca with java

I try use PCA to reduce dimention, and i use jama for help me using matrix. but, i got problem when get eigenvalue with jama. for example i hava 2 image dimention 100x100, then i create single matrix 2 image x (100x100). there is 20.000 pixel. and how to get reduction with eigenvalue? this is sample my code :

public static void main(String[] args) {
    BufferedImage bi;
    int[] rgb;
    int R, G, B;
    // int[] jum;
    double[][] gray = new double[500][500] ;
    String[] baris = new String[1000];
    try {
        //bi = ImageIO.read(new File("D:\\c.jpg"));
        int[][] pixelData = new int[bi.getHeight() * bi.getWidth()][3];

        int counter = 0;
        for (int i = 0; i < bi.getHeight(); i++) {
            for (int j = 0; j < bi.getWidth(); j++) {
                gray[i][j] = getPixelData(bi, i, j);
                // R = getR(bi, i, j);
                //G = getG(bi, i, j);
                //B = getB(bi, i, j);
                //jum = R + G + B;
                // gray[counter] = Double.toString(R + G + B / 3);
                // System.out.println("Gray " +gray);
                //for (int k = 0; k < rgb.length; k++) {
                //   pixelData[counter][k] = rgb[k];
                // }

                counter++;
            }
        }


    } catch (IOException e) {
        e.printStackTrace();
    }
    Matrix matrix =  new Matrix(gray);
    PCA pca = new PCA(matrix);
    pca.getEigenvalue(6);
    String n = pca.toString();
    System.err.println("nilai n "+n);
    //double dete = pcadete(matrix,3600);
}

private static int getPixelData(BufferedImage bi, int x, int y) {
    int argb = bi.getRGB(y, x);
    int r, g, b;
    int gray;
    int rgb[] = new int[]{
        (argb >> 16) & 0xff, //red
        (argb >> 8) & 0xff, //green
        (argb) & 0xff //blue
    };
    r = rgb[0];
    g = rgb[1];
    b = rgb[2];

    gray = (r + g + b) / 3;
    System.out.println("gray: " + gray);
    return gray;
}

when i show eigenvalue in this code :

PCA pca = new PCA(matrix);
    pca.getEigenvalue(6);
    String n = pca.toString();
    System.err.println("nilai n "+n);

Result is :

nilai n PCA@c3e9e9

Can, u tell me what way to get eigenvalue and reduction dimension.

Upvotes: 1

Views: 1419

Answers (1)

Juned Ahsan
Juned Ahsan

Reputation: 68715

You are seeing the object output because there seems to be no toString implentation in PCA class to print it in a beautiful way.

In case you own the PCA class then you can override the toString method to print it the way you want. If you are using eclipse, you can generate that method by right clicking on your PCA class->Source-> Generate toStirng.

Or otherwise try to print it using the getters such as:

System.err.println("nilai n "+ n.getEigenvalue());.

Btw sharing your PCA class will help more.

Upvotes: 1

Related Questions