2013-06-30 83 views
1

我嘗試使用PCA來減小維數,並且我使用jama來幫助我使用矩陣。 但是,我得到問題時,與jama的特徵值。 例如我hava 2圖像尺寸100x100,然後我創建單個矩陣2圖像x(100x100)。 有20.000像素。 以及如何使用特徵值來減少? 這是品嚐我的代碼:用java獲取特徵值pca

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

結果是:

汝來ňPCA @ c3e9e9

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; 
} 

當我在此代碼特徵值顯示,你告訴我如何獲得特徵值和縮減維度。

回答

1

您正在看到對象輸出,因爲在PCA類中似乎沒有toString實現來以美麗的方式打印它。

如果您擁有PCA類,那麼您可以重寫toString方法以您想要的方式打印它。如果您使用的是Eclipse,您可以通過右鍵單擊您的PCA類 - > Source-> Generate toStirng來生成該方法。

或者以其他方式嘗試使用干將打印出來,如:

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

順便說一句分享你的PCA類將幫助更多。