Finding smaller 2D array inside a bigger 2D array

Viewed 2976

Purpose of the Code: to recognize a button on the screen/get its coordinates automatically without human intervention. (The code is supposed to find a 2D array inside a bigger 2D array).

How i tried to solve it: I stored each RGB pixel value in a 2D array (a[][] also called the big array). I Stored the button pixels/smaller 2Darray in the optionArrayButton[][]. Then coded these steps: (Look at the arrays below while reading this).

  1. Get the number for SmallerArray[0][0] = firstSmallerArray Number
  2. Check Biger Array for FirstSmallerArray number by going through [0][0] to [0][end] then [1][0] to [1][end] and so on.
  3. If firstSmallerArray number is not found return -1 or not Found.
  4. Else Get position of where it is found in the bigger Array.
  5. Get the height of Smaller Array (smallerArray.length) and width (smallerArray[0].length).
  6. Using firstSmallerArray number, smallerArray.length, and smallerArray[0].length store in temp array.
  7. check if the temp == smallerArray and get coordinates.

What i need help with: for some reason even though the smaller array is inside the larger array it says button is not found (foundButton returns false). I have spent two days on it and couldn't find whats wrong.

since the arrays I am using has 2 million+ RGB values I am just going to give these arrays instead for example. Bigger array:

[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]

Smaller array:

[5 6 2]
[2 7 3]
[8 7 6]

I am bit of a noob at coding so I likely won't understand java/coding terms. Again thanks for anyone that can help.

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");
1 Answers
Related