java中的直方图计算

发布于 2024-12-23 01:54:58 字数 626 浏览 2 评论 0原文

我尝试创建一个计算图像直方图的程序。我已经得到了上面的代码,但我不明白为什么它不起作用。

    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]);

    }
}

如果我在 main 中调用它,则 count 的每个元素都是 rSize,这是不可能的,因为 r 列表具有图像的红色通道的值。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

謌踐踏愛綪 2024-12-30 01:54:58

您的 if 为空:if( r.get(i) == j ){}

它应该是:

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

下次可能想使用调试器并简单地单步执行代码,就可以很容易地发现这一点。

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.

手长情犹 2024-12-30 01:54:58

添加到前面的答案,您的 if 为空(IDE 中的自动格式会显示您的缩进不正确)。

但是,不需要嵌套循环。如果 r 可以有任何 Integer 或 null,那么您可以:

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

并且假设 r 只包含适合 count 的整数,那么您可以有

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

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]++;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文