在一个较大的二维数组中寻找一个较小的二维数组。

5

代码目的:在没有人为干预的情况下自动识别屏幕上的按钮/获取其坐标。(该代码旨在在一个更大的2D数组中查找一个2D数组)。

我如何尝试解决它:我将每个RGB像素值存储在一个2D数组(也称为大数组)a [][ ]中。我将按钮像素/较小的2D数组存储在optionArrayButton [][ ]中。 然后编写以下步骤:(阅读本文时请查看下面的数组)。

  1. 获取SmallerArray [0] [0] = firstSmallerArray数字。
  2. 通过遍历[0] [0][0] [end]、然后[1] [0][1] [end]等,检查Biger Array中的FirstSmallerArray号码。
  3. 如果未找到firstSmallerArray编号,则返回-1not Found
  4. 否则,获取其在较大数组中找到的位置。
  5. 获取较小数组的高度(smallerArray.length)和宽度(smallerArray [0] .length)。
  6. 使用firstSmallerArray数字、smallerArray.lengthsmallerArray [0] .length存储在临时数组中。
  7. 检查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");

你的算法是如何准确识别按钮的? - Christian Tapia
1
请不要忘记在问题后面加上一个问号!有些人会在页面中搜索“?”如果在“问题”中没有找到,他们就会直接跳到下一个(实际)问题。 - Andrew Thompson
1个回答

3
请使用以下方法:
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位出错,这个数字就会失败。


我只是在尝试使用300,不知道它在那里。 - user3144079

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接