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).
- Get the number for
SmallerArray[0][0] = firstSmallerArrayNumber - Check Biger Array for
FirstSmallerArraynumber by going through[0][0]to[0][end]then[1][0]to[1][end]and so on. - If
firstSmallerArraynumber is not found return-1ornot Found. - Else Get position of where it is found in the bigger Array.
- Get the height of Smaller Array (
smallerArray.length) and width (smallerArray[0].length). - Using
firstSmallerArraynumber,smallerArray.length, andsmallerArray[0].lengthstore in temp array. - check if the
temp == smallerArrayand 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");