【发布时间】:2013-07-22 21:40:57
【问题描述】:
我正在尝试根据以下参数合并两个黑白图像。
新创建的数组使用两个图像的宽度和高度中的最大值作为它自己的值。数组中特定点的 int 包含一个表示灰度量的数字。新的二维数组会根据以下条件合并两张图片。
- 如果位置在两个数组中都存在,则结果数组中的值是数组 a 和 b 中值的平均值。
- 如果位置只存在于数组a中,则结果数组中的值就是数组a中的值
- 如果位置只存在于数组b中,则结果数组中的值为数组b中的值
- 如果该位置不在两个数组中,则在结果数组中的该位置分配值 127。
这是我的代码。由于某种原因,它不会运行,因为它返回 arrayIndexOutOfBounds: 167。如果有人能看一下,我将不胜感激。
public static int[][] merge(int[][] a, int[][] b)
{
int biggerx = 0;
int biggery = 0;
if (a[0].length > b[0].length) {
biggery = a[0].length;
}
else {
biggery = b[0].length;
}
if (a.length > b.length) {
biggerx = a.length;
}
else {
biggerx = b.length;
}
int[][] merged = new int[biggerx][biggery];
for (int x = 0; x < merged.length; x++) {
for (int y = 0; y < merged[x].length; y++) {
if (x <= a.length && y <= a[x].length && x > b.length && y > b[x].length) {
merged[x][y] = a[x][y];
}
else if (x <= b.length && y <= b[x].length && x > a.length && y > a[x].length) {
merged[x][y] = b[x][y];
}
else if (x <= b.length && y <= b[x].length && x <= a.length && y <= a[x].length) {
merged[x][y] = ((a[x][y] + b[x][y]) /2);
}
else {
merged[x][y] = 127;
}
}
}
return merged;
}
【问题讨论】:
-
如需尽快获得更好的帮助,请发帖SSCCE。
标签: java arrays image merge indexoutofboundsexception