我想创建图像的颜色直方图,如此处所述(在示例部分中查看):http://en.wikipedia.org/wiki/Color_histogram
我从*示例部分http://upload.wikimedia.org/wikipedia/commons/0/05/Odd-eyed_cat_by_ihasb33r.jpg复制了猫的图片,并得到了不同的结果.
我在下面粘贴我的代码.我有没有犯错?
我的代码:
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
public class Main {
public static void main(String[] args) throws Exception {
int[][][] ch = new int[4][4][4];
BufferedImage image = ImageIO.read(new File("kot.jpg"));
for(int x = 0; x < image.getWidth(); x++)
for(int y = 0; y < image.getHeight(); y++) {
int color = image.getRGB(x, y);
int alpha = (color & 0xff000000) >> 24;
int red = (color & 0x00ff0000) >> 16;
int green = (color & 0x0000ff00) >> 8;
int blue = color & 0x000000ff;
ch[red / 64][green / 64][blue / 64]++;
}
for(int i = 0; i < ch.length; i++)
for(int j = 0; j < ch[i].length; j++)
for(int p = 0; p < ch[i][j].length; p++)
System.out.println("t[" + i + "][" + j + "][" + p + "] = " + ch[i][j][p]);
}
}
**Wikipedia results**:
Red Green Blue Pixel Count
0 0 0 7414
0 0 1 230
0 0 2 0
0 0 3 0
0 1 0 8
0 1 1 372
0 1 2 88
0 1 3 0
0 2 0 0
0 2 1 0
0 2 2 10
0 2 3 1
0 3 0 0
0 3 1 0
0 3 2 0
0 3 3 0
1 0 0 891
1 0 1 13
1 0 2 0
1 0 3 0
1 1 0 592
1 1 1 3462
1 1 2 355
1 1 3 0
1 2 0 0
1 2 1 101
1 2 2 882
1 2 3 16
1 3 0 0
1 3 1 0
1 3 2 0
1 3 3 0
2 0 0 1146
2 0 1 0
2 0 2 0
2 0 3 0
2 1 0 2552
2 1 1 9040
2 1 2 47
2 1 3 0
2 2 0 0
2 2 1 8808
2 2 2 53110
2 2 3 11053
2 3 0 0
2 3 1 0
2 3 2 170
2 3 3 17533
3 0 0 11
3 0 1 0
3 0 2 0
3 0 3 0
3 1 0 856
3 1 1 1376
3 1 2 0
3 1 3 0
3 2 0 0
3 2 1 3650
3 2 2 6260
3 2 3 109
3 3 0 0
3 3 1 0
3 3 2 3415
3 3 3 53929
我的结果:
t[0][0][0] = 28257
t[0][0][1] = 1181
t[0][0][2] = 0
t[0][0][3] = 0
t[0][1][0] = 58
t[0][1][1] = 1414
t[0][1][2] = 156
t[0][1][3] = 0
t[0][2][0] = 0
t[0][2][1] = 0
t[0][2][2] = 0
t[0][2][3] = 0
t[0][3][0] = 0
t[0][3][1] = 0
t[0][3][2] = 0
t[0][3][3] = 0
t[1][0][0] = 2898
t[1][0][1] = 36
t[1][0][2] = 0
t[1][0][3] = 0
t[1][1][0] = 1517
t[1][1][1] = 9858
t[1][1][2] = 1389
t[1][1][3] = 0
t[1][2][0] = 0
t[1][2][1] = 357
t[1][2][2] = 2405
t[1][2][3] = 1
t[1][3][0] = 0
t[1][3][1] = 0
t[1][3][2] = 0
t[1][3][3] = 0
t[2][0][0] = 2794
t[2][0][1] = 3
t[2][0][2] = 0
t[2][0][3] = 0
t[2][1][0] = 7533
t[2][1][1] = 33807
t[2][1][2] = 40
t[2][1][3] = 0
t[2][2][0] = 0
t[2][2][1] = 60782
t[2][2][2] = 371779
t[2][2][3] = 87464
t[2][3][0] = 0
t[2][3][1] = 0
t[2][3][2] = 438
t[2][3][3] = 60218
t[3][0][0] = 0
t[3][0][1] = 0
t[3][0][2] = 0
t[3][0][3] = 0
t[3][1][0] = 123
t[3][1][1] = 4056
t[3][1][2] = 0
t[3][1][3] = 0
t[3][2][0] = 0
t[3][2][1] = 9118
t[3][2][2] = 14215
t[3][2][3] = 118
t[3][3][0] = 0
t[3][3][1] = 0
t[3][3][2] = 12257
t[3][3][3] = 72160
解决方法:
您的代码还可以,但是您用来对其进行测试的图像中的问题:
Wikipedia使用this图片进行测试
而您使用this one