2

我想将图像像素存储在一个三维数组中,我做了:

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)

如何解决?任何想法 ?

4

1 回答 1

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所以,它当前会指向null,当你初始化p = image.getWidth();and 时int q = image.getHeight();,没有任何值,所以

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

上述语句将抛出NE.

您应该只在 Class 中声明数组,并且应该在您的类的构造函数中使用 initializing 对其进行初始化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);
    }
}

我认为,在初始化BufferedImageif 你得到一个NullPointerExceptionthen 之后,你的方法有一些问题getImage(),可能是它不能正常工作。

并查看评论

int r=3 应该是 int r=4 不是吗。每种颜色有 4 个成分。

于 2013-02-16T07:38:08.920 回答