开发者

Arrays: Counting, Comparing and Increase

Woohoo, I've come to arrays now, thank god.

Now, I've got 2 arrays!

int colorvalues[][]  = {{34,255,255,56},{127,204,11,34},{123,98,127,34},{34,34,127,17}};

Imagine it as a 4x4 pixel picture

Now, I want to create a histogram, the distribution of colorvalues from 0 to 255. For example here I've 2*255, 2*127, 5*34 and so on.

So I've created an int histogram[] = new int [255];

To test if my colorvalues are correct I wrote:

 for(int i=0; i < colorvalues.length; i++){
     for (int j = 0; j < colorvalues.length; j++){
         System.out.println("Colorvalue in Array " + i + "." + j + " is" + colorvalues[i][j]);
     }
 }

So far, so good. Now, how do I write a procedure that goes in histogram[255] from 0 to 255, and compares it to the value of colorvalues[][], and if, for example, histogram[34] compares to colorvalues[][] it adds 5 to histogram[34]. Because there's 5 times 34 in colorvalues[][].

Maybe my thinking is wrong and I was supposed to have histogram[255][], 255 for colorvalues开发者_如何学运维 from 0 to 255 and the other for the counter. Even then, how do I realize it?


You actually shouldn't iterate over the histogram and for every possible value iterate over the image. Iterating over the image should be enough:

for(int i=0; i < colorvalues.length; i++){
     for (int j = 0; j < colorvalues.length; j++){
             histogram[colorvalues[i][j]]++;
     }
}

You can simply take the lightness value in your image as an index into the histogram array.

And you don't need to create an array of arrays just to save the counter. Remember that an array is nothing more than a list of "cells" for a certain value. an int[] is just a range of "cells" that can hold an integer value. That's your count. The index into that list of cells is your brightness value for the histogram. For every array you have those two pieces of information: An index of the cell and the value in that cell. You just need to figure out how to use both.

And as PSpeed notes in the comment, you may want to make sure that the code won't actually try accessing values outside the boundaries of that array:

if (colorvalues[i][j] >= 0 && colorvalues[i][j] <= 255) {
    histogram[colorvalues[i][j]]++;
}

That is needed because your color values are ints, that is, they can hold values from −2147483648 to 2147483647. Which is a way larger range than what your histogram can accommodate. So if a color value happens to be 3456 for example, the program would stop in the loop because of an ArrayIndexOutOfBoundsException. Because the code trid to access a value in the histogram array with index 3456 which is way beyond the maximum usable index of 255.

ETA: As for your histogram being int[255]: I totally overlooked that one, sorry. When creating a new array in Java, you specify the length, not the maximum index. So whatever you use there is larger by exactly one than the maximum index that can be used in the array. So new int[256] is an array that has indexes from 0 through 255.


I'd be tempted to add the histogram values into a TreeMap where the map key is the colour value and the map value is the count.

The map will increase in size automatically, no problems with index out of bounds etc. it will be sorted automatically into size order.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜