Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 为什么这个循环在扫描图像时会给我一个arrayIndexOutOfBoundsException?_Arrays_Image Processing_Multidimensional Array_Indexoutofboundsexception - Fatal编程技术网

Arrays 为什么这个循环在扫描图像时会给我一个arrayIndexOutOfBoundsException?

Arrays 为什么这个循环在扫描图像时会给我一个arrayIndexOutOfBoundsException?,arrays,image-processing,multidimensional-array,indexoutofboundsexception,Arrays,Image Processing,Multidimensional Array,Indexoutofboundsexception,我目前正试图找出一些代码,通过改变图像的颜色,获取每个像素的rgb值,并将它们放入一个数组中,使图像水平翻转。我设法让它在垂直翻转图像时起作用,但我一生都无法让它在水平方向上起作用。我不明白为什么我总是有一种越界的感觉。值得注意的是,此代码确实适用于方形图像,但不适用于矩形图像。所以我的代码是 int width = selection.getWidth(); int height = selection.getHeight(); int rgb[][] = new int[height][wi

我目前正试图找出一些代码,通过改变图像的颜色,获取每个像素的rgb值,并将它们放入一个数组中,使图像水平翻转。我设法让它在垂直翻转图像时起作用,但我一生都无法让它在水平方向上起作用。我不明白为什么我总是有一种越界的感觉。值得注意的是,此代码确实适用于方形图像,但不适用于矩形图像。所以我的代码是

int width = selection.getWidth();
int height = selection.getHeight();
int rgb[][] = new int[height][width];
int rgb2[][] = new int[height][width];

for (int i = 0; i < height; i++) {
    for (int j = 0; j < width; j++) {
        rgb[i][j] = selection.getRGB(i, j);
        rgb2[i][j] = selection.getRGB(i, j);
        System.out.println(j);
    }
}
intwidth=selection.getWidth();
int height=selection.getHeight();
整数rgb[][]=新整数[高度][宽度];
int rgb2[][]=新的int[高度][宽度];
对于(int i=0;i
通过读取小数组的值并测试这个循环来调试它,我不知道发生了什么。我试着用手做了一些数值,看起来它应该一行一行地扫描,创建一个2d数组,该数组的高度和宽度和图像的像素值相同。然而,它永远不会离开内部循环。内部循环中的某些内容超出了范围

甚至做

for (int i = 0; i < rgb.length; i++) {
        for (int j = 0; j < rgb[0].length; j++) {
            rgb[i][j] = selection.getRGB(i, j);
            rgbTwo[i][j] = selection.getRGB(i, j);

        }
    }
for(int i=0;i

它仍然说坐标是不允许的。我不明白为什么这不适用于水平方向,但适用于垂直方向,适用于正方形,但不适用于矩形。我的垂直翻转适用于正方形和矩形。

getRGB(i,j)
替换为
getRGB(j,i)
。x值在y值之前。(请参阅)

看起来
i
j
是向后的。宽度应该是第一位的,不是吗
getRGB
?非常感谢。我有3个不同的循环来解决这个问题,这就是问题所在。