2013-04-06 33 views
4

我有一個圖像數據庫,我想將這些圖像的RGB矩陣分別存儲在mysql數據庫中(例如:redMatrix_column,greenMatrix_column,blueMatrix_column)。在matlab中,我可以使用imread()函數分別獲得RBG矩陣。如何在java中做到這一點?謝謝你的幫助。如何分別獲取圖像的rgb矩陣?

回答

6

這是你如何讓色彩分量:

public class GetImageColorComponents { 
    public static void main(String... args) throws Exception { 
    BufferedImage img = ImageIO.read(GetImageColorComponents.class 
            .getResourceAsStream("/image.png")); 
    int[] colors = new int[img.getWidth() * img.getHeight()]; 
    img.getRGB(0, 0, img.getWidth(), img.getHeight(), colors, 0, img.getWidth()); 

    int[] red = new int[colors.length]; 
    int[] green = new int[colors.length]; 
    int[] blue = new int[colors.length]; 

    for (int i = 0; i < colors.length; i++) { 
     Color color = new Color(colors[i]); 
     red[i] = color.getRed(); 
     green[i] = color.getGreen(); 
     blue[i] = color.getBlue(); 
    } 
    } 
} 

涉及保存和檢索在MySQL數據庫中的字節一個完整的例子見this gist

+0

非常感謝。我比較你的紅色通道與matlab紅色通道都是一樣的:)再次感謝。 – GltknBtn 2013-04-06 08:05:45

+0

感謝您的要求,但是當我使用「字節」而不是「int」時,綠色[],紅色[],藍色[]陣列的負值。這是一個問題嗎?再次感謝。 – GltknBtn 2013-04-06 17:55:48

+0

我認爲這是由於有符號的字節。因爲如果value> = 128,那麼它是負值。 – GltknBtn 2013-04-06 18:05:45