java中的直方图计算
我尝试创建一个计算图像直方图的程序。我已经得到了上面的代码,但我不明白为什么它不起作用。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的
if
为空:if( r.get(i) == j ){}
。它应该是:
下次可能想使用调试器并简单地单步执行代码,就可以很容易地发现这一点。
Your
if
is empty:if( r.get(i) == j ){}
.It should be:
Might want to use the debugger next time and simply step through your code, would've caught this easily.
添加到前面的答案,您的 if 为空(IDE 中的自动格式会显示您的缩进不正确)。
但是,不需要嵌套循环。如果 r 可以有任何 Integer 或 null,那么您可以:
并且假设 r 只包含适合 count 的整数,那么您可以有
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:
and assuming r only contains integers that fit in count, then you can have