snake plissken
snake plissken

Reputation: 2669

Histogram computation in java

I try to create a program which computes image histogram. I ve got the above code but i cant figure out why it doesnt work.

    public void hist(List<Integer> r, List g, List b){

    int count[] = new int [256];

    int rSize = r.size();
    int gSize = g.size();
    int bSize = b.size();


    for (int j = 0; j<=255; j++){

        for(int i = 0; i < rSize; i++){

        if( r.get(i) ==  j ){}

            //System.out.println(r.get(i) ==  j);

            count[j]++;

        }
    }

    for (int i = 0; i < count.length; i++) {

        System.out.print(count[i]);

    }
}

If i call it in main, every element of count is rSize which is impossible as r list has the values of Red channel of an image.

Upvotes: 0

Views: 1251

Answers (2)

Roger Lindsj&#246;
Roger Lindsj&#246;

Reputation: 11543

To add to the previous answers that your if is empty (auto formatting in your IDE would show that your indentation is incorrect).

However, there is no need to have nested loops. If r can have any Integer or null, then you can have:

for (Integer integer: r){
    if (integer != null) {
        int i = integer;
        if(i >= 0 && i<= 255) {
            count[i]++;
        }
    }
}

and assuming r only contains integers that fit in count, then you can have

for (int i: r){
    count[i]++;
}

Upvotes: 1

Femaref
Femaref

Reputation: 61437

Your if is empty: if( r.get(i) == j ){}.

It should be:

if( r.get(i) ==  j )
{
    count[j]++;
}

Might want to use the debugger next time and simply step through your code, would've caught this easily.

Upvotes: 4

Related Questions