2013-02-16 178 views
3

我想圖像像素存儲在三維陣列和我一樣:三維陣列

public class ImageProcessing { 
    private BufferedImage image; 
    int p = image.getWidth(); 
    int q = image.getHeight(); 
    int r = 3; 
    private int[][][] data = new int[p][q][r]; 

然後我從圖像獲取的像素值,並將其存儲到該數組,我讓構造函數如下:

public ImageProcessing(BufferedImage img){ 
     image = img; 
     for(int x = 0; x < image.getWidth(); x++){ 
      for(int y = 0; y < image.getHeight(); y++){ 
       int px = image.getRGB(x,y); 
       int alpha = (px >> 24) & 0xFF; 
       int red = (px >> 16) & 0xFF; 
       int green = (px >> 8) & 0xFF; 
       int blue = px & 0xFF; 
       data[x][y][0] = alpha; 
       data[x][y][1] = red; 
       data[x][y][2] = green; 
       data[x][y][3] = blue; 
      } 
     } 
    } 

的話,我想找回它的圖像轉換成灰度如下:

public BufferedImage greyscale(){ 
     for(int x = 0; x < p ; x++){ 
      for(int y = 0; y < q ; y++){ 
       int avg = (data[x][y][1] + data[x][y][2] + data[x][y][3]/3); 
       int grey = (data[x][y][0]<<24) + (avg<<16) + (avg<<8) + avg; 
       image.setRGB(x, y, grey); 
      } 
     } 
     return image; 
    } 

但是當我運行它時,我顯示錯誤如下,我不知道它是什麼意思,以及如何解決它?

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
    at ImageProcessing.<init>(ImageProcessing.java:6) 
    at Main$1.run(Main.java:14) 
    at java.awt.event.InvocationEvent.dispatch(Unknown Source) 
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 
    at java.awt.EventQueue.access$000(Unknown Source) 
    at java.awt.EventQueue$1.run(Unknown Source) 
    at java.awt.EventQueue$1.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source) 
    at java.awt.EventQueue.dispatchEvent(Unknown Source) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.run(Unknown Source) 

如何解決?任何想法 ?

+3

'int r = 3'應該是'int r = 4'不是。每種顏色有4個組件。 – 2013-02-16 07:31:45

回答

2

顯然,它會因爲

private BufferedImage image;  // had not initialized image 
    int p = image.getWidth();   // will be null 
    int q = image.getHeight();   // same here 
    int r = 3; 
    private int[][][] data = new int[p][q][r]; 

您還沒有初始化BufferedImage image那麼,它將目前指向空,而當你初始化p = image.getWidth();int q = image.getHeight();,則沒有任何價值,所以

private int[][][] data = new int[p][q][r];

以上聲明將拋出NE

你應該在Class中聲明數組,並且應該在你的類的構造函數中初始化它,初始化爲image


編輯:

退房下面的代碼,它的初始化BufferedImage,然後做同樣的,它的做工精細!

public class ArrayInitialize { 
    public static void main(String ar[]){ 
     File file = new File("Image.jpg"); 
     BufferedImage img = new BufferedImage(240, 240, BufferedImage.TYPE_INT_ARGB); 
     try { 
      img = ImageIO.read(file); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     int p = img.getWidth();   // will be null 
     int q = img.getHeight();   // same here 
     int r = 3; 
     int count = 0; 
     int[][][] data = new int[p][q][r]; 
     for(int i=0;i<p;i++){ 
      for(int j=0;j<q;j++){ 
       for(int k=0;k<r;k++){ 
        count++; 
       } 
      } 
     } 
     System.out.println(count); 
    } 
} 

我認爲,初始化BufferedImage如果你得到一個NullPointerException在此之後,有一些問題與您getImage()方法,可能是它不能正常工作。

,並查看評論

INT R = 3應該是INT R = 4是吧。每種顏色有4個組件。

+0

我將圖像初始化爲:GettingImage get = new GettingImage(); \t private BufferedImage image = get.getImage();在你回答之後。但錯誤仍然是,即使圖像初始化的任何建議呢? – 2013-02-16 14:25:42

+0

我已經更新了我的答案。一探究竟。如果它幫助你,那麼你可以將它標記爲已接受。 :) – Parth 2013-02-16 15:52:00

+0

非常感謝。那3換4時,工作正常。只有兩件事:r = 4和BufferedImage的初始化是我的錯誤,我接受了你的答案。 – 2013-02-16 17:29:49