我想圖像像素存儲在三維陣列和我一樣:三維陣列
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)
如何解決?任何想法 ?
'int r = 3'應該是'int r = 4'不是。每種顏色有4個組件。 – 2013-02-16 07:31:45