我有 2 个图像,每个图像都是完整图像的一部分,将 2 个图像组合起来可以创建完整图像。
然而,这两个图像有重叠,我正在尝试创建一个程序,该程序将找到图像 2 的顶行与图像 1 中的任意一行像素相交的位置。 我创建了一个 for 循环来收集数组中每个图像的每一行像素。
这是我的代码:
int row = 0;
for (int i = 0; i < imageArray1.length; i++) {
for (int j = 0; j < imageArray1[i].length; j++) {
if (imageArray1[i][j] == (imageArray2[0][0])) {
row = imageArray1[i][j];
}
}
}
问题是我很确定我只是收集第二张图像左上角的单个像素,而不是整行。 有什么想法可以解决这个问题吗? java新手
最佳答案
您需要将 image1 中的每一行与 image2 中的每一行进行交叉检查。因此,3 级循环: 1) 循环遍历 image1 中的行 2) 循环遍历 image2 中的行 3) 循环遍历 image1 和 image2 中当前行的列,以确定它们是否重叠
int overlappingRowInImage1 = 0;
int overlappingRowInImage2 = 0;
int[][] imageArray1 = null;
int[][] imageArray2 = null;
// loop through the rows in the first image
for (int row1 = 0; row1 < imageArray1.length; row1++) {
boolean foundIdenticalRow = false;
// loop through the rows in the second image
for (int row2 = 0; row2 < imageArray2.length; row2++) {
foundIdenticalRow = true;
// two rows are identical if each column in both rows are the same
for (int col = 0; col < imageArray1[row1].length; col++) {
if (imageArray1[row1][col] != (imageArray2[row2][col])) {
foundIdenticalRow = false;
break;
}
}
if (foundIdenticalRow) {
overlappingRowInImage1 = row1;
overlappingRowInImage2 = row2;
break;
}
}
if (foundIdenticalRow) {
System.out.println("Row " + overlappingRowInImage1 + " in image 1 is overlapping with Row " +
overlappingRowInImage2 + " in image 2");
break;
}
}
关于java - 比较多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33602611/