2015-06-20 149 views
0

在讀取RGB圖像時,請執行移位操作以分別獲取R,G和B矩陣......是否可以讀取灰度圖像(JPEG)並直接操作其像素然後重寫圖片?在java中讀取灰度圖像

最終我必須對灰度圖像執行DCT操作。

回答

1

下面的代碼將讀取的灰度圖像,以簡單的二維陣列:

File file = new File("path/to/file"); 
    BufferedImage img = ImageIO.read(file); 
    int width = img.getWidth(); 
    int height = img.getHeight(); 
    int[][] imgArr = new int[width][height]; 
    Raster raster = img.getData(); 
    for (int i = 0; i < width; i++) { 
     for (int j = 0; j < height; j++) { 
      imgArr[i][j] = raster.getSample(i, j, 0); 
     } 
    } 

注:raster.getSample(...)方法採用3個參數:x - 像素位置的X座標,y - 在Y的座標像素位置,b - 要返回的樂隊。在灰度圖像的情況下,我們應該/只可以得到波段。