So, what I’m trying to do is run through a picture that is 1024×768 (Or, popMap.getWidth x popMap.getHeight), grab the blue color of it, compare it to the highest blue so far, and if it’s more, that blue becomes the new ‘newBlue’. Basically, find the highest blue value in an image, closest to 255 blue.
Also, I’m attempting to store the blue value of the entire thing into an array popMapArray, which is a 2d array with 3 columns, storing [blueValue][x][y]. Which I will then sort, to get a list from high to low of the bluest values.
My problem, is that with the code below, it’s only storing into the array when column=767.
I get 1024 [blue,row,767], and then the rest are all [0,0,0]
Any clue why? Java, by the way.
for (int row = 0; row < popMap.getWidth(); row++)
{
for (int column = 0; column < popMap.getHeight(); column++)
{
System.out.println(column);
//Find a Pixel
int c = popMap.getRGB(row, column);
int red = (c & 0x00ff0000) >> 16;
//int green = (c & 0x0000ff00) >> 8;
//int blue = c & 0x000000ff;
// and the Java Color is ...
Color color = new Color(red);
int newBlue = color.getBlue();
int oldBlue = lastColor.getBlue();
switch(popArrayRow)
{
case 0:
{
arrayVar = newBlue;
popArrayRow = 1;
break;
}
case 1:
{
arrayVar = row;
popArrayRow = 2;
break;
}
case 2:
{
arrayVar = column;
popArrayRow = 0;
break;
}
}
popArray[row][popArrayColumn] = arrayVar;
//System.out.println(popArray[row][popArrayColumn]);
switch(popArrayColumn)
{
case 0:
{
popArrayColumn = 1;
break;
}
case 1:
{
popArrayColumn = 2;
break;
}
case 2:
{
popArrayColumn = 0;
break;
}
}
if(newBlue > oldBlue)
{
startX = row;
startY = column;
//System.out.print(row);
//System.out.print(",");
//System.out.println(column);
System.out.print("The oldBlue is ");
System.out.println(oldBlue);
lastColor = color;
}
}
}
You didn’t show the declaration go
popArray(as well as some other variables which I’m assuming are ints that are initialized to 0). You describe it as “a 2d array with 3 columns”. I’m guessing you’ve declared it asint[1024][3], so it has one row per row inpopMap, then your 3 “columns” which are meant to store the blue value, the original x coordinate, and the original y coordinate.So first of all it is unclear how you expect to store one entry in this array for each pixel in the original image. But maybe my guess about how you declared it is wrong.
In any case, each time through the inner loop you essentially want to set
popArray[currentPixel] = {blueValue, origX, origY}but instead you are assigning only one of the three value each time through the loop. So you are doing something like
So hopefully you can already see that something is wrong, since you are populating “columns” that are supposed to go together with values from different iterations of the loop. Even worse, you then start overwriting those values on the next iteration of the inner loop (which will iterate a total of 768 times before
rowincrements):Rather than using 3 array “columns” with different meanings to hold these data elements, it would be wise to make a class that holds the three values and makes clear what’s what.
popArraywould contain this object type. Furthermore, I would make it aListinstead of an array since it is more flexible and you can simply callCollections.sort()on it at the end; or @jsegal has a good suggestion of using a data structure that sorts as the items are inserted. Which one is better might depend on what you want to do with them later.