[英]Finding smaller 2D array inside a bigger 2D array
代码的目的 :识别屏幕上的按钮/自动获取其坐标,无需人为干预。 (该代码应该在更大的2D数组中找到一个2D数组)。
我是如何解决这个问题的:我将每个RGB像素值存储在一个2D数组中( a[][]
也称为大数组)。 我在optionArrayButton[][]
存储了按钮像素/较小的2Darray。 然后对这些步骤进行编码:(在阅读时查看下面的数组)。
SmallerArray[0][0] = firstSmallerArray
Number的数字 [0][0]
到[0][end]
然后[1][0]
到[1][end]
检查Biger数组的FirstSmallerArray
数字,依此类推。 firstSmallerArray
数,则返回-1
或未not Found
。 smallerArray.length
)和宽度(较小的阵列smallerArray[0].length
。 smallerArray.length
)的高度。 firstSmallerArray
数, smallerArray.length
和smallerArray[0].length
存储。 temp == smallerArray
得到坐标。 我需要帮助:出于某种原因,即使较小的数组在较大的数组中,也表示没有找到按钮(foundButton返回false)。 我花了两天时间就找不到什么错了。
因为我使用的数组有200万+ RGB值,所以我只是给这些数组代替。 更大的数组:
[3 3 1 0 9]
[4 1 5 4 5]
[7 5 6 2 8]
[8 2 7 3 5]
[1 8 7 6 4]
较小的阵列:
[5 6 2]
[2 7 3]
[8 7 6]
我在编码时有点像菜鸟,所以我可能不会理解java /编码术语。 再次感谢任何可以提供帮助的人。
DataStorage DataStorageObject = new DataStorage();
int[][] optionArrayButton = DataStorageObject.optionArrayButton();
int firstSmallerArrayNumber = optionArrayButton[0][0]; //Step 1
int heightOfSmallerArray = optionArrayButton.length; //Step 5
int widthOfSmallerArray = optionArrayButton[0].length; //Step 5
boolean foundButton = false;
//a[][] has the screens rgb values
for(int yaxisCounter = 0; yaxisCounter < 300; yaxisCounter++) //Step 2
{
for(int xaxisCounter = 0; xaxisCounter < 300; xaxisCounter++) //Step 2
{
if(a[yaxisCounter][xaxisCounter] == firstSmallerArrayNumber) //Step 4
{
int[][] tempArray = new int[heightOfSmallerArray][widthOfSmallerArray]; //Step 6
// System.out.println(" " + yaxisCounter + ", " + xaxisCounter);
for(int ycounterForTemp = 0; ycounterForTemp < heightOfSmallerArray; ycounterForTemp++) //Step 6
{
for(int xcounterForTemp = 0; xcounterForTemp < widthOfSmallerArray; xcounterForTemp++) //Step 6
{
tempArray[ycounterForTemp][xcounterForTemp] = a[yaxisCounter][xaxisCounter]; //Step 6
// System.out.println("Storing in temp");
}
}
foundButton = isArrayEqual(tempArray, optionArrayButton); //Step 7
// System.out.println("Button found is a " + foundButton + " statement");
if(foundButton)
{
basePointy = yaxisCounter;
basePointx = xaxisCounter;
// System.out.println("Base Point y is: " + basePointy);
// System.out.println("Base Point x is: " + basePointx);
}
//If there are any problems this is where it would happen
else
{
// System.out.println("Button Found is a : " + "false" + " statement");
// System.out.println("In the nested Else");
continue;
}
}
else
{
// System.out.println("In the else");
continue;
}
}
}
// System.out.println("Button Found is a : " + foundButton + " statement");
使用这种方法:
int[][] matrix = ...;
int[][] submatrix = ...;
loopX: for (int x = 0; x < matrix.length - submatrix.length + 1; ++x)
loopY: for (int y = 0; y < matrix[x].length - submatrix[0].length + 1; ++y)
{
for (int xx = 0; xx < submatrix.length; ++xx)
for (int yy = 0; yy < submatrix[0].length; ++yy)
{
if (matrix[x + xx][y + yy] != submatrix[xx][yy])
{
continue loopY;
}
}
// Found the submatrix!
System.out.println("Found at: " + x + " " + y);
break loopX;
}
System.out.println("Done");
你的幻数300
是可疑的。 也许您的按钮距离左侧或顶部超过300像素? 还要确保使用无损图像。 只有1位错误,这失败了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.